Search code examples
c#asp.netvb.netcropper

Cropping Image with Cropper.js & Asp.net


Trying to get a cropped image , but i keep getting the incorrect portion of the image.

I get this back from cropper.js, {left: 316, top: 50.5, width: 150, height: 150}

I pass it to this in vb.

Public Shared Function CropImage(ByVal source As Image, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
    Dim crop As Rectangle = New Rectangle(x, y, width, height)
    Dim bmp = New Bitmap(crop.Width, crop.Height)

    Using gr = Graphics.FromImage(bmp)
        gr.DrawImage(source, New Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel)
    End Using

    Return bmp
End Function

But I end up just getting the top left or top right portion of my image? Is there something I'm missing? I pass it left for the x, and top for the y from cropper. I've tried quite a bit of possibilities and nothing seems to give me my true cropped result.


Solution

  • I figured it out...

    Problem was I was looking at the original photo's height/width. jscropper was looking at a smaller picture when I was cropping it so the coordinates were incorrect when I got the x and y back. Once corrected the function i posted worked perfectly.