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 Pr开发者_运维知识库ivateFontCollection 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");
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();
}
}
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).
- Install ProcMon (free from Microsoft)
- Run it and create a filter for Path contains barcode.png
- Run your code.
It logs every file system call and the result (with detailed information)
Some ideas
- The directory doesn't exist
- You don't have permission
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"));
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.
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.
精彩评论