locked
Image Cropping RRS feed

  • Question

  • User294545519 posted

     Hi,

    Can anyone see any errors with the code below? My crop function doesn't crop the areas specified it seems. Thanks.

     

     

     

        Public Shared Function CropImage(ByVal OriginalImage As Bitmap, ByVal TopLeft As Point, ByVal BottomRight As Point) As Bitmap
            Dim btmCropped As New Bitmap((BottomRight.Y - TopLeft.Y), (BottomRight.X - TopLeft.X))
            Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)
    
            grpOriginal.DrawImage(OriginalImage, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), _
                TopLeft.X, TopLeft.Y, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)
            grpOriginal.Dispose()
    
            Return btmCropped
        End Function
      
    Tuesday, November 11, 2008 7:08 AM

Answers

  • User187056398 posted

    It works.  There must be something else wrong.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 11, 2008 9:20 AM
  • User187056398 posted

    Could this have something to do with the resolution of the original images?

    It's not likely.  A pixel is a pixel.

    Try the simplest possible case.  Start with an image 100 x 100.  Try to crop the upper left 50 x 50 (1/4 the image).  If you can get that to work....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 12, 2008 9:00 AM

All replies

  • User187056398 posted

    It works.  There must be something else wrong.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 11, 2008 9:20 AM
  • User294545519 posted

     OK cheers. Heres the code I'm using the function in (img.CropImage is the function I previously posted):

     

     

       Dim ImageURL As String = "/Uploads/Gallery/" + ImageID + ".jpg"
    
            Dim oBitmap As New Bitmap(Server.MapPath(ImageURL))
    
            Dim Ratio As Integer = oBitmap.Width / 572
    
    
            Try
    
                Dim X1, Y1, X2, Y2, Width, Height As Integer
    
                Try
                    X1 = x1txt.Text
                    Y1 = y1txt.Text
                    X2 = x2txt.Text
                    Y2 = y2txt.Text
                    Width = widthtxt.Text
                    Height = heighttxt.Text
                Catch
                End Try
    
                X1 = X1 * Ratio
                Y1 = Y1 * Ratio
                X2 = X2 * Ratio
                Y2 = Y2 * Ratio
                Width = Width * Ratio
                Height = Height * Ratio
    
                Dim TopLeft As New Point(X1, Y1)
                Dim BottomRight As New Point(CInt(X1) + CInt(Height), CInt(Y1) + CInt(Width))
    
                Dim BitmapToSave As New Bitmap(oBitmap.Width, oBitmap.Height)
    
                Dim objGraphics As System.Drawing.Graphics
                objGraphics = System.Drawing.Graphics.FromImage(BitmapToSave)
                objGraphics.DrawImage(oBitmap, 0, 0)
    
                oBitmap.Dispose()
    
                BitmapToSave = img.CropImage(BitmapToSave, TopLeft, BottomRight)
    
                BitmapToSave.Save(Server.MapPath(ImageURL))
    
                BitmapToSave.Dispose()
    
            Catch
            End Try
      
    Tuesday, November 11, 2008 11:06 AM
  • User187056398 posted

    Dim Ratio As Integer = oBitmap.Width / 572

    Numerical Analysis 101, Integer math:

        500 / 501 = 0

        501 / 500 = 1

    Change your math to use floating point.

    Tuesday, November 11, 2008 11:39 AM
  • User294545519 posted

     Thanks for your reply.

    For testing purposes Ive removed anything to do with the 'Ratio' integer. However my images are still cropping strange. Could this have something to do with the resolution of the original images?

    Thanks again.

    Wednesday, November 12, 2008 4:39 AM
  • User187056398 posted

    Could this have something to do with the resolution of the original images?

    It's not likely.  A pixel is a pixel.

    Try the simplest possible case.  Start with an image 100 x 100.  Try to crop the upper left 50 x 50 (1/4 the image).  If you can get that to work....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 12, 2008 9:00 AM