Save canvas to bitmap

前端 未结 6 467
感动是毒
感动是毒 2020-12-06 02:32

I want to save my canvas to bitmap. I found some examples in internet, but all of those saves only black image (with size of my canvas). What can I do with this?

Co

6条回答
  •  眼角桃花
    2020-12-06 03:35

    PAY ATTENTION

    if your render is a black image it is because of your incorrect sizing.

    this is a good example for you:

    RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, mXdpi, mYdpi, System.Windows.Media.PixelFormats.Default);
            rtb.Render(my_canvas);
    
            BitmapEncoder pngEncoder = new PngBitmapEncoder();
            pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
    
            using (var fs = System.IO.File.OpenWrite("test.png"))
            {
                pngEncoder.Save(fs);
            }
    

    this code saves a png image from your bitmap that rendered from your canvas.

    Hope helps you.

提交回复
热议问题