作者:啊沙发的非飞 | 来源:互联网 | 2023-05-17 21:16
I have some c# code that works fine on Vista & Windows 7 but throws a GDI+ Error on Windows XP (with service pack 3 installed).
我有一些c#代码在Vista和Windows 7上运行良好,但在Windows XP上抛出GDI+错误(安装了service pack 3)。
Error thrown on XP
错误扔在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)
System.Runtime.INteropServices.ExternalException(0x80004005): GDI+在system . draw. graphics . checkerrorstatus (Int32Status)中出现的一个通用错误。DrawImage(Image Image, Int32 x, Int32 y) At System.Drawing.Graphics。在mysolution.Core.ImageTools中,drawimageunscale(图像图像,Int32 x,Int 32 y)。ConvertToBitonal(位图原创,Int32阈值)
Code breaks on this line:
代码在这一行上中断:
using (var g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0); // Error Is Thrown Here
}
WpFAppTestingConvertToBitonalCS
WpFAppTestingConvertToBitonalCS
....
....
Below is the full function I'm calling.
下面是我调用的完整函数。
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 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;
}
1 个解决方案