在C#中转换位图PixelFormats

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


我希望使用Bitmap.Clone,但似乎无法正常工作。


Bitmap orig = new Bitmap("orig.bmp");

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

如果我运行上面的代码,然后检查clone.PixelFormat,它将设置为PixelFormat.Format32bppRgb。怎么回事/如何转换格式?


一只萌萌小番薯
浏览 1422回答 3
3回答

慕后森

由于某种原因,如果您Bitmap从文件路径(即)创建一个Bitmap bmp = new Bitmap("myimage.jpg");并对其进行调用Clone(),则返回的内容Bitmap将不会转换。但是,如果您Bitmap从旧版本创建另一个版本Bitmap,Clone()则会按预期工作。尝试这样的事情:using (Bitmap oldBmp = new Bitmap("myimage.jpg"))using (Bitmap newBmp = new Bitmap(oldBmp))using (Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format32bppArgb)){    // targetBmp is now in the desired format.}

qq_遁去的一_1

using (var bmp = new Bitmap(width, height, PixelFormat.Format24bppArgb))using (var g = Graphics.FromImage(bmp)) {  g.DrawImage(..);}应该那样工作。也许您想设置一些参数g以定义质量等的插值模式。
打开App,查看更多内容
随时随地看视频慕课网APP