C# 图片处理心得

本文介绍了在Windows Form开发中进行图片处理的经验,详细阐述了如何将32bpp、24bpp转换为16bpp、1bpp、4bpp和8bpp的过程,涉及到色彩匹配和色板操作。
摘要由CSDN通过智能技术生成

做Windows form软件开发的时候碰到了图片处理。分享一些图片转换的方法。

32bpp转24bpp

public static public static Bitmap Convert32bppTo24bpp(Bitmap bmpSource)
{
	Bitmap bmpDest = null;
	//When 32 bpp bitmap, convert to 24 bpp bitmap
	if (bmpSource != null &&
	   (PixelFormat.Format32bppArgb == bmpSource.PixelFormat ||
	   PixelFormat.Format32bppPArgb == bmpSource.PixelFormat ||
	   PixelFormat.Format32bppRgb == bmpSource.PixelFormat))
	{
		//Convert to 24-bit Bitmap                            
		bmpDest = new Bitmap(bmpSource.Width, bmpSource.Height, PixelFormat.Format24bppRgb);
		bmpDest.SetResolution(bmpSource.HorizontalResolution, bmpSource.VerticalResolution);
		using (Graphics g = Graphics.FromImage(bmpDest))
		{
			//Use high quality graphics to draw
			g.SmoothingMode = SmoothingMode.HighSpeed;
			g.CompositingQuality = CompositingQuality.HighSpeed;
			g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
			g.InterpolationMode = InterpolationMode.NearestNeighbor;
			g.DrawImage(bmpSource, 0, 0);
		}
	}
	return bmpDest;
}

同样,32bpp、24bpp转16bpp

public static Bitmap ConvertTo16bppRgb565(Bitmap img24or32Bitmap)
{
	//Create Format16bppRgb565 Bitmap
	var bitmap16bit = new Bitmap(img24or32Bitmap.Width, img24or32Bitmap.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
	//Keep same resolution format
	bitmap16bit.SetResolution(img24or32Bitmap.HorizontalResolution, img24or32Bitmap.VerticalResolution);
	using (var g = Graphics.FromImage(bitmap16bit))
	{
		//Use high quality graphics to draw
		g.SmoothingMode = SmoothingMode.Hi
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值