ASP.NET Image Resizing and returning in Response.OutputStream

N

Neil Woodvine

***Scenario ...
I have a DataList with a hyperlink WebControl in the Item Template.
I want to display a 64x64 image in the Hyperlink and set the NavigateURL to
the full size image.

***Source Data Item for Databinding is a class with 2 props ...
URL
Description

***MyMainPage.aspx Page_Load Code ...
Dim myImages As ArrayList = New ArrayList
myImages.Add(New ImageItem(URL As String =
"http://localhost/mywebapp/images/bigpic.bmp", Description As String = "My
bigpic")
DataList1.DataSource = myImages
DataList1.DataBind()

***Databing Code for Hyperlink in DataList1 on MyMainPage.aspx ...
*Tooltip = [DataBinder.Eval(Container.DataItem, "Description")
*NavigateURL = [DataBinder.Eval(Container.DataItem, "URL")
*ImageURL = ["ImageProcessor.aspx?filename=" &
DataBinder.Eval(Container.DataItem, "URL") & "&width=64&height=64"]

***ImageProcessor.aspx Page_Load Code (Imports System.Drawing) ...
Dim imagePath As String = Request.Params("filename")
Dim requestedWidth As Integer = Request.Params("width")
Dim requestedHeight As Integer = Request.Params("height")
Dim sourceImage As Image = Image.FromFile(imagePath)
Dim newImage As Bitmap = New Bitmap(requestedWidth, requestedHeight,
Imaging.PixelFormat.Format24bppRgb)
newImage.SetResolution(sourceImage.HorizontalResolution,
sourceImage.VerticalResolution)
Dim gr As Graphics = Graphics.FromImage(newImage)
gr.InterpoloationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gr.DrawImage(sourceImage, _
New Rectangle(0, 0, requestedWidth, requestedHeight), _
New Rectangle(0, 0, sourceImage.Width, sourceImage.Height), _
GraphicsUnit.Pixel)
gr.Dispose()
Response.ContentType = "image/bmp"
newImage.Save(Response.OutputStream, Imaging.ImageFormat.Bmp)

==================================
When I run this code I get the hyperlink control with a 'X' in it because it
could not load
an image, but the link itself IS active and correctly takes me to a view of
the full size image.

I've tested the resizing code used in ImageProcessor.aspx on a Windows Form
with a
couple of picture boxes and all seems to be fine, so I conclude that my
problem lies in
how I am writing the resized image to the Response OutputStream (new ground
for me).

So, if anyone can spot my mistake I'd be very grateful.

Thanks,
Neil
 
N

Neil Woodvine

Sorted it.

Just read elsewhere that Response.OutputStream only likes GIFs or JPEGs, not
BMPs.

So I Changed ...
Response.ContentType = "image/bmp"
newImage.Save(Response.OutputStream, Imaging.ImageFormat.Bmp)
To ...
Response.ContentType = "image/jpeg"
newImage.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

and all is just dandy!

Cheers,
Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top