C#由像素自成图像解决方法
C#由像素自成图像
我现在得到了一幅8位灰度图像处理后的像素,以及此图像的长和宽,请问如何用C#由这些数据生成一幅8位灰度图像呢?要是知道一幅彩色图像的RGB又应该如何生成图像呢??
------解决方案--------------------
new一副那么大小的灰度bitmap,然后以原始格式LockBits,然后按照位图的要求将你的数据填充到Lock到的内存中,在unlockbis。
------解决方案--------------------
private static Bitmap PImage(byte[]src,int w,int h)
{
//构建与原图像大小一样的模版图像
Bitmap dstBitmap = new Bitmap(w, h, 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* pOut = (byte*)dstData.Scan0.ToPointer();
int stride = dstData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pOut[0] = (byte)src[i*3+j*h];//b
pOut[1] = (byte)src[i*3+1+j*h];//g
pOut[2] = (byte)src[i*3+2+j*h];//r
pOut += 3;
}
pOut += srcData.Stride - w * 3;
}
dstBitmap.UnlockBits(dstData);
我现在得到了一幅8位灰度图像处理后的像素,以及此图像的长和宽,请问如何用C#由这些数据生成一幅8位灰度图像呢?要是知道一幅彩色图像的RGB又应该如何生成图像呢??
c#
图像处理
------解决方案--------------------
new一副那么大小的灰度bitmap,然后以原始格式LockBits,然后按照位图的要求将你的数据填充到Lock到的内存中,在unlockbis。
------解决方案--------------------
private static Bitmap PImage(byte[]src,int w,int h)
{
//构建与原图像大小一样的模版图像
Bitmap dstBitmap = new Bitmap(w, h, 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* pOut = (byte*)dstData.Scan0.ToPointer();
int stride = dstData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pOut[0] = (byte)src[i*3+j*h];//b
pOut[1] = (byte)src[i*3+1+j*h];//g
pOut[2] = (byte)src[i*3+2+j*h];//r
pOut += 3;
}
pOut += srcData.Stride - w * 3;
}
dstBitmap.UnlockBits(dstData);