2008-09-30 210 views

回答

12

你應該堅持使用抽象類BitmapSource,如果你需要獲取的位,甚至ImageSource,如果你只是想畫出來。

實現BitmapFrame僅僅是實現的面向對象的性質。您實際上不需要區分這些實現。 BitmapFrames可能包含一些額外的信息(元數據),但通常只有成像應用程序纔會關心。

你會發現,從繼承的BitmapSource這些其他類:

  • BitmapFrame
  • 的BitmapImage
  • CachedBitmap
  • ColorConvertedBitmap
  • CroppedBitmap
  • FormatConvertedBitmap
  • RenderTargetBitmap
  • TransformedBitmap
  • WriteableBitmap的

您可以通過構建一個BitmapImage的對象得到一個URI一個的BitmapSource:

Uri uri = ...; 
BitmapSource bmp = new BitmapImage(uri); 
Console.WriteLine("{0}x{1}", bmp.PixelWIdth, bmp.PixelHeight); 

的的BitmapSource也可能來自一個解碼器。在這種情況下,您是間接使用BitmapFrames。

Uri uri = ...; 
BitmapDecoder dec = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default); 
BitmapSource bmp = dec.Frames[0]; 
Console.WriteLine("{0}x{1}", bmp.PixelWIdth, bmp.PixelHeight); 
+0

你甚至可以如何使用ImageSource?你如何加載一個帶有Uri的BitmapSource? – 2008-09-30 22:57:50

0

BitmapFrame是圖像操作的低級別原語。通常在您想要將某個圖像從一種格式編碼/解碼到另一種格式時使用。

BitmapImage是更高層次的抽象,具有一些整齊的數據綁定屬性(UriSource等)。

如果你只是顯示圖像,並希望一些微調BitmapImage是你所需要的。

如果你正在做低級圖像處理,那麼你將需要BitmapFrame。

0

我知道這是一個古老的問題,但接受的答案是不完整的(不建議我的答案是完整的),我的補充可能有助於某人某處。

原因(儘管只有原因)我使用BitmapFrame是當我訪問使用TiffBitmapDecoder類的多幀TIFF圖像的個別幀。例如,

TiffBitmapDecoder decoder = new TiffBitmapDecoder(
    new Uri(filename), 
    BitmapCreateOptions.None, 
    BitmapCacheOption.None); 

for (int frameIndex = 0; frameIndex < decoder.Frames.Count; frameIndex++) 
{ 
    BitmapFrame frame = decoder.Frames[frameIndex]; 
    // Do something with the frame 
    // (it inherits from BitmapSource, so the options are wide open) 
}