Ramblings of an IT Consultant

Dynamically Resizing an Image in ASP.NET

In asp.net on October 25, 2010 at 8:25 am

I have been working in ASP today rather than ASP.NET and I was a bit rusty at it.  Which is quite odd as I can still remember how to use DataEase for DOS which did not even support a mouse.  Maybe its old age or memory overload, but I really did struggle at the start of the day.  I generally program in ASP.NET these days, however today has got me thinking.  Was life simpler in the old days of  standard ASP?  Ok you can’t have as many bells and whistles but do we actually need them?

Saying that I did use an ASP.NET web page to pass through images for dynamic resizing.  In the old days I would have had to use ASP JPEG or something similar to resize images.  In case your interested I have attached the code below:

<%@ Page Language=”VB” %>

<!–DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<%@Import Namespace=”System.Drawing.Imaging” %>
<script language=”VB” runat=”server”>

Function ThumbnailCallback() as Boolean
Return False
End Function
Sub Page_Load(sender as Object, e as EventArgs)

‘Read in the image filename to create a thumbnail of
Dim imageUrl As String = Request.QueryString(“img”)

‘Read in the width and height
Dim imageHeight as Integer = Request.QueryString(“h”)
Dim imageWidth As Integer = Request.QueryString(“w”)
‘Make sure that the image URL doesn’t contain any /’s or \’s
If imageUrl.IndexOf(“/”) >= 0 Or imageUrl.IndexOf(“\”) >= 0 then
‘We found a / or \
Response.End()
End If

‘Add on the appropriate directory
imageUrl = “~/images/” & imageUrl

Dim bitmapNew = New System.Drawing.Bitmap(Server.MapPath(imageUrl)) ‘ // load original image
Dim widthOrig = bitmapNew.Width
Dim heightOrig = bitmapNew.Height

Dim fullSizeImg as System.Drawing.Image
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl))

If imageHeight = 0 Then
imageHeight = (heightOrig * imageWidth) / widthOrig

End If

‘Do we need to create a thumbnail?
Response.ContentType = “image/jpg”
If imageHeight > 0 And imageWidth > 0 Then
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New  _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

Dim thumbNailImg As System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, _
dummyCallBack, IntPtr.Zero)

thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)

‘Clean up / Dispose…
thumbNailImg.Dispose()
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
End If

‘Clean up / Dispose…
fullSizeImg.Dispose()
End Sub
</script>

I saved the above code in a page called showimage.aspx.  The code below shows how I used the new page within an ImageURL.

imagebutton runat=”server” ID=”imgProd” ImageUrl='<%# “~/Showimage.aspx?img=” & eval(“ProductImage”)  & “&w=203&h=128″ %>’ postbackurl=”someform.aspx” />

So after typing this blog I have realised that I would not want to go backwards so its ASP.NET all the way!

Advertisements
  1. Have you checked out some of the programs that do image manipulation like this as a managed .NET library like ImageSense? They encapsulate the protection and make this type of thing a one-liner (and give the ability to save to a bunch of other formats and apply other changes).

    • Hi Michael Thank you for your comment.

      I have used asp.jpeg for some time now and really liked the way it worked. However a lot of the time I just need to create a quick thumbnail from an uploaded image. Howeber your product does look very interesting. I can see you offer an evaluation download. Over the next couple of weeks, once my workload has decreased, I will take a look at your product and evaluate it for some of my clients use.

  2. Thanks. We have just come out with for exactly those reasons — while doing a bunch of client work we found the need to resize and manipulate uploaded images — without wanting to worry about all the details about image type, signature, format, etc. With ImageSense, we take all of that out of your hands so you can easily convert image types (bmp to jpg to gif to png) and easily thumbnail, save, etc. Let us know how it goes and if you have any questions or need other features – just let us know. We found the collection of reusable code kept growing as we encountered one-offs for format types (and all their edge cases). Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: