簡體   English   中英

將 canvas 保存到 bitmap

[英]Save canvas to bitmap

我想將我的 canvas 保存到 bitmap。 我在互聯網上找到了一些例子,但所有這些都只保存黑色圖像(我的畫布大小)。 我能用這個做什么?

代碼:

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

試試這個答案:

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

為了方便起見,從[此頁面] 復制了此答案。 -xps-document-or-raw-xaml/ )

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

此代碼從您的 bitmap 保存一個 png 圖像,該圖像是從您的 canvas 呈現的。

希望能幫到你。

如果您正在使用 windows 10 UWP 應用程序,請查看此頁面:

Microsoft Docs 上的 RenderTargetBitmap Class

作為獎勵,UWP 簡化了流程。

在我的渲染代碼中,我調用target.UpdateLayout(); target.Arrange(new Rect(size)); ,也許這會解決它。 另請注意,如果未設置 canvas 背景,它將被渲染為透明,而編碼為 BMP 時可能會變成純黑色,因此如果您只有黑色對象,它們可能是不可見的。

嘗試將 canvas 背景顏色設置為白色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM