问题描述
- C# 将byte[]转化为灰度图片
-
想要将存在byte[]信号强度数据转换成灰度图片,现在用下面这种方法,总是出现内存操作
错误,请大牛来为我解惑~~不胜感谢!
public static Bitmap ToGrayBitmap(Int16[] rawValues, int width, int height)
{
//// 申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height,PixelFormat.Format16bppRgb565);
Rectangle newbmp = new Rectangle(0, 0, width, height);
BitmapData bmpData = bmp.LockBits((newbmp),ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);
//// 获取图像参数
int stride = bmpData.Stride; // 扫描线的宽度
int offset = stride - width; // 显示宽度与扫描线宽度的间隙int scanBytes = stride * height; // 用stride宽度,表示这是内存区域的大小
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置//// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组 int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组 Int16[] pixelValues = new Int16[scanBytes]; //为目标数组分配内存 for (int x = 0; x < height; x++) { //// 下面的循环节是模拟行扫描 for (int y = 0; y < width; y++) { pixelValues[posScan++] = rawValues[posReal++]; } posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙” } //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中 Marshal.Copy(pixelValues, 0, iptr, scanBytes); bmp.UnlockBits(bmpData); return bmp; }
解决方案
BitmapData.stride
是字节数,你怎么用在 Int16
的计数上了?
牛头不对马嘴!
解决方案二:
private static Bitmap PGray(Bitmap src)
{
int w = src.Width;
int h = src.Height;
//构建与原图像大小一样的模版图像
Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//将原图像存入内存
System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
//调用图像灰度化公式
pOut[0] = pOut[1] = pOut[2] = (byte)(b * 0.114 + g * 0.587 + r * 0.299);
pIn += 3;
pOut += 3;
}
pIn += srcData.Stride - w * 3;
pOut += srcData.Stride - w * 3;
}
src.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
}
直接使用!
时间: 2024-10-17 23:38:00