I have no idea how to cut a rectangle image from other big image.
Let's say there is 300 x 600 image.png.
I want just to cut a rectangle with X: 10 Y 20 , with 200, height 100 and save it into other file.
How I can do it in C#?
Thanks!!!
Check out the Graphics Class on MSDN.
Here's an example that will point you in the right direction (notice the Rectangle
object):
public Bitmap CropImage(Bitmap source, Rectangle section)
{
var bitmap = new Bitmap(section.Width, section.Height);
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bitmap;
}
}
// Example use:
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));
Bitmap CroppedImage = CropImage(source, section);
Another way to corp an image would be to clone the image with specific starting points and size.
int x= 10, y=20, width=200, height=100;
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Bitmap CroppedImage = source.Clone(new System.Drawing.Rectangle(x, y, width, height), source.PixelFormat);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With