为了账号安全,请及时绑定邮箱和手机立即绑定

在C#中转换位图PixelFormats

在C#中转换位图PixelFormats

一只萌萌小番薯 2019-11-02 14:21:59
我需要将位图从转换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。怎么回事/如何转换格式?
查看完整描述

3 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

由于某种原因,如果您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.

}


查看完整回答
反对 回复 2019-11-02
?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

using (var bmp = new Bitmap(width, height, PixelFormat.Format24bppArgb))

using (var g = Graphics.FromImage(bmp)) {

  g.DrawImage(..);

}

应该那样工作。也许您想设置一些参数g以定义质量等的插值模式。


查看完整回答
反对 回复 2019-11-02
  • 3 回答
  • 0 关注
  • 1419 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信