sroughley

Hi all,

I need to write a method that takes a Drawing.Image plus two Int32s for dimensions and returns a cropped version of the image in the same format as it was supplied in. So far I have:
...
Public Function CropImage(ByVal img As Image, _

ByVal height As Int32, _

ByVal width As Int32) As Image

Try

Dim sourceImage As Bitmap = New Bitmap(img)

Dim resultImage As Bitmap = New Bitmap(width, height)

Dim resultGraphics As Graphics = Graphics.FromImage(resultImage)

resultGraphics.DrawImage(sourceImage, 0, 0)

Catch ex As Exception

Throw ex

End Try

End Function

...

As you can probably tell, I am now stuck at getting the Drawing.Image back from the Graphics. How can I achieve this

Regards,

Stephen.



Re: Common Language Runtime Cropping a System.Drawing.Image

Adam Wayne

The resultImage is the bitmap. The graphics object will draw directly on the bitmap. The cropped image will be drawn on the bitmap and can be saved to disk, to a stream, or drawn to another graphics object. For the purposes of your function your return value should be resultImage.

As a side note, make sure to dispose the graphics object. I'd suggest declaring the graphics object in a using clause.

Hope this helps!





Re: Common Language Runtime Cropping a System.Drawing.Image

sroughley

Thanks for the reply.

I see where I have gone wrong in the above example now. The line resultGraphics.DrawImage(sourceImage, 0, 0)  should read resultGraphics.DrawImage(newImage, 0, 0). I wasn't aware that DrawImage applies the image to the Bitmap or that the Bitmap could be saved to a Stream. So I now have:
...

Public Function CropImage(ByVal img As Image, _

ByVal height As Int32, _

ByVal width As Int32) As Image

Dim outImage As Image

Dim ms As MemoryStream

Try

Dim sourceImage As Bitmap = New Bitmap(img)

Dim resultImage As Bitmap = New Bitmap(width, height)

Dim resultGraphics As Graphics = Graphics.FromImage(resultImage)

resultGraphics.DrawImage(sourceImage, 0, 0)

ms = New MemoryStream()

sourceImage.Save(ms, img.RawFormat)

outImage = Image.FromStream(ms)

Return outImage

Catch ex As Exception

Throw ex

Finally

If (Not ms Is Nothing) Then ms.Close()

End Try

End Function

...

I'll give this a try this afternoon and let you know how it works, but it looks like its exactly what we're after!

Thanks,

Stephen.





Re: Common Language Runtime Cropping a System.Drawing.Image

sroughley

Also forgot to dispose the Graphics object in the above code, but I have applied it in our source now.

Regards,

Stephen.





Re: Common Language Runtime Cropping a System.Drawing.Image

Adam Wayne

You wouldn't even need to write the image to the stream for your above example. You can just do:

Public Function CropImage(ByVal img As Image, _
ByVal height As Int32, _
ByVal width As Int32) As Image

Dim outImage As Image

Try
Dim sourceImage As Bitmap = New Bitmap(img)
Dim resultImage As Bitmap = New Bitmap(width, height)

Using resultGraphics As Graphics = Graphics.FromImage(resultImage)

resultGraphics.DrawImage(sourceImage, 0, 0)

return resultImage

End Using

End Function

This will draw the image onto the resultImage bitmap and return that image. The resultImage bitmap object itself has the cropped image drawn on it. No need to do any more than just return it out and do with it what you'd like. The resultImage will already be backed by a memory stream internally, so when returned the data of the image will live as long as the resultImage object lives...I hope this makes it more clear.