【问题标题】:GDI+ error when saving Bitmap to .png file in c#在 C# 中将位图保存到 .png 文件时出现 GDI+ 错误
【发布时间】:2015-03-24 01:28:43
【问题描述】:

我正在创建二维码,并在上面使用了ZXing。但是在将位图保存到物理文件时会给我一个错误。

请帮忙。谢谢!

GDI+ 中出现一般错误

我也尝试过更改路径,但仍然出现错误。

      protected void btnSaveInfo_Click(object sender, EventArgs e)
            {
                Bitmap img = new Bitmap(GenerateQR("HelloWorld"));
                img.Save("QRCode.png"); //here gives me an error
            }

      public Bitmap GenerateQR(string text)
            {
                var bw = new ZXing.BarcodeWriter();
                var encOptions = new ZXing.Common.EncodingOptions() { Width = 200, Height = 200, Margin = 0 };
                bw.Options = encOptions;
                bw.Format = ZXing.BarcodeFormat.QR_CODE;
                var result = new Bitmap(bw.Write(text));

                return result;
            }

【问题讨论】:

  • 我测试了你的代码,它对我来说运行良好。我对您的代码唯一要做的就是删除两个new Bitmap(...) 调用,因为bw.Write(text)GenerateQR("HelloWorld") 无论如何都返回Bitmap,但这不会影响程序。无论哪种方式都可以正常工作。

标签: c# asp.net image bitmap zxing


【解决方案1】:

您是在 IIS 下运行还是在 Development WebServer (Cassini) 下运行?

如果您要保存到磁盘,则需要确保您的 ASP.NET 应用程序有权写入文件(取决于您的网站在哪个“池”下运行,以及您分配给它的“身份”,如访问权限)。

根据您使用 GDI+ 的方式,ASP.NET 可能会遇到问题。

查看此链接了解一些常见问题:

还建议您处理已创建的位图:

  protected void btnSaveInfo_Click(object sender, EventArgs e)
        {
            using (Bitmap img = GenerateQR("HelloWorld"))
            {
                img.Save("QRCode.png"); //here gives me an error
            }
        }

  public Bitmap GenerateQR(string text)
        {
            var bw = new ZXing.BarcodeWriter();
            var encOptions = new ZXing.Common.EncodingOptions() { Width = 200, Height = 200, Margin = 0 };
            bw.Options = encOptions;
            bw.Format = ZXing.BarcodeFormat.QR_CODE;

            return bw.Write(text); // already a bitmap...no need to copy (in fact copying might be part of your problem).
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多