Code to make a Thumbnail image in asp.net
Code to make a Thumbnail image
Description
This code is use to make a thumbnail of an image. generally,
it is use when we have to give the facility to upload images in applications folder. then
it have to create thumbnail first...
You have to add below code in seperate page which will access by main page with
file name consist in query string.
eg. "webform1.aspx?file=filename.jpg"
NOTE: you have to sent filename with QueryString.Add Namespaces
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.ImagingAdd below code on page load
Try
' get the file name -- fall800.jpg
Dim file As String = Request.QueryString("file")
file = Server.MapPath("headshotsThumbs") + "\" & file
' create an image object, using the filename we just retrieved
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(file)
' create the actual thumbnail image
Dim thumbnailImage As System.Drawing.Image = New Bitmap(Server.MapPath(Request.ApplicationPath) & "\headshotsThumbs\" &
Request.QueryString("file"))
' make a memory stream to work with the image bytes
Dim imageStream As New MemoryStream()
' put the image into the memory stream
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
' make byte array the same size as the image
Dim imageContent As Byte() = New Byte(imageStream.Length - 1) {}
' rewind the memory stream
imageStream.Position = 0
' load the byte array with the image
imageStream.Read(imageContent, 0, CInt(imageStream.Length))
' return byte array to caller with image type
Response.ContentType = "image/jpeg"
Response.BinaryWrite(imageContent)
Catch ex As Exception
Dim [error] As String = ""
'Response.Write(ex.Message);
[error] = ex.Message.ToString()
End Try
It will find the perticular image and will create thumbnail.
Thanks
Nilesh Jadhav
