Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Brothers & Friend,
I need help to crop Image from code behind with specific size that is 250 in width and 150 in height so what ever size of image user is uploading our code has to crop in this size but not resize better to crop from center.
please help thanks for your advice and suggestion.
Posted

I tried cropping an image, could not do it. So instead I choose to make a new image.
The below text is the thinking process I used to complete the tasks, which I think is the closest you will get, without using Photoshop or something. I'm sure someone out there has a better solution than mine here.


You have to upload the image fist,
take a peek at the size of the uploaded image,
then get it's size, consider the aspect ratio for calculating
Draw a canvas, set the back color
create a new image based on the uploaded image size, make sure it's smaller than the target size
Set the new image in the canvas
Save the image

Peek at the image
Dim fs As FileStream = New FileStream(p_PhysicalPath, FileMode.Open, FileAccess.Read)
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(fs)
iWidth = image.Width
iHeight = image.Height


Once you have your calculations, you then create the new image. Take note of how you can get tons of information from an image such at the color of every pixel in the image by knowing the pixel location.

Sample code to examine
Dim sourceFile As Drawing.Bitmap = Nothing
Dim convertedfile As Drawing.Bitmap = Nothing
Dim newGraphic As Drawing.Graphics = Nothing

'Create a brand new image, for preview purposes
Try

   Dim staticBGColor As Drawing.Color = System.Drawing.Color.White
   Dim peekImage As Drawing.Bitmap = New Drawing.Bitmap(p_PhysicalPath, True)
   Dim bgColor As System.Drawing.Color = peekImage.GetPixel(1, 1) <- Take Note
   peekImage.Dispose()
   peekImage = Nothing

   convertedfile = New Drawing.Bitmap(cWidth, cHeight,Drawing.Imaging.PixelFormat.Format24bppRgb)
   convertedfile.SetResolution(72, 72)
   convertedfile.MakeTransparent()

   newGraphic = Drawing.Graphics.FromImage(convertedfile)
   newGraphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.Default
   newGraphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
   newGraphic.Clear(bgColor)

   sourceFile = Drawing.Bitmap.FromFile(p_PhysicalPath)

   newGraphic.DrawImage(sourceFile, newX, newY, newWidth, newHeight)
   convertedfile.Save(nPhysicalPath, Drawing.Imaging.ImageFormat.Jpeg)

Catch ex As Exception

End Try

newGraphic.Dispose()
convertedfile.Dispose()
sourceFile.Dispose()
 
Share this answer
 
Thank you so much for your solutions i had modified little bit and it is working superb for me.




VB
Dim rd As New Random
Label3.Text = "MEM" + rd.Next(1000000000).ToString
Dim uploadFolder As String = Request.PhysicalApplicationPath + "photo\"
Dim x As Integer, y As Integer, width As Integer, height As Integer
x = InlineAssignHelper(y, 0)
'X, Y values for crop the image.

If FileUpload1.HasFile Then
    Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
    FileUpload1.SaveAs(uploadFolder & lblticket.Text & Label3.Text & extension)
    fu1.Text = "http://ltho-14847/newbangdam/photo/" & lblticket.Text & Label3.Text & extension

    width = 270
    height = 100
    Dim cropArea As New Rectangle(x, y, width, height)
    Dim bmpImage As New Bitmap(FileUpload1.PostedFile.InputStream)
    Dim bmpCrop As Bitmap = bmpImage.Clone(cropArea, bmpImage.PixelFormat)
    ' bmpCrop.Save("c:\temp\test.jpg")
    'bmpCrop.Save(uploadFolder & lblticket.Text & Label2.Text)
    bmpCrop.Save(Request.PhysicalApplicationPath + "photo\thumbs\" & lblticket.Text & Label3.Text & extension)
    '  fu1.Visible = False
    lblDone.Text = "Your Photo is Uploaded, Suffessfully !"

Else
    fu1.Text = "http://ltho-14847/newbangdam/photo/blankuser.jpg"
    fu1.Text = "First select a file."
End If




VB
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
    target = value
    Return value
End Function
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900