位图和位图数据的区别

Difference between bitmap and bitmapdata

C#中的System.Drawing.bitmapSystem.Drawing.Imaging.bitmapdata有什么区别?
如何相互转换?

将位图转换为位图数据。另请参阅此 link

Private void LockUnlockBitsExample(PaintEventArgs e) { 
// Create a new bitmap. 
Bitmap bmp = new Bitmap("c:\fakePhoto.jpg");
 // Lock the bitmap's bits.
 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
 System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); 
// Get the address of the first line.

 IntPtr ptr = bmpData.Scan0; 
// Declare an array to hold the bytes of the bitmap.  
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;

byte[] rgbValues = new byte[bytes];
 // Copy the RGB values into the array.
 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); 
// Set every third value to 255. A 24bpp bitmap will look red.  
 for (int counter = 2; counter < rgbValues.Length; counter += 3) rgbValues[counter] = 255;
 // Copy the RGB values back to the bitmap
 System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
 // Unlock the bits.
 bmp.UnlockBits(bmpData); 
// Draw the modified image.
 e.Graphics.DrawImage(bmp, 0, 150); 
} 

System.Drawing.Bitmap 是一个实际的位图对象。你可以用它来绘制到使用从它获得的Graphics实例,你可以在屏幕上显示它,你可以将数据保存到文件等

System.Drawing.Imaging.BitmapDataclass是调用Bitmap.LockBits()方法时使用的辅助对象。它包含有关锁定位图的信息,您可以使用它来检查位图中的像素数据。

您不能真正 "convert" 区分两者本身,因为它们不代表相同的信息。您可以通过调用 LockBits()Bitmap 对象 获得 一个 BitmapData 对象。如果你有一个来自其他 Bitmap 对象的 BitmapData 对象,你可以通过分配一个与原始对象相同格式的对象,调用 LockBits 也在那个上面,然后只是将字节从一个复制到另一个。