E
E
Evgeny Sh2015-11-24 17:35:28
WPF
Evgeny Sh, 2015-11-24 17:35:28

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

1 answer(s)
A
Alexander Evseev, 2015-12-03
@kryos

There are 2 options.
First, let's say you have a source image and a selected rectangle:

Image img = ...;
Rectangle selection = ...;

1. You can try using Bitmap Bitmap.Clone
Bitmap bmp = new Bitmap(img);
Bitmap selectedBmp = bmp.Clone(selection, bmp.PixelFormat);

2. You can try using Graphics Graphics.DrawImage :
Bitmap selectedBmp = new Bitmap(selection.Width, selection.Height);
using(var g = Graphics.FromImage(selectedBmp))
{
    g.DrawImage(img,0,0,selection,GraphicsUnit.Pixel);
}

Well, then:
selectedBmp.Save(...);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question