C# 在.NET中读取/保存PixelFormat.Format48bppRgb PNG位图?
我已经能够使用以下C#代码创建Format48bppRgb.PNG文件(来自一些内部HDR数据): ImageMagik(和其他应用程序)验证这确实是一个16bpp的图像:C# 在.NET中读取/保存PixelFormat.Format48bppRgb PNG位图?,c#,.net,png,gdi+,system.drawing.imaging,C#,.net,Png,Gdi+,System.drawing.imaging,我已经能够使用以下C#代码创建Format48bppRgb.PNG文件(来自一些内部HDR数据): ImageMagik(和其他应用程序)验证这确实是一个16bpp的图像: C:\temp>identify 48bpp.png 48bpp.png PNG 1022x1125 1022x1125+0+0 DirectClass 16-bit 900.963kb 然而,我很失望地发现,在读回PNG时,它已被转换为Format32bppRgb,使用时: Bitmap bmp = new Bit
C:\temp>identify 48bpp.png
48bpp.png PNG 1022x1125 1022x1125+0+0 DirectClass 16-bit 900.963kb
然而,我很失望地发现,在读回PNG时,它已被转换为Format32bppRgb,使用时:
Bitmap bmp = new Bitmap( "c:/temp/48bpp.png", false );
String info = String.Format("PixelFormat: {0}", bmp.PixelFormat );
...
鉴于PNG编解码器可以编写格式为48BPPRGB的文件,有没有任何方法可以在不进行转换的情况下使用.NET将其读入?我不介意它是否为DrawImage调用执行此操作,但我希望访问解压缩的原始数据以进行一些直方图/图像处理工作。仅供参考-我确实找到了一个使用System.Windows.Media.Imaging的.NET解决方案(我一直严格使用WinForms/GDI+,这需要添加WPF程序集,但可以使用),我的格式为64bpparGB像素格式,因此不会丢失信息:
using System.Windows.Media.Imaging; // Add PresentationCore, WindowsBase, System.Xaml
...
// Open a Stream and decode a PNG image
Stream imageStreamSource = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
// Convert WPF BitmapSource to GDI+ Bitmap
Bitmap bmp = _bitmapFromSource(bitmapSource);
String info = String.Format("PixelFormat: {0}", bmp.PixelFormat );
MessageBox.Show(info);
此代码段来自:
如果有人知道一种不需要WPF的方法,请分享 使用Image.FromFile(字符串,布尔值)
或Bitmap.FromFile(字符串,布尔值)
并将布尔值设置为true。所有图像属性都将保存在新图像中
这里字符串是带有完整路径的文件名
如果图像已加载到程序中,并且要使用它创建新位图,也可以使用
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Bmp); // img is any Image, previously opened or came as a parameter
Bitmap bmp = (Bitmap)Bitmap.FromStream(ms,true);
常见的选择是
Bitmap bmp = new Bitmap(img); // this won't preserve img.PixelFormat
我不相信您的初始代码段在这里创建了每个通道16位的文件;我的结果产生一个32bpp的PNG,四个通道的8位数据。令人失望的是,我希望它能对我起作用。我遵循一个相关问题的提示,成功地编写了16位单通道位图.img.Save(ms,ImageFormat.Bmp);不保留像素格式
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Bmp); // img is any Image, previously opened or came as a parameter
Bitmap bmp = (Bitmap)Bitmap.FromStream(ms,true);
Bitmap bmp = new Bitmap(img); // this won't preserve img.PixelFormat