Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / July 2004

Tip: Looking for answers? Try searching our database.

ASP.NET Image Resizing and returning in Response.OutputStream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neil Woodvine - 29 Jul 2004 18:18 GMT
***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
Neil Woodvine - 30 Jul 2004 16:42 GMT
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

> ***Scenario ...
> I have a DataList with a hyperlink WebControl in the Item Template.
[quoted text clipped - 55 lines]
> Thanks,
> Neil

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.