kbr*_*ton 10
无论图像格式如何,SetPixel()都是非常慢的.我从来没有在实践中使用它.
您可以使用LockBits方法更快地设置像素,这允许您快速将托管数据封送到非托管位图字节.
这是一个如何看起来的例子:
Bitmap bitmap = // ...
// Lock the unmanaged bits for efficient writing.
var data = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
bitmap.PixelFormat);
// Bulk copy pixel data from a byte array:
Marshal.Copy(byteArray, 0, data.Scan0, byteArray.Length);
// Or, for one pixel at a time:
Marshal.WriteInt16(data.Scan0, offsetInBytes, shortValue);
// When finished, unlock the unmanaged bits
bitmap.UnlockBits(data);
Run Code Online (Sandbox Code Playgroud)
请注意,GDI +似乎不支持16bpp灰度,这意味着.NET无法将16bpp灰度位图保存到文件或流中.见http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab