PC Review


Reply
Thread Tools Rate Thread

ASP.NET Image Resizing and returning in Response.OutputStream

 
 
Neil Woodvine
Guest
Posts: n/a
 
      29th Jul 2004

***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


 
Reply With Quote
 
 
 
 
Neil Woodvine
Guest
Posts: n/a
 
      30th Jul 2004

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

"Neil Woodvine" <(E-Mail Removed)> wrote in message
news:cebbgr$eub$(E-Mail Removed)...
>
> ***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
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with rendering page using 'Response.Redirect' and 'Response.OutputStream' André Microsoft ASP .NET 3 1st Dec 2006 07:20 AM
Response.OutputStream landers Microsoft ASP .NET 6 8th Jan 2006 04:07 PM
Help on Response.OutputStream Joseph Microsoft C# .NET 6 9th Jan 2004 11:40 AM
saving a bitmap image but not to response.outputstream Joseff Microsoft ASP .NET 1 9th Jan 2004 08:04 AM
Response.OutputStream Jenny Microsoft ASP .NET 3 4th Aug 2003 10:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:33 AM.