本文介绍了来自文件 PixelFormat 的 BitmapImage 始终是 bgr32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从文件加载图像:

I am loading an image from file with this code:

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 格式.我知道网上有讨论,但我还没有找到解决这个问题的任何方法.有谁知道解决了吗?

It works as expected except for the fact that no matter what kind of image I'm loading (24bit RGB, 8bit grey, 12bit grey,...), after .EndInit() the BitmapImage always has as Format bgr32. I know there have been discussions on the net, but I have not found any solution for this problem.Does anyone of you know if it has been solved yet?

谢谢,

塔比娜

推荐答案

来自 BitmapCreateOptions:

如果没有选择PreservePixelFormat,图片的PixelFormat由系统根据系统决定的内容来选择产生最佳性能.启用此选项会保留文件格式,但可能会导致性能下降.

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

Therefore you also need to set the PreservePixelFormat flag:

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

这篇关于来自文件 PixelFormat 的 BitmapImage 始终是 bgr32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:41