加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

无法在ASP.NET C#项目中保存条形码生成的图像

发布时间:2020-12-16 09:49:48 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用System.Drawing.Save()保存图像,并且我一直收到无效参数异常. 有人可以看看我的代码并告诉我我做错了什么. 这是生成条形码图像的代码. public class BarcodeHelper { Font barcodeFont; public BarcodeHelper() { PrivateFontCollection fonts
我正在尝试使用System.Drawing.Save()保存图像,并且我一直收到无效参数异常.

有人可以看看我的代码并告诉我我做错了什么.

这是生成条形码图像的代码.

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:UsersAdministratorDesktopYodelShippingYodelShippingimagesbarcode.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;
        }
    }

这是我调用代码来创建和保存条形码图像的地方.我在调用Save()方法时遇到异常.

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("~/Barcodeabc.jpg"),y);
                    Label1.Text = "Image Saved successfully";

    }
    catch (Exception EE)
    {
        EE.ToString();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读