Crop Image – C# and VB.NET

For cropping an image in C# & VB.NET we can use the Graphics class. We’ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  Let’s code!

The Code


C#:


VB.NET

Explanation

The above code will work perfectly but it seems confusing, doesn’t it? You might have been wondering how it works. Well…

We first make an empty bitmap of the same width/height as the section rectangle. This bitmap will hold the cropped image. We use the section rectangle’s width/height because it determines the size of the cropped image. Then we use the Graphics class’s DrawImage() method (function) for the cropping. It draws the given area (section) of the source image on the empty bitmap and that’s it. The empty bitmap now holds the cropped image.

Source Code

Download Source Code for C#

Download Source Code for VB.NET

If you have any questions, don’t hesitate to post a comment.

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *