2012-04-02 191 views
0

添加8 bpp PNG到您的资源文件。 如果您尝试使用它,比如:如何防止VS设置为32ARGB从8 bpp原始图像的位图

Bitmap bmp = properties.Resources.My8bppImage; 

的BMP的PixelFormat将是32 ARGB!但它是错误的,应该是8 bpp索引。 如何获取正确的位图?

+0

你能显示一些代码吗? – 2012-04-02 18:46:08

回答

2

这里没有很多选项,Visual Studio资源编辑器和Bitmap类都使用PNG解码器,它将图像转换为32bpp。这意味着很有帮助,32bpp呈现的很好,很快。

回退选项是使用System.Windows.Media.Imaging.PngBitmapDecoder类。您可以将它传递给BitmapCreateOptions.PreservePixelFormat选项并强制它保留8bpp格式。您可以将png作为资源添加,首先将其重命名为.bin文件,这样它就不会尝试将其解释为图像文件,但会将其设置为字节[]。然后像这样的代码将工作:

using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.IO; 
... 
     Stream stream = new MemoryStream(Properties.Resources.marble8); 
     PngBitmapDecoder decoder = new PngBitmapDecoder(stream, 
      BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
     BitmapSource bitmapSource = decoder.Frames[0]; 

其中“marble8”是我用的测试图像,替代你自己的。您需要添加对WindowsBase和PresentationCore程序集的引用。

+0

这很伤心......恩,非常感谢。 :) – Pedro77 2012-04-02 21:10:23

+0

其实这是错误的; Bitmap类只会将png图像更改为32位(如果它们包含透明度)。我对这种奇怪的行为做了一些研究,并找到了解决方法(https://stackoverflow.com/a/43137699/395685)。 – Nyerguds 2017-11-03 18:01:33

相关问题