Resizing an image using asp.net
This piece of code can resize an image according to the size we need...
We need to import system.drawing namespace and since images are displayed in the browser as binary data, the need for using BinaryWrite at the end.
If you dont know what type of extension image you are displaying, i would assume you would need additional steps to extract the type of image and maybe use case to sort it out.
***resizeImage.GetThumbnailImage(10, 10, dummyCallBack, New System.IntPtr()) is the key function thats responsible for reducing the imagesize.
In the example below, dummyCallBack is the function that returns false...We need to import system.drawing namespace and since images are displayed in the browser as binary data, the need for using BinaryWrite at the end.
If you dont know what type of extension image you are displaying, i would assume you would need additional steps to extract the type of image and maybe use case to sort it out.
***resizeImage.GetThumbnailImage(10, 10, dummyCallBack, New System.IntPtr()) is the key function thats responsible for reducing the imagesize.
Public Function GetThumbnailImage( _
ByVal thumbWidth As Integer, _
ByVal thumbHeight As Integer, _
ByVal callback As Image.GetThumbnailImageAbort, _
ByVal callbackData As IntPtr _
) As Image
The thumbWidth and thumbHeight methods specify the width and height of the resulting thumbnail image. For the third parameter, callback, you need to provide a delegate. To supply the delegate, you must provide in your ASP.NET Web page a function of the following form:
Public Function FunctionName as Boolean
Return False
End Function
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray, 0, byteArray.Length)
Dim resizeImage As System.Drawing.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(byteArray))
Dim thumbNailImage As System.Drawing.Image = resizeImage.GetThumbnailImage(10, 10, dummyCallBack, New System.IntPtr())
thumbNailImage.Save(mstream,resizeImage.RawFormat)
Dim thumbNailByteArray() As Byte = New Byte(mstream.Length) {}
mstream.Position = 0
mstream.Read(thumbNailByteArray, 0, Convert.ToInt32(mstream.Length))
Response.Clear()
Response.ContentType = "Image/jpeg"
Response.BinaryWrite(thumbNailByteArray)
Courtesy..www.4GuysFromRolla.com