标签:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 11 public partial class _Default : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 Response.Write(bar_code("www.sosuo8.com", 100, 1, 2)); 16 } 17 public string bar_code(object str, int ch, int cw, int type_code) 18 { 19 //str:输入的字符串;ch:要显示条形码的高度;cw:要显示条形码的宽度;type_code:代码类型 20 string strTmp = str.ToString(); 21 string code = strTmp; 22 // ToLower()将string转化成小写形式的副本,返回是使用指定区域的性的大小写规则。 23 strTmp = strTmp.ToLower(); 24 int height = ch; 25 int width = cw; 26 27 //将传入的参数进行转化。 28 strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ; 29 strTmp = strTmp.Replace("1", "_||_|__|_|_||"); 30 strTmp = strTmp.Replace("2", "_|_||__|_|_||"); 31 strTmp = strTmp.Replace("3", "_||_||__|_|_|"); 32 strTmp = strTmp.Replace("4", "_|_|__||_|_||"); 33 strTmp = strTmp.Replace("5", "_||_|__||_|_|"); 34 strTmp = strTmp.Replace("7", "_|_|__|_||_||"); 35 strTmp = strTmp.Replace("6", "_|_||__||_|_|"); 36 strTmp = strTmp.Replace("8", "_||_|__|_||_|"); 37 strTmp = strTmp.Replace("9", "_|_||__|_||_|"); 38 strTmp = strTmp.Replace("a", "_||_|_|__|_||"); 39 strTmp = strTmp.Replace("b", "_|_||_|__|_||"); 40 strTmp = strTmp.Replace("c", "_||_||_|__|_|"); 41 strTmp = strTmp.Replace("d", "_|_|_||__|_||"); 42 strTmp = strTmp.Replace("e", "_||_|_||__|_|"); 43 strTmp = strTmp.Replace("f", "_|_||_||__|_|"); 44 strTmp = strTmp.Replace("g", "_|_|_|__||_||"); 45 strTmp = strTmp.Replace("h", "_||_|_|__||_|"); 46 strTmp = strTmp.Replace("i", "_|_||_|__||_|"); 47 strTmp = strTmp.Replace("j", "_|_|_||__||_|"); 48 strTmp = strTmp.Replace("k", "_||_|_|_|__||"); 49 strTmp = strTmp.Replace("l", "_|_||_|_|__||"); 50 strTmp = strTmp.Replace("m", "_||_||_|_|__|"); 51 strTmp = strTmp.Replace("n", "_|_|_||_|__||"); 52 strTmp = strTmp.Replace("o", "_||_|_||_|__|"); 53 strTmp = strTmp.Replace("p", "_|_||_||_|__|"); 54 strTmp = strTmp.Replace("r", "_||_|_|_||__|"); 55 strTmp = strTmp.Replace("q", "_|_|_|_||__||"); 56 strTmp = strTmp.Replace("s", "_|_||_|_||__|"); 57 strTmp = strTmp.Replace("t", "_|_|_||_||__|"); 58 strTmp = strTmp.Replace("u", "_||__|_|_|_||"); 59 strTmp = strTmp.Replace("v", "_|__||_|_|_||"); 60 strTmp = strTmp.Replace("w", "_||__||_|_|_|"); 61 strTmp = strTmp.Replace("x", "_|__|_||_|_||"); 62 strTmp = strTmp.Replace("y", "_||__|_||_|_|"); 63 strTmp = strTmp.Replace("z", "_|__||_||_|_|"); 64 strTmp = strTmp.Replace("-", "_|__|_|_||_||"); 65 strTmp = strTmp.Replace("*", "_|__|_||_||_|"); 66 strTmp = strTmp.Replace("/", "_|__|__|_|__|"); 67 strTmp = strTmp.Replace("%", "_|_|__|__|__|"); 68 strTmp = strTmp.Replace("+", "_|__|_|__|__|"); 69 strTmp = strTmp.Replace(".", "_||__|_|_||_|"); 70 strTmp = strTmp.Replace("_", "<span style=‘height:" + height + ";width:" + width + ";background:#FFFFFF;‘></span>"); 71 strTmp = strTmp.Replace("|", "<span style=‘height:" + height + ";width:" + width + ";background:#000000;‘></span>"); 72 73 if (type_code == 1) 74 { 75 return strTmp + "<BR>" + code; 76 } 77 else 78 { 79 return strTmp; 80 } 81 } 82 } 83 84 方法二: 85 using System.Drawing; 86 87 public void CreateCodeLogo(string code) 88 { 89 90 long len = code.Length; 91 string lastString = ""; 92 char[] list = new char[len + 1]; 93 94 95 list = code.ToCharArray(); 96 97 for (int i = 0; i < list.Length; i++) 98 { 99 lastString += this.ConvertToBinaryString(list[i].ToString()); 100 } 101 102 char[] numList = new char[lastString.Length + 1]; 103 numList = lastString.ToCharArray(); 104 105 106 Bitmap image = new Bitmap(200, 140); 107 Graphics g = Graphics.FromImage(image); 108 109 g.Clear(Color.White); 110 111 Pen penBlack = new Pen(Color.FromArgb(255, 0, 0, 0), 2.5F); 112 Pen penWhite = new Pen(Color.White, 2.5F); 113 114 int j = 0; 115 116 for (float k = 10; j < numList.Length; k += 2F, j++) 117 { 118 if (numList[j].ToString() == "1") 119 { 120 g.DrawLine(penBlack, k, 10, k, 110); 121 122 } 123 else 124 { 125 g.DrawLine(penWhite, k, 10, k, 110); 126 } 127 128 if (j % 4 == 0) 129 { 130 g.DrawString(list[j / 4].ToString(), new System.Drawing.Font("Courier New", 12), new SolidBrush(Color.Red), k, 112); 131 } 132 } 133 image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); 134 } 135 136 137 //将字符串数值转换为二进制字符串数值 138 public string ConvertToBinaryString(string buf) 139 { 140 int[] temp = new int[20]; 141 string binary; 142 int val = 0, i = 0, j; 143 144 //先将字符转化为十进制数 145 try 146 { 147 val = Convert.ToInt32(buf); 148 } 149 catch 150 { 151 val = 0; 152 } 153 154 if (val == 0) 155 { 156 return ("0000"); 157 } 158 159 i = 0; 160 while (val != 0) 161 { 162 temp[i++] = val % 2; 163 val /= 2; 164 } 165 166 binary = ""; 167 168 for (j = 0; j <= i - 1; j++) 169 { 170 binary += (char)(temp[i - j - 1] + 48); 171 } 172 173 if (binary.Length < 4) //如果小于4位左边补零 174 { 175 int len = 4 - binary.Length; 176 string str = ""; 177 178 while (len > 0) 179 { 180 str += "0"; 181 len--; 182 } 183 184 binary = str + binary; 185 } 186 187 return (binary); 188 } 189 190 protected void Button1_Click(object sender, EventArgs e) 191 { 192 CreateCodeLogo(TextBox1.Text); 193 }
标签:
原文地址:http://www.cnblogs.com/soulmate/p/5286046.html