发现了msdn的一处错误

upc_xiaowei 2012-10-09 04:30:54
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);

http://msdn.microsoft.com/zh-cn/library/5ey6h79d.aspx
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2012-10-09
  • 打赏
  • 举报
回复
这只是个demo而已,而且你说的不是3的倍数的情况不是他们考虑的范围内。

不过你这种精神很不错,赞一个!
bdmh 2012-10-09
  • 打赏
  • 举报
回复
折算什么bug,这只是个示例而已,没必要考虑那么仔细,具体的细节是你编程时要考虑的
threenewbee 2012-10-09
  • 打赏
  • 举报
回复
最下面不是有个“社区内容”嘛,你可以反馈下。
upc_xiaowei 2012-10-09
  • 打赏
  • 举报
回复
最下面那个for循环错了,没有考虑到stride和每行的字节数可能不一样。如果stride不是3的倍数,就会出错的啊

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧