Create a new image from a cropped image

I am currently trying to crop the image and then save the new image. I have the original image, the x and y coordinates, where on this image I want the crop to be, and the width and height of the new cropped image.

Here is my code:

Bitmap originalBitmap = new Bitmap(filePath);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(originalBitmap, x, y, width, height);
newImage.Save(newFilePath);

But when the image is saved, this is a small image of the correct height and width, but completely empty.

I'm sure I'm just missing the trick here or completely misunderstanding something (or both!), So any help will be appreciated!

+3
source share
1 answer

try using the Bitmap clone function:

Bitmap newImage = originalBitmap.Clone(new RectangleF(x, y, width, height),  
                                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);
newImage.Save(newFilePath);
+3
source

Source: https://habr.com/ru/post/1718470/


All Articles