C# Image.Save AccessViolationException

C# Image.Save AccessViolationException

我有一个函数,它从像素数组构建图像并将其保存到文件中。调用一次就可以正常工作。但是如果我调用它两次(在它第一次完全执行之后),函数会抛出 AccessViolationException.

private void saveImage(byte[] bmpBytes)
{
    Debug.Print("Image saving started");

    var arrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(bmpBytes, System.Runtime.InteropServices.GCHandleType.Pinned);
    System.Drawing.Image bmp = new System.Drawing.Bitmap(960, 540, 960 * 3, PixelFormat.Format24bppRgb, arrayHandle.AddrOfPinnedObject());
    bmp.Save("img.bmp");

    Debug.Print("Image saving completed");
}

因此,bmp.Save("img.bmp"); 行抛出异常。我也试过在 MemoryStream 中保存数据,但结果相同:第一次调用成功,每隔一次调用 AccessViolationException。可能是什么原因?

更新:

System.Drawing.Image bmp = new System.Drawing.Bitmap(960, 540, 960 * 540 * 3, PixelFormat.Format24bppRgb, arrayHandle.AddrOfPinnedObject());

此错误表明您正在尝试访问受保护的内存。很可能您传递的数组的大小小于您所需的图像。它类似于缓冲区溢出。因此,当 GDI 尝试使用您的图像读取内存时,它超出了为您的进程内存分配的范围,并且会发生故障。顺便说一下,行为是未定义的。例如,如果那块内存已经分配给一个进程,您将读取它拥有的任何内容并且不会收到任何错误。在这种情况下,您很可能会看到带有一些噪点的图像。

您确定要传递有效长度的数组吗?尝试添加相应的检查。并且不要忘记释放分配的内存 - GCHandle must be released with Free when it is no longer needed.

只有 运行 那个代码并且没有错误:

int width = 960;
int height = 540;

void Main()
{
    var arr = Enumerable.Range(0, width * height * 3).Select(i =>
    {
        i = i / 3;
        var y = i / width;
        var x = i - y * width;
        var xd = x / (double)(width - 1);
        var yd = y / (double)(height - 1);
        return (byte)((xd + yd) / 2d * 255);
    }).ToArray();
    saveImage2(arr);
}

private void saveImage2(byte[] bmpBytes)
{
    if (bmpBytes == null)
        throw new ArgumentNullException("bmpBytes");

    if (bmpBytes.Length != width * height * 3)
        throw new ArgumentException("Invalid array length", "bmpBytes");

    var output = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    var rect = new Rectangle(0, 0, width, height);
    var bmpData = output.LockBits(rect, ImageLockMode.WriteOnly, output.PixelFormat);

    // Stride must be taken into account.
    // Actual row length may be greater then required due to GDI's internal memory alignment.
    // It is an error to copy entire array as-is, need to copy row-by-row.
    var rowBytes = width * Image.GetPixelFormatSize(output.PixelFormat) / 8;
    var ptr = bmpData.Scan0;
    for (var i = 0; i < height; i++)
    {
        Marshal.Copy(bmpBytes, i * rowBytes, ptr, rowBytes);
        ptr += bmpData.Stride;
    }

    output.UnlockBits(bmpData);

    output.Save(@"d:\temp\img.bmp");
}

我也遇到过这样的问题,但只有一种方法在这种情况下有效。

[HandleProcessCorruptedStateExceptions]
private void saveImage(byte[] bmpBytes)
{
    try {
     Debug.Print("Image saving started");
     var arrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(bmpBytes, System.Runtime.InteropServices.GCHandleType.Pinned);
     System.Drawing.Image bmp = new System.Drawing.Bitmap(960, 540, 960 * 3, PixelFormat.Format24bppRgb, arrayHandle.AddrOfPinnedObject());
     bmp.Save("img.bmp");
     Debug.Print("Image saving completed");
    } catch ( System.AccessViolationException ex ) {
         // Doing smth
    }

}

通常情况下,您无法捕获 AccessViolationException 异常,因此您需要添加一个 [HandleProcessCorruptedStateExceptions] 属性