ASP.NET Image Control

Mondeo

Well-known member
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have this code that Mr Paul kindly helped me with a while ago
Code:
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 im trying to do is take newImg as use it in an asp.net image control. Cant 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
 
Last edited by a moderator:
I cant think of any way to do what you want. Im 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 dont think theres any other way though.
 
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:

Code:
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)
Code:
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 :)
 
Re: Using a separate getImage page

@MrPaul

So your code doesnt save the image on the disk? :confused:

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

So your code doesnt save the image on the disk? :confused:

No, it doesnt 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. Its 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.

:)
 
Last edited by a moderator:
Re: Serving images

No, it doesnt touch the disk. :rolleyes:

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

When I think it through, Ive 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, its like replacing the stream we usually use to save a file to disk with the HTTP response stream.
 
Back
Top