从 RGB 格式的文件加载位图(无 Alpha)
我只想加载 .BMP 文件并获取 24 位 RGB 格式(或 32 位 RGB 格式)的位图对象。
我尝试的所有方法都返回 PixelFormat = Format32bppArgb 的位图/图像对象。当然,即使 BMP 没有 alpha。
new Bitmap(System.Drawing.Image.FromFile(fileName, true));
new Bitmap(fileName);
目前,我通过将第一个对象复制到 24 位 RBG 内存位图中的另一个对象来解决该问题。
有没有一种方法可以做到这一点?
谢谢
i simply want to load a .BMP file and get the Bitmap object in 24bit RGB format (or 32bit in RGB format).
All methods I tried return a Bitmap/Image object with PixelFormat = Format32bppArgb. Even if of course BMPs don't have alpha.
new Bitmap(System.Drawing.Image.FromFile(fileName, true));
new Bitmap(fileName);
I currently solve the problem by copying the first object to another in memory bitmap at 24bit RBG.
Is there a single method to do it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,不可能使用 System.Drawing 中的类指定用于加载位图的 PixelFormat。要转换位图,请检查此问题:Converting Bitmap PixelFormats in C#
目前这是最重要的答案是:
一种选择是将其放入一个采用文件名和 PixelFormat 的函数中。这隐藏了丑陋的代码,这些代码有其优点和缺点,因为它也隐藏了它可能效率不高的事实。
根据链接的SO问题,使用克隆方法并不总是有效。
As far as I can tell it is not possible to specify the PixelFormat for loading bitmaps using the classes in System.Drawing. To convert the bitmap check this question: Converting Bitmap PixelFormats in C#
This is currently the top answer there:
One option would be to put that in a function which takes a filename and PixelFormat. That hides the ugly code, which has its ups and downs since it also hides the fact it is probably not that efficient.
According to the linked SO question using the Clone method not always works.
您可以将其克隆为 RGB 格式:
You can clone it to a RGB format one:
将第一个对象复制到另一个到底是什么意思?
要实现您想要做的事情,即加载图像并将其转换为 24 位,只需获取与大小匹配的第二个位图的图形上下文,即 RGB,并将原始位图绘制到这个。
Exactly what do you mean by copying the first object to another?
To achieve what you want to do, meaning loading an image and converting it to 24 bit, just get the graphics context of a second bitmap that matches the size and that is RGB, and paint the original bitmap onto this one.