gpt4 book ai didi

c# - GDI+ 错误仅发生在 Windows XP 上

转载 作者:太空狗 更新时间:2023-10-29 23:46:07 25 4
gpt4 key购买 nike

我有一些 c# 代码在 Vista 和 Windows 7 上运行良好,但在 Windows XP(安装了 Service Pack 3)上抛出 GDI+ 错误。

在 XP 上抛出错误

System.Runtime.INteropServices.ExternalException(0x80004005): A generic error occured in GDI+ at System.Drawing.Graphics.CheckErrorStatus(Int32Status) at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y) At System.Drawing.Graphics.DrawImageUnscaled(Image image, Int32 x,Int 32 y) at mysolution.Core.ImageTools.ConvertToBitonal(Bitmap orginal, Int32 threshold)

这一行的代码中断:

using (var g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0); // Error Is Thrown Here
}

WpFAppTestingConvertToBitonalCS

....

下面是我调用的完整函数。

    public static Bitmap ConvertToBitonal(Bitmap original, int threshold)
{
Bitmap source;

// If original bitmap is not already in 32 BPP, ARGB format, then convert
if (original.PixelFormat != PixelFormat.Format32bppArgb)
{
source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);

using (var g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0);
}
}
else
{
source = original;
}

// Lock source bitmap in memory
var sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

// Copy image data to binary array
var imageSize = sourceData.Stride * sourceData.Height;
var sourceBuffer = new byte[imageSize];
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);

// Unlock source bitmap
source.UnlockBits(sourceData);

// Create destination bitmap
var destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
destination.SetResolution(original.HorizontalResolution, original.VerticalResolution);

// Lock destination bitmap in memory
var destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

// Create destination buffer
imageSize = destinationData.Stride * destinationData.Height;
var destinationBuffer = new byte[imageSize];

var sourceIndex = 0;
var destinationIndex = 0;
var pixelTotal = 0;
byte destinationValue = 0;
var pixelValue = 128;
var height = source.Height;
var width = source.Width;

// Iterate lines
for (var y = 0; y < height; y++)
{
sourceIndex = y * sourceData.Stride;
destinationIndex = y * destinationData.Stride;
destinationValue = 0;
pixelValue = 128;

// Iterate pixels
for (var x = 0; x < width; x++)
{
// Compute pixel brightness (i.e. total of Red, Green, and Blue values) - Thanks murx
// B G R
pixelTotal = sourceBuffer[sourceIndex] + sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2];
if (pixelTotal > threshold)
{
destinationValue += (byte)pixelValue;
}
if (pixelValue == 1)
{
destinationBuffer[destinationIndex] = destinationValue;
destinationIndex++;
destinationValue = 0;
pixelValue = 128;
}
else
{
pixelValue >>= 1;
}
sourceIndex += 4;
}

if (pixelValue != 128)
{
destinationBuffer[destinationIndex] = destinationValue;
}
}

// Copy binary image data to destination bitmap
Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);

// Unlock destination bitmap
destination.UnlockBits(destinationData);

// Dispose of source if not originally supplied bitmap
if (source != original)
{
source.Dispose();
}

// Return
return destination;
}

最佳答案

我讨厌 GDI+ 异常处理,它触发的唯一错误是 GDI+ 中发生一般错误,试试这个

Bitmap source = new Bitmap(0, 0);  
using (Graphics g = Graphics.FromImage(source))
{ g.Clear(Color.White);
g.Graphics.DrawImageUnscaled(your original source,0,0);
}

关于c# - GDI+ 错误仅发生在 Windows XP 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20149007/

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