C# 使用 WPF 将 WriteableBitmap 保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11212771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:59:29  来源:igfitidea点击:

Save WriteableBitmap to file using WPF

c#wpfwriteablebitmap

提问by Sait

I have:

我有:

WriteableBitmap bmp;

I basicly want to save it into a file on the disk like the following:

我基本上想将它保存到磁盘上的文件中,如下所示:

C:\bmp.png

I read some forums which mentions to read:

我阅读了一些提到阅读的论坛:

bmp.Pixels

and save those pixels into a Bitmapthen use Bitmap.SaveImage()function. However, I can't access any Pixels. Apperantly my WriteableBitmapdoes not have any property named Pixels.

并将这些像素保存到Bitmapthen useBitmap.SaveImage()函数中。但是,我无法访问任何Pixels. 显然 myWriteableBitmap没有任何名为Pixels.

I use .NET Framework 4.0.

我使用 .NET Framework 4.0。

采纳答案by Indy9000

Use your WriteableBitmap's clone and use this function as below:

使用您的 WriteableBitmap 的克隆并使用此函数,如下所示:

CreateThumbnail(filename, _frontBitmap.Clone());

...

...

void CreateThumbnail(string filename, BitmapSource image5)
{
    if (filename != string.Empty)
    {
         using (FileStream stream5 = new FileStream(filename, FileMode.Create))
         {
             PngBitmapEncoder encoder5 = new PngBitmapEncoder();
             encoder5.Frames.Add(BitmapFrame.Create(image5));
             encoder5.Save(stream5);
         }
    }
 }