基于位图(Bitmap、BitmapData)的图片处理方法(C#)

时间:2022-01-14 10:31:38

目前操作位图的主流方法有三种:

  1、基于Bitmap像素的处理方法,以GetPixel()和SetPixel()方法为主。方法调用简单,但是效率偏低。

  2、基于内存的像素操作方法,以System.Runtime.InteropServices.Marshal.Copy()方法将数据变为非托管资源,操作后再写入内存。

  3、基于指针的操作方式,效率最高,但是对使用者的能力有要求,能力不够者容易造成内存溢出。

第二种方法的一个实例:

 //大图逐行遍历,y为行索引
for (var y = ; y < destHeight; y++)
{
//小图。把一行数据读入数组。第一个参数是起始位。
System.Runtime.InteropServices.Marshal.Copy(srcScan0 + y * srcStride, srcBuffer, , srcStride);
//大图。
System.Runtime.InteropServices.Marshal.Copy(dstScan0 + y * dstStride, dstBuffer, , dstStride); //大图逐列,rgb三色.(与源码相比做了修改,遍历长度减小,数值因为是灰度值,计算了变为了三分之一。)
for (var x = ; x < destWidth; x ++)
{
//字节总索引
int fullIndex = *x;
//相乘,再除以255。返回一个byte
//dstBuffer[x] = channelProcessFunction(ref srcBuffer[x], ref dstBuffer[x]);
var blendValue = channelProcessFunction(ref srcBuffer[fullIndex], ref dstBuffer[fullIndex]);
dstBuffer[fullIndex + ] = blendValue;
dstBuffer[fullIndex + ] = blendValue;
dstBuffer[fullIndex] = blendValue;
}
//写回托管内存
System.Runtime.InteropServices.Marshal.Copy(dstBuffer, , dstScan0 + y * dstStride, dstStride);
}

基于内存的像素操作

而对于灰度图的处理,可以使用调整gamma值的方式。对于灰度图处理的一个实例:

 /// <summary>
/// 把小图片按照权重,重设gamma值,重新渲染。可得到加权重后的小图,背景为白色,非透明。
/// </summary>
/// <param name="image">小图片</param>
/// <param name="weight">权重</param>
/// <returns>加权重后的小图,背景为白色,非透明</returns>
private static Bitmap ApplyHeatValueToImage(Bitmap image, float weight)
{
//新建临时位图
var tempImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); using (var g = Graphics.FromImage(tempImage))
{
//把权重参数映射为[0.1-5],以便后面进行gamma的矫正。gamma值正常范围为1.0 - 2.2
//此处拓展,可以让色彩更加intense
//gamma值可用来进行灰度值层面的明暗矫正,改善失真。
////I want to make the color more intense (White/bright)
if (weight < 0.02f) weight = 0.02f;//最小0.02
weight *= 5f;
if (weight > 5f) weight = 5f; // Create ImageAttributes
var ia = new ImageAttributes(); //Gamma values range from 0.1 to 5.0 (normally 0.1 to 2.2), with 0.1 being the brightest and 5.0 the darkest.
//Convert the 100% to a range of 0.1-5 by multiplying it by 5
ia.SetGamma(weight, ColorAdjustType.Bitmap); //在image中 重绘
// Draw Image with the attributes
g.DrawImage(image,
new Rectangle(, , image.Width, image.Height),//这里如果size设小一点,可以对目标图像进行缩放。如果小于Graphics的尺寸,则会出现白边
, , image.Width, image.Height,//这里可以对源图像进行裁剪。
GraphicsUnit.Pixel, ia);
}
//New dot with a different intensity
return tempImage;
}