简体   繁体   中英

GDI+ error when saving Bitmap to .png file in c#

I'm creating QR code and I used ZXing on it. but upon saving bitmap to physical file gives me an error.

Please help. thanks!

A Generic error occured in GDI+

I've also tried to change the path but the error still occurs.

      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;
            }

Are you running under IIS or the Development WebServer (Cassini) ?

If you are saving to disk, you need to make sure your ASP.NET Application has rights to write the file (depends on which "pool" your website is running under, and which "identity" you assigned to it, as to what access is available).

ASP.NET can have issues with GDI+ depending on how you use it.

Look at this links for some common issues:

Also suggest you dispose the Bitmaps that are created:

  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).
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM