Bitmap.LockBits中ImageLockMode的用途(带代码)

时间:2017-05-11 15:13:37

标签: c# .net image-processing bitmap unsafe

Bitmap.LockBits中ImageLockMode的用途是什么? 对于ReadOnly,documentation仅表示

  

ReadOnly:指定图像的一部分被锁定以供阅读。

但是下面的代码证明,这不是真的。 我知道之前已经问过这个问题,这次我尝试使用一些实际代码,因为我在其他地方找不到答案。

如果我运行以下代码,它的行为与答案中的解释完全相同。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace LockBits_Trials
{
   class Program
   {
      static readonly Random rnd = new Random(42);
      static void Main(string[] args)
      {
         Bitmap bmp_fromFile = new Bitmap("example.png");
         Bitmap bmp_fromCtor = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
         marshalCopy(bmp_fromFile, "result_marshalCopy_fromFile.png");
         marshalCopy(bmp_fromCtor, "result_marshalCopy_fromCtor.png");
         usePointer(bmp_fromFile, "result_usePointer_fromFile.png");
         usePointer(bmp_fromCtor, "result_usePointer_fromCtor.png");
      }

      private static unsafe void usePointer(Bitmap bmp, string filename)
      {
         ImageLockMode mode = ImageLockMode.ReadOnly;
         //code from turgay at http://csharpexamples.com/fast-image-processing-c/ 
         if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
            throw new Exception();
         BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), mode, bmp.PixelFormat);
         int bytesPerPixel = 3; int heightInPixels = bitmapData.Height; int widthInBytes = bitmapData.Width * bytesPerPixel;
         byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
         for (int y = 0; y < heightInPixels; y++) {
            byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
            for (int x = 0; x < widthInBytes; x = x + bytesPerPixel) {
               currentLine[x] = (byte)rnd.Next(0, 255);
               currentLine[x + 1] = (byte)rnd.Next(0, 255);
               currentLine[x + 2] = (byte)rnd.Next(0, 255);
            }
         }
         bmp.UnlockBits(bitmapData);
         bmp.Save(filename, ImageFormat.Png);
      }

      private static unsafe void marshalCopy(Bitmap bmp, string filename)
      {
         ImageLockMode mode = ImageLockMode.ReadOnly;
         if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
            throw new Exception();
         BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), mode, bmp.PixelFormat);
         IntPtr ptrFirstPixel = bitmapData.Scan0;
         int totalBytes = bitmapData.Stride * bitmapData.Height;
         byte[] newData = new byte[totalBytes];
         for (int i = 0; i < totalBytes; i++)
            newData[i] = (byte)rnd.Next(0, 255);
         Marshal.Copy(newData, 0, ptrFirstPixel, newData.Length);
         bmp.UnlockBits(bitmapData);
         bmp.Save(filename, ImageFormat.Png);
      }
   }
}

图片 result_marshalCopy_fromFile.png result_usePointer_fromFile.png 都包含原始图片,因此没有任何内容被覆盖(并且没有抛出异常!)。 另外两张图片包含随机噪音,这些噪音是在被锁定时写入的。

我没有做进一步的测试来确认并行写访问的行为,因为我不这样做。

这不是重复,但强烈相关: Does Bitmap.LockBits “pin” a bitmap into memory?

1 个答案:

答案 0 :(得分:1)

正如您所观察到的,一旦您获得了对原始数据指针的访问权限,无论您请求的锁定类型如何,都无法阻止您写入该内存。锁定类型仅在两种情况下非常有用:

1)如果多位代码请求同时锁定,则一次只允许发出一个写锁定,而可以共享读锁定。当然,这取决于使用它们正确获取锁的代码。

2)并非所有锁都由Bitmap内存直接支持。在您的示例中,这是因为您创建了一个内存Bitmap,然后以相同的像素格式请求锁定。然而,位图可以表示其他GDI +对象,例如设备上下文拥有的像素。此外,如果您请求的源像素格式不同,则可能必须进行转换。在任何一种情况下,当请求读取锁定时,GDI +可能必须从真实源中提取位图的副本,并以您请求的像素格式将其提供给您。如果您要修改该副本,则不会将其写回源上下文。如果您请求写锁定,GDI +会在释放锁定后将像素复制回源。

相关问题