[英]How to load a 16bit (rgb565) bmp into a byte array with c# standard
所以我有这段代码,它在我的 windows c# 应用程序或框架 DLL 中工作得很好。
private static Icon? LoadSync(string filepath)
{
Stream imageStreamSource = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
BmpBitmapDecoder decoder = new BmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
if (bitmapSource.Format != System.Windows.Media.PixelFormats.Bgr565)
{
Debug.Print($"Image at path `{filepath}` is not RGB565!");
return null;
}
byte[] icon = new byte[bitmapSource.PixelHeight * bitmapSource.PixelWidth * sizeof(short)];
bitmapSource.CopyPixels(icon, bitmapSource.PixelWidth * sizeof(short), 0);
return new Icon() { Width = (ushort)bitmapSource.PixelWidth, Height = (ushort)bitmapSource.PixelHeight, Data = icon };
}
它将一个 16 位的 .bmp 文件加载到内存中,然后以字节数组的形式将像素提供给我。
但是,我遇到了一个奇怪的编译错误,我的在线搜索似乎发现它是由一个函数在一个实例中如何声明为扩展方法而在另一个实例中声明为实际成员的一些奇怪差异引起的。 我忘记了细节,但建议的解决方案是从框架切换到标准 dll。 我试过了,它解决了我的问题。
但是现在,重新引入上面的代码,我不再可以访问System.Windows.Media.Imaging
。 所以我想知道我必须有哪些选择才能完成同样的事情。
使用普通的旧位图库,数据被转换为(我认为)rgb888,没有任何保留格式的选项。
如果您想知道为什么我需要一个字节数组,我会将数据发送到带有屏幕的 arduino。
我从来没有完全按照你的意思去做,但我最近确实需要在一些 C# 代码中处理图像。 我添加了 System.Drawing NuGet 包并使用了 Image 类。 这可能会有所帮助: https : //docs.microsoft.com/en-us/dotnet/api/system.drawing.image?view=windowsdesktop-5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.