Answer the question
In order to leave comments, you need to log in
How to save part of an image to a file in WPF?
I have a task: to write a simple program in which one could load an image, select a separate part, and then save it to a separate file.
I load the image in Image. Which parts need to be saved, I select using a polygon (LMB create new points)
The polygon consists of 4 points, in theory the selected area is a tilted rectangle (I can only draw a normal rectangle). This is a tilted rectangle. I need to make it vertical and save the resulting part of the picture to a file
. How can I do it better?
Answer the question
In order to leave comments, you need to log in
There are 2 options.
First, let's say you have a source image and a selected rectangle:
Image img = ...;
Rectangle selection = ...;
Bitmap bmp = new Bitmap(img);
Bitmap selectedBmp = bmp.Clone(selection, bmp.PixelFormat);
Bitmap selectedBmp = new Bitmap(selection.Width, selection.Height);
using(var g = Graphics.FromImage(selectedBmp))
{
g.DrawImage(img,0,0,selection,GraphicsUnit.Pixel);
}
selectedBmp.Save(...);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question