gpt4 book ai didi

c# - [C#][WPF]如何将多个canvas保存为一张图片?

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:59 25 4
gpt4 key购买 nike

我只是环顾四周并尝试制作一个将多个 Canvas 保存为一个图像的工作版本。例如,最好将它们保存在彼此附近,但我不知道该怎么做。

public static void CreateSaveBitmap(Canvas canvas1, Canvas canvas2, string filename)
{
RenderTargetBitmap renderBitmap1 = new RenderTargetBitmap((int)canvas1.ActualWidth, (int)canvas1.ActualWidth, 96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas1.Measure(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualWidth));
canvas1.Arrange(new Rect(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualWidth)));

renderBitmap1.Render(canvas1);

RenderTargetBitmap renderBitmap2 = new RenderTargetBitmap((int)canvas2.ActualWidth, (int)canvas2.ActualWidth, 96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas2.Measure(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualWidth));
canvas2.Arrange(new Rect(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualWidth)));

renderBitmap2.Render(canvas2);

//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(renderBitmap1));
encoder.Frames.Add(BitmapFrame.Create(renderBitmap2));
using (FileStream file = File.Create(filename))
{
encoder.Save(file);
}
}

这是我尝试过的方式,但它只保存了第一张图片。有人可以帮我解决这个问题吗?

更新

我纠正了@TheLethalCoder 指出的一些错误,我还将输出文件类型更改为 Tiff,但没有帮助。这是现在的最新版本:

public static void CreateSaveBitmap(Canvas canvas1, Canvas canvas2, string filename)
{
RenderTargetBitmap renderBitmap1 = new RenderTargetBitmap((int)canvas1.ActualWidth, (int)canvas1.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas1.Measure(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualHeight));
canvas1.Arrange(new Rect(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualHeight)));

renderBitmap1.Render(canvas1);

RenderTargetBitmap renderBitmap2 = new RenderTargetBitmap((int)canvas2.ActualWidth, (int)canvas2.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas2.Measure(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualHeight));
canvas2.Arrange(new Rect(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualHeight)));

renderBitmap2.Render(canvas2);

//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
TiffBitmapEncoder encoder = new TiffBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(renderBitmap1));
encoder.Frames.Add(BitmapFrame.Create(renderBitmap2));
using (FileStream file = File.Create(filename))
{
encoder.Save(file);
}
}

最佳答案

您想要的不是多帧图像,而是通过并置 Canvas 图像来组合它们。所以实际上你在这里只需要 1 帧,但需要一个大的组合帧。有一些方法可以将它们结合起来,但是您可以使用 DrawingVisual 执行一种简单的方法。我们可以在该视觉对象上绘制绘图并使用 RenderTargetBitmap 来捕获它。我们使用 DrawingGroup 组合 ImageDrawing(来自 2 个 RenderTargetBitmap)。总之,我们必须执行以下步骤:从 2 个 RenderTargetBitmap,将它们放入 2 个 ImageDrawings 并将它们组合到一个 DrawingGroup 中。然后在 DrawingVisual 上绘制组合绘图。最后使用 RenderTargetBitmap 正常渲染该视觉对象。

这是您可以试用的代码:

public static void CreateSaveBitmap(Canvas canvas1, Canvas canvas2, string filename)
{
RenderTargetBitmap renderBitmap1 = new RenderTargetBitmap((int)canvas1.ActualWidth, (int)canvas1.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);

canvas1.Measure(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualHeight));
canvas1.Arrange(new Rect(new Size((int)canvas1.ActualWidth, (int)canvas1.ActualHeight)));

renderBitmap1.Render(canvas1);

RenderTargetBitmap renderBitmap2 = new RenderTargetBitmap((int)canvas2.ActualWidth, (int)canvas2.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);

canvas2.Measure(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualHeight));
canvas2.Arrange(new Rect(new Size((int)canvas2.ActualWidth, (int)canvas2.ActualHeight)));

renderBitmap2.Render(canvas2);

//Combine the images here
var dg = new DrawingGroup();
var id1 = new ImageDrawing(renderBitmap1,
new Rect(0,0,renderBitmap1.Width, renderBitmap1.Height));
var id2 = new ImageDrawing(renderBitmap2,
new Rect(renderBitmap1.Width, 0
renderBitmap2.Width,
renderBitmap2.Height));
dg.Children.Add(id1);
dg.Children.Add(id2);
var combinedImg = new RenderTargetBitmap((int)(renderBitmap1.Width + renderBitmap2.Width + 0.5),
(int)(Math.Max(renderBitmap1.Height, renderBitmap2.Height) + 0.5), 96, 96, PixelFormats.Pbgra32);
var dv = new DrawingVisual();
using(var dc = dv.RenderOpen()){
dc.DrawDrawing(dg);
}
combinedImg.Render(dv);

//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(combinedImg));

using (FileStream file = File.Create(filename)) {
encoder.Save(file);
}
}

关于c# - [C#][WPF]如何将多个canvas保存为一张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33104637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com