Bitmap和byte[]的相互转换///<summary>
///将Bitmap转换为字节数组
///</summary>
///<param name="width">图像宽度</param>
///<param name="height">图像长度</param>
///<param name="channel">图像通道</param>
///<param name="img">原图像</param>
///<returns></returns>
public static byte[] getByteStreamFromBitmap(int width, int height, int channel, Bitmap img)
{
byte[] bytes = new byte[width * height * channel];
BitmapData im = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, img.PixelFormat);
int stride = im.Stride;
int offset = stride - width * channel;
int length = stride * height;
byte[] temp = new byte[stride * height];
Marshal.Copy(im.Scan0, temp, 0, temp.Length);
img.UnlockBits(im);
int posreal = 0;
int posscan = 0;
for (int c = 0; c < height; c++)
{
for (int d = 0; d < width * channel; d++)
{
bytes[posreal++] = temp[posscan++];
}
posscan += offset;
}
return bytes;
}
///<summary>
///将⼀个字节数组转换为8bit灰度位图
///</summary>
/
//<param name="rawValues">显⽰字节数组</param>
///<param name="width">图像宽度</param>
///<param name="height">图像⾼度</param>param name
///<returns>位图</returns>
public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)
{
//申请⽬标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
//获取图像参数
int stride = bmpData.Stride; // 扫描线的宽度
int offset = stride - width; // 显⽰宽度与扫描线宽度的间隙
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置
int scanBytes = stride * height;// ⽤stride宽度,表⽰这是内存区域的⼤⼩
//下⾯把原始的显⽰⼤⼩字节数组转换为内存中实际存放的字节数组
int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和⽬标数组
byte[] pixelValues = new byte[scanBytes]; //为⽬标数组分配内存
for (int x = 0; x < height; x++)
{
//下⾯的循环节是模拟⾏扫描
for (int y = 0; y < width; y++)
{
pixelValues[posScan++] = rawValues[posReal++];
}
posScan += offset; //⾏扫描结束,要将⽬标位置指针移过那段“间隙”
}
//⽤Marshal的Copy⽅法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
bmp.UnlockBits(bmpData); // 解锁内存区域
//下⾯的代码是为了修改⽣成位图的索引表,从伪彩修改为灰度
ColorPalette tempPalette;
using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
tempPalette = tempBmp.Palette;
}
for (int i = 0; i < 256; i++)
{
tempPalette.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = tempPalette;
//算法到此结束,返回结果
return bmp;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论