简体   繁体   English

GDI +中发生一般错误。 在System.Drawing.Image.Save

[英]A generic error occurred in GDI+. at System.Drawing.Image.Save

Hi I am getting this error while saving an Image at the given path 嗨,我在给定路径保存图像时遇到此错误

string WriteImage(string data, string imgPath)
{           
    try
    {
        data = "*" + data + "*";
        Bitmap barcode = new Bitmap(1, 1);
        Font threeOfNine = new Font("IDAutomationHC39M", 60, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
        Graphics graphics = Graphics.FromImage(barcode);
        SizeF dataSize = graphics.MeasureString(data, threeOfNine);
        barcode = new Bitmap(barcode, dataSize.ToSize());
        graphics = Graphics.FromImage(barcode);
        graphics.Clear(Color.White);
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
        graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);
        graphics.Flush();
        threeOfNine.Dispose();
        graphics.Dispose();
        barcode.SetResolution(300, 300);
        barcode.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        return imgPath.Substring(imgPath.LastIndexOf("\\")+1);
    }
    catch
    {
        return "";
    }
}

Dont know what i am doing wrong. 不知道我在做什么错。

As I wrote in my comment, I'm not seeing any problem. 正如我在评论中所写,我没有看到任何问题。 The following version of your code outputs information in the event of an error so you can debug it. 以下版本的代码会在发生错误时输出信息,以便您对其进行调试。 It also disposes of resources properly: 它还可以正确地处理资源:

    public static string WriteImage(string data, string imgPath)
    {
        try
        {
            data = "*" + data + "*";
            using (var dummyBitmap = new Bitmap(1, 1))
            using (var threeOfNine = new Font("IDAutomationHC39M", 60, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point))
            {
                SizeF dataSize;
                using (var graphics = Graphics.FromImage(dummyBitmap))
                {
                    dataSize = graphics.MeasureString(data, threeOfNine);
                }
                using (var barcode = new Bitmap(dummyBitmap, dataSize.ToSize()))
                using (var graphics = Graphics.FromImage(barcode))
                using (var brush = new SolidBrush(Color.Black))
                {
                    graphics.Clear(Color.White);
                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                    graphics.DrawString(data, threeOfNine, brush, 0, 0);
                    graphics.Flush();
                    barcode.SetResolution(300, 300);
                    barcode.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Error saving string \"" + data + "\" to a bitmap at location: " + imgPath);
            Debug.WriteLine(ex.ToString());
            return "";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GDI+ 中的 System.Drawing.Image.Save 发生一般性错误 - A generic error occurred in GDI+ at System.Drawing.Image.Save GDI +中发生一般错误。 使用Sysytem.Drawing.Image的ASP.NET CORE MVC - A generic error occurred in GDI+. ASP.NET CORE MVC using Sysytem.Drawing.Image System.Runtime.InteropServices.ExternalException: 'GDI+ 中发生一般错误。' - System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.' 当我尝试保存图像时,出现错误“ GDI +中发生一般错误”。 - When I try to save an image I get the error “A generic error occurred in GDI+.” 错误:GDI +中发生一般错误。 同时保存图像 - Error : A generic error occurred in GDI+. while saving Image 图像到字节数组失败“GDI+ 中发生一般错误。” - Image to byte array fails “A generic error occurred in GDI+.” GDI +中发生一般错误。 在Azure Webapp上 - A generic error occurred in GDI+. on Azure Webapp GDI +中发生一般错误。 PDF到MemoryStream - A generic error occurred in GDI+. PDF to MemoryStream 另一个“GDI+ 中出现一般错误”。 -问题 - Another “A generic error occurred in GDI+.” -Question ExternalException“GDI +中发生了一般错误。” - ExternalException “A generic error occurred in GDI+.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM