Carl Weis
Carl Weis

Reputation: 7062

Can't save image for barcode generation in ASP.NET C# project

I'm trying to save an image using System.Drawing.Save() and I keep getting a Invalid Parameter exception.

Can somebody 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 is where I call the code to create and save the barcode image. I'm getting the exception, when calling the Save() method.

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

enter image description here

Upvotes: 4

Views: 4304

Answers (6)

Pradeep Kumar Patel
Pradeep Kumar Patel

Reputation: 36

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

Upvotes: 2

Paul
Paul

Reputation: 36339

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

Upvotes: 0

Patrick Pitre
Patrick Pitre

Reputation: 851

I believe it has to do with the fact that you're trying to save to a relative path. Trying using an absolute path, or getting the current path via a call to Environment.CurrentDirectory, and prepending that to your barcode.png path.

Upvotes: 0

Steve Rowbotham
Steve Rowbotham

Reputation: 2868

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

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

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89222

  1. Install ProcMon (free from Microsoft)
  2. Run it and create a filter for Path contains barcode.png
  3. Run your code.

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

Some ideas

  1. The directory doesn't exist
  2. You don't have permission

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75336

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

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

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

Note that you will probably run into permissions issues at some point (the account running your ASP.NET app will need write permissions at this location).

Upvotes: 1

Related Questions