将画布保存到位图

问题描述:

我想将我的画布保存到位图。我在网上找到了一些例子,但所有这些只保存黑色图像(与我的画布大小)。 我能做些什么?将画布保存到位图

代码:

public static void SaveCanvasToFile(Canvas surface, string filename) 
    { 
     Size size = new Size(surface.Width, surface.Height); 

     surface.Measure(size); 
     surface.Arrange(new Rect(size)); 

     // Create a render bitmap and push the surface to it 
     RenderTargetBitmap renderBitmap = 
      new RenderTargetBitmap(
      (int)size.Width, 
      (int)size.Height, 
      96d, 
      96d, 
      PixelFormats.Pbgra32); 
     renderBitmap.Render(surface); 

     // Create a file stream for saving image 
     using (FileStream outStream = new FileStream(filename, FileMode.Create)) 
     {    
      BmpBitmapEncoder encoder = new BmpBitmapEncoder(); 
      // push the rendered bitmap to it 
      encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
      // save the data to the stream 
      encoder.Save(outStream); 
     }} 
+0

您是否使用调试器完成了这一步?有没有什么可疑的,比如'surface.Width'返回0? – 2011-05-01 20:29:25

+0

不,在调试器中,一切都看起来正常 – zc21 2011-05-01 20:42:58

+0

使用你的代码我只是成功地呈现了一大堆(〜25)的控件(尽管我使用了ActualWidth和ActualHeight属性,因为我很少明确地设置大小) – 2011-05-01 21:16:16

在我的渲染代码,我叫target.UpdateLayout();target.Arrange(new Rect(size));后,也许这将修复它。另请注意,如果未设置画布背景,则会将其呈现为透明,而对BMP进行编码时可能会变成纯黑色,因此如果您只有黑色物体,它们可能不可见。

+0

我试过UpdateLayout()并没有帮助。 – zc21 2011-05-01 21:01:13

+0

如果我编码为PNG,则获取透明图像,但画布背景设置为白色 – zc21 2011-05-01 21:02:17

+0

Canvas上是否应用了任何转换? – 2011-05-01 21:03:57

尝试这样的回答:

public void ExportToPng(Uri path, Canvas surface) 
{ 
    if (path == null) return; 

    // Save current canvas transform 
    Transform transform = surface.LayoutTransform; 
    // reset current transform (in case it is scaled or rotated) 
    surface.LayoutTransform = null; 

    // Get the size of canvas 
    Size size = new Size(surface.Width, surface.Height); 
    // Measure and arrange the surface 
    // VERY IMPORTANT 
    surface.Measure(size); 
    surface.Arrange(new Rect(size)); 

    // Create a render bitmap and push the surface to it 
    RenderTargetBitmap renderBitmap = 
    new RenderTargetBitmap(
     (int)size.Width, 
     (int)size.Height, 
     96d, 
     96d, 
     PixelFormats.Pbgra32); 
    renderBitmap.Render(surface); 

    // Create a file stream for saving image 
    using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create)) 
    { 
    // Use png encoder for our data 
    PngBitmapEncoder encoder = new PngBitmapEncoder(); 
    // push the rendered bitmap to it 
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
    // save the data to the stream 
    encoder.Save(outStream); 
    } 

    // Restore previously saved layout 
    surface.LayoutTransform = transform; 
} 

这个答案在这里复制为了方便从this page.

+0

这对我来说很好。实际上,我使用此代码创建了数百个图像,但是一旦这些代码创建的图像是黑色的。什么可能是问题? – SST 2012-10-25 12:05:12

尝试设置画布底色为白色

var fileName = "img.jpg"; 
var bitMap = new WriteableBitmap(DrawCanvas, null); 
var ms = new MemoryStream(); 
System.Windows.Media.Imaging.Extensions.SaveJpeg(bitMap, ms, bitMap.PixelWidth, bitMap.PixelHeight, 0, 100); 
ms.Seek(0, SeekOrigin.Begin); 
var library = new MediaLibrary(); 
library.SavePicture(string.Format("{0}", fileName), ms); 

注意

如果你的渲染是一个黑色的图像,这是因为你的尺寸不正确。

这是一个很好的例子给你:

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); 
     } 

这个代码从您的位图,从你的画布渲染保存为PNG图像。

希望可以帮助你。