Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I have this code that Mr Paul kindly helped me with a while ago

'Imports
Imports System.Drawing
Imports System.Drawing.Imaging
'Code
Dim oldImg As Image
Dim newImg As Bitmap
'Load original image
oldImg = Image.FromFile(fileName)
'Create new image at new size
newImg = New Bitmap(oldImg, 200, 150)
'Save
newImg.Save(newFileName, ImageFormat.Jpeg)

What i'm trying to do is take newImg as use it in an asp.net image control. Can't seem to get it to work

 

Image1 = newImg

 

Value of type 'system.drawing.bitmap' cannot be converted to 'system.web.ui.webcontrols.image'

 

Is there a way to do it without saving the new image to disk first?

 

Thanks

Edited by PlausiblyDamp
Posted
I can't think of any way to do what you want. I'm guessing MrPaul suggested that you save the new image to the disk and then set the "ImageUrl" property of your Image instance to point to that new image. I don't think there's any other way though.
Amir Syafrudin
Posted

Using a separate getImage page

 

This is the method I usually use.

 

I suggest you do your image processing when the image is requested, not when the page is requested. This means setting the ImageURL to point to another aspx page which just serves up images, with some parameters in a URL querystring which are used to do the image processing.

 

So, in your page code you would have something like this:

 

Image1.ImageURL = "getImage.aspx?fn=" + fileName + "&w=200&h=150"

 

Then, you create a new blank aspx page, in this case called getImage.aspx, and in the codebehind you place the code needed for the image generation, and serve it directly to the client:

 

(untested)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

   Dim fileName As String
   Dim width, height As Integer
   Dim oldImg As Image
   Dim newImg As Bitmap

   'Get image settings from querystring
   fileName = Request.QueryString("fn")
   width = Integer.Parse(Request.QueryString("w"))
   height = Integer.Parse(Request.QueryString("h"))

   'Load original image
   oldImg = Image.FromFile(fileName)
   
   'Create new image at new size
   newImg = New Bitmap(oldImg, width, height)

   'Serve directly to the client
   Response.Clear()
   Response.ContentType = "image/jpeg"
   newImg.Save(Response.OutputStream, ImageFormat.Jpeg)

End Sub

 

Good luck :)

Never trouble another for what you can do for yourself.
Posted

Re: Using a separate getImage page

 

@MrPaul

 

So your code doesn't save the image on the disk? :confused:

 

Sorry for asking. I rarely deal with Image. :p

Amir Syafrudin
Posted (edited)

Serving images

 

So your code doesn't save the image on the disk? :confused:

 

No, it doesn't touch the disk. :rolleyes:

 

It performs the desired image processing and then instead of saving to disk, it saves the file to the response stream, which then sends it on to the client.

 

I have to admit I have never needed to do it quite as it is here - the systems I worked on dealt with serving images stored in a database, but essentially the process is the same. Create a page for serving images and have it perform the required processing, whether that be altering the image or fetching it from a database, and then send it directly to the client without saving to disk. It's basically how most CAPTCHA systems work, and even the avatars on this forum.

 

If for some reason you were unhappy putting image properties into a querystring, I suppose you could perform the processing when serving the page and store the image data as a session variable. Then the getImage page would simply serve the image which is stored in the session variable.

 

:)

Edited by MrPaul
Never trouble another for what you can do for yourself.
Posted

Re: Serving images

 

No, it doesn't touch the disk. :rolleyes:

 

That explains this portion of your code: [csharp]newImg.Save(Response.OutputStream, ImageFormat.Jpeg)[/csharp]

 

When I think it through, I've used similar methods when serving files directly to be downloaded by a client without having to save them to disk.

 

Looking at the principle of file processing, it's like replacing the stream we usually use to save a file to disk with the HTTP response stream.

Amir Syafrudin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...