如何在视图中生成条形码

时间:2014-04-11 09:31:04

标签: c# asp.net asp.net-mvc-3 barcode barcode-printing

条形码是从控制器传递并显示在视图上的字符串:

@shelfEdge.Barcode

如何以条形码格式显示,只需下载条形码字体并将其添加到课程中即可?

欢迎任何想法

更新 我使用了Andrei Schneider的Free Barcode API for .NET中的帖子......

 BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()
    {
        IncludeLabel = true,
        Alignment = AlignmentPositions.CENTER,
        Width = 300,
        Height = 100,
        RotateFlipType = RotateFlipType.RotateNoneFlipNone,
        BackColor = Color.White,
        ForeColor = Color.Black,
    };

    model.BarcodeImage = barcode.Encode(TYPE.CODE128B, "123456789");

然后在视图中@Model.BarcodeImage

但这会引发错误:

    Compiler Error Message: CS0012: The type 'System.Drawing.Image' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Source Error:




Line 54:                             </td>
Line 55:                             <td width="30%">
Line 56:                                 @Model.BarcodeImage
Line 57:                             </td>
Line 58:                         </tr>

我在项目中引用了system.drawing?

2 个答案:

答案 0 :(得分:2)

您需要将条形码生成为图像,然后照常在页面上渲染图像。看到这个免费的库:

http://barcoderender.codeplex.com/

答案 1 :(得分:0)

@ViewBag.mostrar = "data:image/jpg;base64," +
 Convert.ToBase64String((byte[])model.BarcodeImage);

我希望比这更能帮到你