0

I have created a custom image class that overrides the onRender(DrawingContext dc) method. In this method I use the drawingContext to draw lines, dots and text into the image. Until then everything works. Now I want to save the image including the drawn elements in a folder. I have searched very long, but unfortunately found nothing suitable. Does anyone have an idea how I can save the image with the drawn elements as a bitmap?

Edit: This is what I have now:

cachedTargetBitmap = new RenderTargetBitmap((int)ActualWidth, 
(int)ActualHeight, 96, 96, PixelFormats.Pbgra32);

cachedTargetBitmap.Render(this);
var encoder = new BmpBitmapEncoder();
BitmapFrame frame = BitmapFrame.Create(Image1.TargetBitmap);
encoder.Frames.Add(frame);

using (var stream = File.Create(string.Format("{0}\\Left.bmp", SavePath)))
{
     encoder.Save(stream);
}

var encoder2 = new BmpBitmapEncoder();
BitmapFrame frame2 = BitmapFrame.Create(Image2.TargetBitmap);
encoder2.Frames.Add(frame2);

using (var stream = File.Create(string.Format("{0}\\Right.bmp", SavePath)))
{
encoder2.Save(stream);
}

I have 2 WPF Images and I draw lines, points and text on them. At saving the images as Bitmap, the first image is saved without the text, but with the points and lines and it is shifted to the right. The second image is saved completely black.

David Fox
  • 10,603
  • 9
  • 50
  • 80
  • Render you custom control into a RenderTargetBitmap, then save that by means of a BitmapEncoder. – Clemens Dec 04 '19 at 15:18
  • I just did something like this and I was only partially successful. I have 2 WPF Images and I draw lines, points and text on them. At saving the images as Bitmap, the first image is saved without the text, but with the points and lines and it is shifted to the right. The second image is saved completely black. – mehmet_demir Dec 05 '19 at 16:43
  • @Clemens `cachedTargetBitmap = new RenderTargetBitmap((int)ActualWidth, (int)ActualHeight, 96, 96, PixelFormats.Pbgra32); cachedTargetBitmap.Render(this);` – mehmet_demir Dec 05 '19 at 16:45
  • In case you want to save semi-transparent images, do not use BmpBitmapEncoder. Use a PngBitmapEncoder instead. – Clemens Dec 05 '19 at 17:02
  • Any chance you could share a [mcve] by including a demo version of your custom image class? – dbc Dec 05 '19 at 18:30

1 Answers1

-1

If you are dispaying it in a picturebox, you can do something as follows - Also be nice if you shared your current code.

 Bitmap snapshot As New Bitmap(PictureBox1.Image)
 PictureBox1.Image.Save("Path here\testImage.png", System.Drawing.Imaging.ImageFormat.Png)
Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45