热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GDI+错误只出现在windowsXP上-GDI+ErroronlyoccuringonwindowsXP

Ihavesomec#codethatworksfineonVista&Windows7butthrowsaGDI+ErroronWindowsXP(

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 个解决方案

#1


1  

I hate GDI+ exception handling, the only error it fires is A generic error occured in GDI+, Try this

我讨厌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#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
author-avatar
啊沙发的非飞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有