Fixing “Generic error occurred in GDI+” while generating a Barcode

前端 未结 6 2003
春和景丽
春和景丽 2021-01-02 13:11

I am writing a barcode generator in C# I can generate barcodes as bitmaps and can show them in a Picturebox(WindowsForms). On the other hand I could not save my barcode as a

6条回答
  •  失恋的感觉
    2021-01-02 13:42

    I had this problem before, and I was able to solve it using an intermediate bitmap object, something like this:

    Bitmap barCode = CreateBarCode("*"+txtBarCodeStr.Text+"*");
    
    //Use an intermediate bitmap object
    Bitmap bm = new Bitmap(barCode);
    
    bm.Save(@"C:\barcode.gif", ImageFormat.gif);
    picBox.Image = barCode;            
    MessageBox.Show("Created");
    

    I found this solution/workaround here

    The reason is because "You are trying to write to a file which already has a bitmap image open on it" as @Plutonix mentioned before.

提交回复
热议问题