在 C# 中转换位图像素格式 [英] Converting Bitmap PixelFormats in C#

查看:50
本文介绍了在 C# 中转换位图像素格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将位图从 PixelFormat.Format32bppRgb 转换为 PixelFormat.Format32bppArgb.

I need to convert a Bitmap from PixelFormat.Format32bppRgb to PixelFormat.Format32bppArgb.

我希望使用 Bitmap.Clone,但它似乎不起作用.

I was hoping to use Bitmap.Clone, but it does not seem to be working.

Bitmap orig = new Bitmap("orig.bmp");
Bitmap clone = orig.Clone(new Rectangle(0,0,orig.Width,orig.Height), PixelFormat.Format24bppArgb);

如果我运行上面的代码然后检查 clone.PixelFormat 它被设置为 PixelFormat.Format32bppRgb.发生了什么/如何转换格式?

If I run the above code and then check clone.PixelFormat it is set to PixelFormat.Format32bppRgb. What is going on/how do I convert the format?

推荐答案

草率,GDI+ 并不少见.这修复了它:

Sloppy, not uncommon for GDI+. This fixes it:

Bitmap orig = new Bitmap(@"c:	emp24bpp.bmp");
Bitmap clone = new Bitmap(orig.Width, orig.Height,
    System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

using (Graphics gr = Graphics.FromImage(clone)) {
    gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
}

// Dispose orig as necessary...

这篇关于在 C# 中转换位图像素格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆