【问题标题】:Generate barcode in MVC在 MVC 中生成条形码
【发布时间】:2015-12-10 15:33:33
【问题描述】:

我正在使用 BarcodeLib。 我怎样才能在控制器中这样使用?我在带有 IHttpHandler 的 aspx 中使用它,但如何在控制器中使用?

<img src="/StokBarkod/Barrr?barcodeText=Hello" />

/////

public ActionResult Barcode(string barcodeText)
{
    BarcodeLib.Barcode barcode = new BarcodeLib.Barcode
    {
        IncludeLabel = false,

        Alignment = BarcodeLib.AlignmentPositions.CENTER,
        LabelPosition = LabelPositions.BOTTOMCENTER,
        RotateFlipType = RotateFlipType.RotateNoneFlipNone,
        //LabelFont = new Font("Arial", 18 * 1),
        Width = 135,
        Height = 22,
        BackColor = System.Drawing.Color.White,
        ForeColor = System.Drawing.Color.Black
    };

    //var yeniKod = barcodeText.Replace("//", "%");
    System.Drawing.Image img = barcode.Encode(BarcodeLib.TYPE.CODE128, barcodeText);


    Byte[] bytdizi = new Byte[-1 + 1];
    bytdizi = (Byte[])imageToByteArray(img);

    System.IO.Stream ms = new System.IO.MemoryStream(bytdizi);

    byte[] buffer = new byte[4096];
    int byteSeq = ms.Read(buffer, 0, 4096);

    HttpContext.Current.Response.ContentType = "image/jpeg";
    while (byteSeq > 0)
    {

        HttpContext.Current.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = ms.Read(buffer, 0, 4096);
    }

}

请帮帮我?

【问题讨论】:

  • 你不应该只使用 FileResult 吗?这段代码的后半部分似乎使简单的事情过于复杂。
  • 您永远不应该在操作方法中直接写入响应。你应该总是返回一些继承自 ActionResult 的对象。

标签: c# .net barcode


【解决方案1】:

使用JsBarcode。它很容易在 JS 中生成条形码。

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2019-12-17
    • 2018-12-01
    • 2018-06-29
    相关资源
    最近更新 更多