473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

ASP.NET Image Resizing and returning in Response.OutputStream


***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.HorizontalResol ution,
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
Jul 21 '05 #1
1 2899

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" <nR****************@btEinternMet.cEom> wrote in message
news:ce**********@titan.btinternet.com...

***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.HorizontalResol ution,
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

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
1
by: Stephen | last post by:
Hi, I am using an Access database (OLE Object) and storing Image as a bytes after streaming. I am able to read it back as a stream and display it on the browser using the following code:...
4
by: dgk | last post by:
I have an app that has many subdirectories containing one or more images (jpg, gif, bmp). When the user selects a directory, my plan is to show the images if there are only one or two, or show...
2
by: Brad | last post by:
I have code which takes an image, uploaded from a web page, and saves it to a database. Now I want to always resize an uploaded image before it is saved to the database. My code to resize is...
1
by: Neil Woodvine | last post by:
***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...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: rob lynch | last post by:
I am having a real problme returning an image from a directory that isn't under my webserver.. I.E. the image is on my F: drive but my website exists in the D: and I have no intention of adding...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
4
by: Mark S. | last post by:
Hello, On a high volume page we have the following JavaScript: img = new Image() img.src = 'http://myserver.com/count.aspx?x=1'; and it works fine, but now we've added: img.onload =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.