【问题标题】:BitmapImage from file PixelFormat is always bgr32来自文件 PixelFormat 的 BitmapImage 始终为 bgr32
【发布时间】:2013-02-19 18:47:27
【问题描述】:

我正在使用以下代码从文件中加载图像:

BitmapImage BitmapImg = null;
BitmapImg = new BitmapImage();
BitmapImg.BeginInit();
BitmapImg.UriSource = new Uri(imagePath);
BitmapImg.CacheOption = BitmapCacheOption.OnLoad;
BitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
BitmapImg.EndInit();

它按预期工作,除了无论我加载哪种图像(24 位 RGB、8 位灰色、12 位灰色……),在 .EndInit() 之后,BitmapImage 始终具有格式 bgr32。我知道网上有讨论,但我没有找到任何解决这个问题的方法。 有谁知道这个问题解决了吗?

谢谢,

塔比娜

【问题讨论】:

  • 非常感谢!这解决了我的问题。

标签: wpf bitmapimage pixelformat


【解决方案1】:

来自BitmapCreateOptions 的备注部分:

如果未选择 PreservePixelFormat,则图像的 PixelFormat 由系统根据系统确定的意愿选择 产生最佳性能。启用此选项会保留文件 格式,但可能会导致性能下降。

因此你还需要设置PreservePixelFormat标志:

var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imagePath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                     | BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多