gpt4 book ai didi

c# - 在 .NET 中读取/保存 PixelFormat.Format48bppRgb PNG 位图?

转载 作者:行者123 更新时间:2023-11-30 20:08:15 28 4
gpt4 key购买 nike

我已经能够使用以下 C# 代码创建 Format48bppRgb .PNG 文件(来自一些内部 HDR 数据):

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 图像:

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 编解码器可以编写 Format48bppRgb,有没有什么方法可以使用 .NET 在不进行转换的情况下读取它?我不介意它是否为 DrawImage 调用执行此操作,但我想访问解压缩的原始数据以进行某些直方图/图像处理工作。

最佳答案

仅供引用 - 我确实找到了一个使用 System.Windows.Media.Imaging 的 .NET 解决方案(我一直严格使用 WinForms/GDI+ - 这需要添加 WPF 程序集,但有效。)有了这个,我得到了 Format64bppArgb PixelFormat ,所以没有丢失信息:

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);

...

此代码片段来自: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 的方法,请分享!

关于c# - 在 .NET 中读取/保存 PixelFormat.Format48bppRgb PNG 位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7276212/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com