I must create application, where it can be to load some image, apply some filters, draw few lines and save it. And I must do it using WPF. How I can draw on Image control in WPF? Or another control is better for it?
Asked
Active
Viewed 5,806 times
1 Answers
5
You can do this by adding InkCanvas to your page, add your image as Background Image of InkCanvas, and add save functionality.
- Add InkCanvas to your WPF Form:
- Open the Toolbox (on the View menu, click Toolbox)
- Right-click the Toolbox, and then click Choose Items (Choose Toolbox Items dialog box opens)
- On the WPF Components tab of the Choose Toolbox Items dialog box, scroll down to InkCanvas and select it so that a check appears in the check box.
- Click OK to add the InkCanvas control to the Toolbox.
- Drag an InkCanvas control from the Toolbox to the design surface.
- Add your image as a background of InkCanvas. You can do it either in properties window under Background or in XAML
Add "Save" button to your form and use following code to save it:
string newImagePath = "your file path"; var ms = new MemoryStream(); using (FileStream fs = new FileStream(newImagePath , FileMode.Create) { var rtb = new RenderTargetBitmap((int)inkImageCanvas.Width, (int)inkImageCanvas.Height, 96d, 96d, PixelFormats.Default); rtb.Render(inkImageCanvas); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(fs); }
newImagePath
is the path to new file; inkImageCanvas
is your InkCanvas control.
This will save content of your inkCanval to jpg file.