Cannot save image for barcode generation in ASP.NET C # project

I am trying to save an image using System.Drawing.Save () and I continue to get an Invalid Parameter exception.

Can someone please take a look at my code and tell me what I'm doing wrong.

Here is the code that generates the barcode image.

public class BarcodeHelper { Font barcodeFont; public BarcodeHelper() { PrivateFontCollection fonts; FontFamily family = LoadFontFamily("~/../fonts/Code128bWin.ttf", out fonts); barcodeFont = new Font(family, 20.0f); // when done: barcodeFont.Dispose(); family.Dispose(); family.Dispose(); } public FontFamily LoadFontFamily(string fileName, out PrivateFontCollection fontCollection) { fontCollection = new PrivateFontCollection(); fontCollection.AddFontFile(fileName); return fontCollection.Families[0]; } public Image GenerateBarcode(string barcodeText) { Image barcodeImage; using (barcodeImage = Image.FromFile(@"C:\Users\Administrator\Desktop\YodelShipping\YodelShipping\images\barcode.bmp")) { using (Graphics g = Graphics.FromImage(barcodeImage)) { g.DrawString(barcodeText, new Font(barcodeFont, FontStyle.Bold), Brushes.Black, barcodeImage.Height /2, barcodeImage.Width / 2); } } return barcodeImage; } } 

Here I call the code to create and save the barcode image. I get an exception when calling the Save () method.

 System.Drawing.Image img = barcodeHelper.GenerateBarcode("2lgbub51aj+01000002"); img.Save("~/images/barcode.png"); 

enter image description here

+4
source share
6 answers
 protected void Button1_Click(object sender, EventArgs e) { try { Barcode barcode = new Barcode(); BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE93; BarcodeLib.SaveTypes y = SaveTypes.JPG; barcode.Encode(type, "12344"); barcode.SaveImage(Server.MapPath("~/Barcode\\abc.jpg"), y); Label1.Text = "Image Saved successfully"; } catch (Exception EE) { EE.ToString(); } } 
+2
source

I think the problem is with your file path ("~ / images / barcode.png"). You need to use a valid absolute path (for example, "c: /projects/myproject/images/barcode.png").

Edit: I just noticed that this is ASP.NET, so you only need to do this:

 img.Save(Server.MapPath("~/images/barcode.png")); 

Note that at some point you are likely to run into permission problems (for the account of your ASP.NET application, you will need write permissions at this location).

+1
source
  • Install ProcMon (free from Microsoft)
  • Run it and create a filter for Path containing barcode.png
  • Run your code.

It registers every file system call and result (with detailed information)

Some ideas

  • Directory does not exist
  • You do not have permission
0
source

The Image.Save method needs the full path. Tilde (~) only works in ASP.Net. Try:

 img.Save(HttpContext.Current.Server.MapPath("~/images/barcode.jpg")); 
0
source

I believe this is due to the fact that you are trying to maintain a relative path. Trying to use an absolute path or get the current path by calling Environment.CurrentDirectory and adding that path to your barcode.png path.

0
source

I would check that a) the image folder exists where you think it is, and b) that your ASP.NET process has the rights to write to it.

0
source

All Articles