将WPF画布保存为图像

时间:2020-12-17 21:19:12

I was following this article and I got my canvas to be saved, however, I want to extend the code's functionality and save a particular part of my canvas as an image, rather than my entire canvas.

我正在关注这篇文章,我得到了保存的画布,但是,我想扩展代码的功能并将画布的特定部分保存为图像,而不是整个画布。

I tried setting the rect.Offset and rect.Location properties but the image is always saved from the upper left corner of my canvas.

我尝试设置rect.Offset和rect.Location属性,但图像总是从我画布的左上角保存。

Does anyone know how can I achieve my wanted functionality in a similar way?

有谁知道如何以类似的方式实现我想要的功能?

Thanks!

谢谢!

3 个解决方案

#1


16  

A simple method would be to use a CroppedBitmap after rendering the whole canvas. You could reuse the same RenderTargetBitmap if you need multiple images.

一种简单的方法是在渲染整个画布后使用CroppedBitmap。如果需要多个图像,可以重复使用相同的RenderTargetBitmap。

RenderTargetBitmap rtb = new RenderTargetBitmap((int)canvas.RenderSize.Width,
    (int)canvas.RenderSize.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);

var crop = new CroppedBitmap(rtb, new Int32Rect(50, 50, 250, 250));

BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(crop));

using(var fs = System.IO.File.OpenWrite("logo.png"))
{
    pngEncoder.Save(fs);
}

#2


0  

Looking at the link you posted, obviously you can choose the rendered target coordinates here.

查看您发布的链接,显然您可以在此处选择渲染的目标坐标。

RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
     (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);

#3


0  

See if this solution works for you.

看看这个解决方案是否适合您。

Size size = new Size(width, height);
canvas.Measure(size);
canvas.Arrange(new Rect(X, Y, width, height));

//Save Image
...  
...

// Revert old position
canvas.Measure(new Size());

#1


16  

A simple method would be to use a CroppedBitmap after rendering the whole canvas. You could reuse the same RenderTargetBitmap if you need multiple images.

一种简单的方法是在渲染整个画布后使用CroppedBitmap。如果需要多个图像,可以重复使用相同的RenderTargetBitmap。

RenderTargetBitmap rtb = new RenderTargetBitmap((int)canvas.RenderSize.Width,
    (int)canvas.RenderSize.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);

var crop = new CroppedBitmap(rtb, new Int32Rect(50, 50, 250, 250));

BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(crop));

using(var fs = System.IO.File.OpenWrite("logo.png"))
{
    pngEncoder.Save(fs);
}

#2


0  

Looking at the link you posted, obviously you can choose the rendered target coordinates here.

查看您发布的链接,显然您可以在此处选择渲染的目标坐标。

RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
     (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);

#3


0  

See if this solution works for you.

看看这个解决方案是否适合您。

Size size = new Size(width, height);
canvas.Measure(size);
canvas.Arrange(new Rect(X, Y, width, height));

//Save Image
...  
...

// Revert old position
canvas.Measure(new Size());