簡體   English   中英

WPF中BitmapFrame和BitmapImage之間的區別

[英]Difference between a BitmapFrame and BitmapImage in WPF

WPF中的BitmapFrame和BitmapImage有什么區別? 你會在哪里使用(即為什么你會使用BitmapFrame而不是BitmapImage?)

如果你需要獲取位,你應該堅持使用抽象類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);

接受的答案是不完整的(不是暗示我的答案也是完整的),而我的補充可能會幫助某個人。

我使用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)
}

BitmapFrame是用於圖像處理的低級原語。 當您想要將某些圖像從一種格式編碼/解碼到另一種格式時,通常會使用它。

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

如果您只是顯示圖像並想要一些微調BitmapImage就是您所需要的。

如果您正在進行低級圖像處理,那么您將需要BitmapFrame。

暫無
暫無

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

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