zoukankan      html  css  js  c++  java
  • MVC 返回图片

    //调用 http://localhost:60663/home/GetCoder39Img?mycode=123443545

    public void GetCoder39Img(string mycode)
    {

    //string str= Request.QueryString.ToString ();
    Response.ContentType = "image/jpeg";

    string codeStr;
    string c;
    //codeStr = "*-%$*"
    codeStr = "*" + mycode + "*"; //Code 39 的特性是前、後置碼會標識「星號(*)」,表示開始和結束

    int bmpHeight = 35;
    int bmpWidth = 0;
    int _x = 0;
    int _y = 20;
    //int 筆寬 = 0;

    if (!string.IsNullOrEmpty(mycode))
    {
    bmpWidth = codeStr.Length * 13;

    Bitmap BMP = new Bitmap(bmpWidth, bmpHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    Graphics G = Graphics.FromImage(BMP);
    G.TextRenderingHint = TextRenderingHint.AntiAlias;
    G.Clear(Color.White);

    Brush brush = new SolidBrush(Color.White);
    G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    G.FillRectangle(brush, 0, 0, bmpWidth, bmpHeight);

    for (int i = 0; i < codeStr.Length; i++)
    {
    //取得 Code 39 碼的規則
    c = this.genBarcode(codeStr.Substring(i, 1).ToUpper());

    for (int j = 0; j < 4; j++)
    {
    if (c.Substring(j, 1).Equals("0"))
    {
    G.DrawLine(Pens.Black, _x, 0, _x, _y);
    }
    else
    {
    G.DrawLine(Pens.Black, _x, 0, _x, _y);
    G.DrawLine(Pens.Black, _x + 1, 0, _x + 1, _y);
    _x += 1;
    }

    _x += 1;

    if (c.Substring(j + 5, 1).Equals("0"))
    {
    G.DrawLine(Pens.White, _x, 0, _x, _y);
    }
    else
    {
    G.DrawLine(Pens.White, _x, 0, _x, _y);
    G.DrawLine(Pens.White, _x + 1, 0, _x + 1, _y);
    _x += 1;
    }

    _x += 1;
    } //end of loop

    if (c.Substring(4, 1).Equals("0"))
    {
    G.DrawLine(Pens.Black, _x, 0, _x, _y);
    }
    else
    {
    G.DrawLine(Pens.Black, _x, 0, _x, _y);
    G.DrawLine(Pens.Black, _x + 1, 0, _x + 1, _y);
    _x += 1;
    }

    _x += 2;
    } //end of loop

    int x = 0;
    int addx = 13;

    G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));
    x += addx;

    for (int k = 0; k < mycode.Length; k++)
    {
    G.DrawString(mycode.Substring(k, 1), new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));
    x = x + addx;
    }

    G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));
    BMP.Save(Response.OutputStream, ImageFormat.Jpeg);
    G.Dispose();
    BMP.Dispose();

    }


    }
    /// <summary>
    /// Code 39 碼的規則。
    /// Code 39 碼可使用的c如下:0~9、A~Z、+、-、*、/、%、$、. 及空白c。
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public string genBarcode(string code)
    {
    switch (code)
    {
    case "0":
    code = "001100100";
    break;
    case "1":
    code = "100010100";
    break;
    case "2":
    code = "010010100";
    break;
    case "3":
    code = "110000100";
    break;
    case "4":
    code = "001010100";
    break;
    case "5":
    code = "101000100";
    break;
    case "6":
    code = "011000100";
    break;
    case "7":
    code = "000110100";
    break;
    case "8":
    code = "100100100";
    break;
    case "9":
    code = "010100100";
    break;
    case "A":
    code = "100010010";
    break;
    case "B":
    code = "010010010";
    break;
    case "C":
    code = "110000010";
    break;
    case "D":
    code = "001010010";
    break;
    case "E":
    code = "101000010";
    break;
    case "F":
    code = "011000010";
    break;
    case "G":
    code = "000110010";
    break;
    case "H":
    code = "100100010";
    break;
    case "I":
    code = "010100010";
    break;
    case "J":
    code = "001100010";
    break;
    case "K":
    code = "100010001";
    break;
    case "L":
    code = "010010001";
    break;
    case "M":
    code = "110000001";
    break;
    case "N":
    code = "001010001";
    break;
    case "O":
    code = "101000001";
    break;
    case "P":
    code = "011000001";
    break;
    case "Q":
    code = "000110001";
    break;
    case "R":
    code = "100100001";
    break;
    case "S":
    code = "010100001";
    break;
    case "T":
    code = "001100001";
    break;
    case "U":
    code = "100011000";
    break;
    case "V":
    code = "010011000";
    break;
    case "W":
    code = "110001000";
    break;
    case "X":
    code = "001011000";
    break;
    case "Y":
    code = "101001000";
    break;
    case "Z":
    code = "011001000";
    break;
    case "*":
    code = "001101000";
    break;
    case "-":
    code = "000111000"; //好像辨識不出來
    break;
    case "%":
    code = "100101000"; //好像辨識不出來
    break;
    case "$":
    code = "010101000"; //好像辨識不出來
    break;
    default:
    code = "010101000"; //都不是就印 $
    break;
    }

    return code;
    }

  • 相关阅读:
    DroidParts 中文系列教程(基于官方教程)
    IDEA添加其他项目为库文件的方法
    IDEA 部署项目的时候出错:Jar not loaded错误
    解决IDEA导入Myclipse项目的时候没有识别为Web项目的问题
    IDEA中安装及配置SVN
    VirtualBox下设置 XP虚拟机桥接模式
    主机上设置共享文件夹供虚拟机访问
    JS的splice()方法在for循环中使用可能会遇到的坑
    Eclipse优化
    State Design Pattern
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/4975463.html
Copyright © 2011-2022 走看看