Thursday, April 5, 2012

Get ThumbImage,CanvasImage While uploaded Image in Asp.net

Here is a code that show how to get Thumb Image and Canvas Image From Uploaded Image


In .aspx


 <asp:FileUpload ID="Fuploadprodimg" runat="server"/>


In .vb


Dim strthumbimage, strcanvasimage As String
'for image
If Fuploadprodimg.PostedFile.FileName.ToString.Trim <> "" Then

'---------------code for image exentsion-------------
 Dim ext As String
 ext = Path.GetExtension(Fuploadprodimg.PostedFile.FileName).ToLower()
'Check The Extension--------------------
 If (ext <> ".jpg") And (ext <> ".jpeg") And (ext <> ".gif") And (ext <> ".png") And (ext <> ".bmp") Then
 lblMsg.Text = "Please select jpg or gif or png or bmp Images only"
 Fuploadprodimg.Focus()
  Exit Sub
  End If

'-----------for original image------------------
 Fuploadprodimg.SaveAs(Server.MapPath("../originalimg/") + Fuploadprodimg.FileName)

'-----------for thumb and canvas image------------
strthumbimage = "thumbimg/" + Fuploadprodimg.FileName
strcanvasimage = "canvasimg/" + Fuploadprodimg.FileName

'''''''''Create an image object from the uploaded file
Dim FrontImage As System.Drawing.Image
FrontImage = System.Drawing.Image.FromStream(Fuploadprodimg.PostedFile.InputStream)

''''''''''''''Determine width and height of uploaded image
Dim UploadedImageWidth As Double = FrontImage.PhysicalDimension.Width
Dim UploadedImageHeight As Double = FrontImage.PhysicalDimension.Height
Dim isoscalar, newWidth, newHeight As Double
Dim isocanvas, newWidth1, newHeight1 As Double
isoscalar = Min((178 / UploadedImageWidth), (178 / UploadedImageHeight))
newWidth = isoscalar * UploadedImageWidth
newHeight = isoscalar * UploadedImageHeight

'for canvas------------------
isocanvas = Min((600 / UploadedImageWidth), (600 / UploadedImageHeight))
newWidth1 = isocanvas * UploadedImageWidth
newHeight1 = isocanvas * UploadedImageHeight
 Dim nwidth, wheight As Integer
Dim nwidth1, wheight1 As Integer
nwidth = CType(newWidth, Integer)
wheight = CType(newHeight, Integer)

'for canvas
 nwidth1 = CType(newWidth1, Integer)
 wheight1 = CType(newHeight1, Integer)
 Dim x, y, x1, y1 As Integer
 x = (178 - nwidth) / 2
 y = (178 - wheight) / 2
 x1 = (600 - nwidth1) / 2
 y1 = (600 - wheight1) / 2
Dim newImage, canImage As Bitmap
newImage = New Bitmap(178, 178)
newImage.SetResolution(72, 72) 'web resolution;

'create a graphics object
 'for canvas
 canImage = New Bitmap(600, 600)
canImage.SetResolution(72, 72)
 Dim gr, gr1 As Graphics
 gr = Graphics.FromImage(newImage)
'for canvas
 gr1 = Graphics.FromImage(canImage)

'just in case it's a transparent GIF force the bg to white
 Dim sb As SolidBrush
 sb = New SolidBrush(System.Drawing.Color.Transparent)
 'sb = New SolidBrush(System.Drawing.Color.Transparent)
 gr.FillRectangle(sb, 0, 0, newImage.Width, newImage.Height)

'for canvas--------------
 gr1.FillRectangle(sb, 0, 0, canImage.Width, canImage.Height)
       
 'Re-draw the image to the specified height and width
 gr.DrawImage(FrontImage, x, y, nwidth, wheight)
 gr1.DrawImage(FrontImage, x1, y1, nwidth1, wheight1)
 newImage.Save(Server.MapPath("../thumbimg/") + Fuploadprodimg.FileName)
 canImage.Save(Server.MapPath("../canvasimg/") + Fuploadprodimg.FileName)

Else
 lblMsg.Text = "Please Insert Image"
Exit Sub

End If


No comments:

Post a Comment