第1个回答 推荐于2016-02-18
二楼的方法效率很低的,读一张稍大的图片可能需要十几秒,给你一个效率比较高的方法。
public static short[][] GetPixs(Bitmap bitmap)
{
int height = bitmap.Height;
int width = bitmap.Width;
byte tempB, tempG, tempR;
short[][] spOriginData = new short[height][];
for (int i = 0; i < height; i++)
{
spOriginData[i] = new short[width];
}
BitmapData dataOut = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int offset = dataOut.Stride - dataOut.Width * 3;
try
{
unsafe
{
byte* pOut = (byte*)(dataOut.Scan0.ToPointer());
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
tempB = pOut[0];
tempG = pOut[1];
tempR = pOut[2];
double data=0.31 * tempR + 0.59 * tempG + 0.11 * tempB;
if (data > 255)
spOriginData[y][x] = 255;
else
if (data < 0)
spOriginData[y][x] = 0;
else
spOriginData[y][x] = (short)data;
pOut += 3;
}
pOut += offset;
}
bitmap.UnlockBits(dataOut);
}
}
catch
{
}
return spOriginData;
}
我毕业设计的时候用的本回答被提问者采纳
第2个回答 2009-07-13
Bitmap oldbitmap = new Bitmap(url);
int Height = oldbitmap.Height;
int Width = oldbitmap.Width;
Bitmap newbitmap = new Bitmap(Width, Height);
Color pixel;
for (int x = 0; x < Width-1; x++)
{
for (int y = 0; y < Height-1; y++)
{
int r, g, b;
pixel = oldbitmap.GetPixel(x, y);
r = pixel.R;
g = pixel.G;
b = pixel.B;
}
打印rgb的值就可以了