我只是环顾四周,试图制作一个将多个画布保存为一个图像的工作版本。例如,将它们彼此保存在一起会很好,但我无法弄清楚如何做到这一点。
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);
}
}
答案 0 :(得分:0)
你想要的不是多帧图像,而是通过并置画布来合并画布的图像。所以实际上你在这里只需要1帧,但需要一个很大的组合。有一些方法可以将它们组合在一起,但有一种简单的方法可以用DrawingVisual
来完成。我们可以在该视觉上绘制一个绘图,并使用RenderTargetBitmap
来捕获它。我们使用DrawingGroup
来组合ImageDrawing
s(来自2个RenderTargetBitmaps)。总之,我们必须执行以下步骤:从2个RenderTargetBitmaps中,将它们放在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);
}
}