假设我有一个位图图像,是否可以遍历图像中的所有单个字节?如果是,如何?
4 回答
我发现了这个:http ://channel9.msdn.com/forums/TechOff/108813-Bitmap-to-byte-array/
说您可以使用 Memorystream 和 .Save 方法,它看起来像这样:
System.Drawing.Bitmap bmp = GetTheBitmap();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
If you need to access the pixel information, the super-slow but super-easy way is to call the GetPixel and SetPixel methods on your Bitmap object.
The super-fast and not-that-hard way is to call the Bitmap's LockBits method and use the BitmapData object returned from it to read and write the Bitmap's byte data directly. You can do this latter part with the Marshal class as in Ilya's example, or you can skip the Marshal overhead like this:
BitmapData data;
int x = 0; //or whatever
int y = 0;
unsafe
{
byte* row = (byte*)data.Scan0 + (y * data.Stride);
int columnOffset = x * 4;
byte B = row[columnOffset];
byte G = row[columnOffset + 1];
byte R = row[columnOffset + 2];
byte A = row[columnOffset + 3];
}
使用 Bitmap 类的 LockBits 成员获取 BitmapData,然后使用 Scan0 和 Marshal.ReadByte 读取字节。这是一个小例子(虽然它不是关于正确的亮度调整):
public static void AdjustBrightness(Bitmap image, int brightness)
{
int offset = 0;
brightness = (brightness * 255) / 100;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
IntPtr Scan0 = bmData.Scan0;
int nVal = 0;
int nOffset = stride - image.Width * 3;
int nWidth = image.Width * 3;
for (int y = 0; y < image.Height; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
nVal = Marshal.ReadByte(Scan0, offset) + brightness;
if (nVal < 0)
nVal = 0;
if (nVal > 255)
nVal = 255;
Marshal.WriteByte(Scan0, offset, (byte)nVal);
++offset;
}
offset += nOffset;
}
image.UnlockBits(bmData);
}
另一种解决方案是使用 LockBits 和 Marshal.Copy 将您的位图转换为数组。我需要这个解决方案,因为我有两张仅在颜色深度上有所不同的图像,而其他提供的解决方案不能很好地处理(或者太慢)。
using (Bitmap bmp = new Bitmap(fname)) {
// Convert image to int32 array with each int being one pixel
int cnt = bmp.Width * bmp.Height * 4 / 4;
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32[] rgbValues = new Int32[cnt];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(bmData.Scan0, rgbValues, 0, cnt);
bmp.UnlockBits(bmData);
for (int i = 0; i < cnt; ++i) {
if (rgbValues[i] == 0xFFFF0000)
Console.WriteLine ("Red byte");
}
}