问题描述
我用下面的C#code之所以能够创造一个Format48bppRgb .png文件(从一些内部HDR数据):
I've been able to create a Format48bppRgb .PNG file (from some internal HDR data) using the the following C# code:
Bitmap bmp16 = new Bitmap(_viewer.Width, _viewer.Height, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
System.Drawing.Imaging.BitmapData data16 = bmp16.LockBits(_viewer.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp16.PixelFormat);
unsafe { (populates bmp16) }
bmp16.Save( "C:/temp/48bpp.png", System.Drawing.Imaging.ImageFormat.Png );
ImageMagik(和其他应用程序)验证这确实是一个16bpp的图像:
ImageMagik (and other apps) verify that this is indeed a 16bpp image:
C:\temp>identify 48bpp.png
48bpp.png PNG 1022x1125 1022x1125+0+0 DirectClass 16-bit 900.963kb
我很失望,但是,找到阅读的PNG回来,它已转换为Format32bppRgb,使用时:
I was disappointed, however, to find that on reading the PNG back in, it had been converted to Format32bppRgb, when using:
Bitmap bmp = new Bitmap( "c:/temp/48bpp.png", false );
String info = String.Format("PixelFormat: {0}", bmp.PixelFormat );
...
鉴于PNG codeC可以写一个Format48bppRgb,有什么办法,我可以使用.NET来读取它没有转换?如果它这样做了DrawImage的电话,我不介意,但我想访问DECOM pressed,原始数据的一些直方图/图像处理工作。
Given that the PNG codec can write a Format48bppRgb, is there any way I can use .NET to read it in without the conversion? I don't mind if it does this for a DrawImage call, but I would like access to the decompressed, original data for some histogram/image processing work.
推荐答案
仅供参考 - 我没有找到一个.NET解决方案,这一点使用System.Windows.Media.Imaging(我一直在使用严格的WinForms / GDI + - 这需要加入WPF 。组件,但作品)有了这个,我得到一个Format64bppArgb的PixelFormat,所以没有丢失的信息:
FYI - I did find a .NET solution to this using System.Windows.Media.Imaging (I had been using strictly WinForms/GDI+ - this requires adding WPF assemblies, but works.) With this, I get a Format64bppArgb PixelFormat, so no lost information:
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);
...
而从这个code段:的
And this code snippet from: http://www.generoso.info/blog/wpf-system.drawing.bitmap-to-bitmapsource-and-viceversa.html
private System.Drawing.Bitmap _bitmapFromSource(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
// from System.Media.BitmapImage to System.Drawing.Bitmap
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
如果任何人都知道的一种方式来做到这一点并不需要WPF,欢迎交流!
If anyone has knows of a way to do this that doesn't require WPF, please share!
这篇关于读/ $ P $在.NET pserving一个PixelFormat.Format48bppRgb PNG位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!