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");

1 Answer