gpt4 book ai didi

c# - Bitmap.LockBits 中 ImageLockMode 的用途(附代码)

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:51 24 4
gpt4 key购买 nike

Bitmap.LockBits 中 ImageLockMode 的作用是什么?对于只读 documentation只说明

ReadOnly: Specifies that a portion of the image is locked for reading.

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

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

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.pngresult_usePointer_fromFile.png 都包含原始图像,因此没有任何内容被覆盖(也没有抛出异常!)。另外两张图片包含随机噪声,这些噪声是在锁定时写入的。

我没有做进一步的测试来确认并行写入访问的行为,因为无论如何我都不会这样做。

它不是重复的,而是密切相关的: Does Bitmap.LockBits “pin” a bitmap into memory?

最佳答案

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

1)如果多位代码同时请求锁,一次只允许下一个写锁,而读锁可以共享。当然,这取决于获取锁的代码是否恰本地使用它们。

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

关于c# - Bitmap.LockBits 中 ImageLockMode 的用途(附代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43919383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com