473,386 Members | 1,694 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,386 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 2903

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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.