2012-06-26 42 views
11

我:保存WriteableBitmap的到文件使用WPF

WriteableBitmap bmp; 

我basicly想將其保存到一個文件中像磁盤上的以下內容:

C:\bmp.png 

我看了一些論壇,其中提到閱讀:

bmp.Pixels 

並保存這些像素劃分爲Bitmap然後使用Bitmap.SaveImage()功能。但是,我無法訪問任何Pixels。突然我的WriteableBitmap沒有任何名爲Pixels的屬性。

我使用.NET Framework 4.0。

+0

只是想提的是'WriteableBitmap.Pixels'是在Silverlight的屬性。它不可用於WPF- –

+0

@AndersGustafsson感謝您的評論。我沒有意識到這一點。乾杯。 – Sait

回答

27

使用您的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); 
     } 
    } 
} 
+0

它工作得很好,謝謝。 – Sait

+1

真的有必要調用'stream.Close()'?不會'Dispose()'自動執行該操作? –