public void ProcessRequest(HttpContext context) { if (context.Request["code"] == null || context.Request["width"] == null || context.Request["height"] == null) { context.Response.StatusCode = 401; return; } int width = Int32.Parse(context.Request["width"].ToString()); int height = Int32.Parse(context.Request["height"].ToString()); BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.IncludeLabel = true; b.Alignment = AlignmentPositions.LEFT; b.LabelPosition = LabelPositions.BOTTOMLEFT; b.LabelFont = new Font("Tohoma", 9); b.Encode(BarcodeLib.TYPE.CODE128, context.Request["code"].ToString(), Color.Black, Color.White, width, height); context.Response.ContentType = "image/jpeg"; b.SaveImage(context.Response.OutputStream, BarcodeLib.SaveTypes.JPG); b.Dispose(); }
public BitmapImage GetCell(string content) { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White; b.ForeColor = System.Drawing.Color.Black; b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.ImageFormat = System.Drawing.Imaging.ImageFormat.Png; System.Drawing.Font font = new System.Drawing.Font("utf-8", 15); b.LabelFont = font; #region 限制字号和长度 double screenWidth = SystemParameters.PrimaryScreenWidth; // 屏幕整体宽度 double screenHeight = SystemParameters.PrimaryScreenHeight; double rPower = screenWidth / screenHeight; double cPower = (content.Length * 11 + 35) * 0.01693; #endregion try { b.Encode(BarcodeLib.TYPE.CODE128, content); error.Visibility = Visibility.Hidden; } catch { error.Visibility = Visibility.Visible; return(null); } byte[] buffer = b.GetImageData(SaveTypes.PNG); Image bitmap; using (MemoryStream ms = new MemoryStream(buffer)) { bitmap = (Image)Image.FromStream(ms); ms.Dispose(); } if (File.Exists("1.png")) { File.Delete("1.png"); } bitmap.Save("1.png"); bitmap.Dispose(); b.Dispose(); BitmapImage bitmapImage; using (Image image = Image.FromFile("1.png")) { ServerSetting.MarCodeHeight = image.Height; ServerSetting.MarCodeWidth = image.Width; using (Graphics graphics = Graphics.FromImage(image)) { Font fonts = new Font("楷体", fontSize, System.Drawing.FontStyle.Bold); SizeF sizeF = graphics.MeasureString(header.Text, fonts); using (Pen pen = new Pen(Color.White, fontSize * 2)) { graphics.DrawRectangle(pen, new Rectangle(0, 0, image.Width, sizeF.Height.ToInt() + 10));//加椭圆边框 } graphics.DrawString(header.Text, fonts, System.Drawing.Brushes.Black, new PointF(image.Width / 2 - sizeF.Width / 2, 0)); } bitmapImage = ImageToBitmapImage(image); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; image.Dispose(); } return(bitmapImage); }
public override void Draw(Graphics g, List<Matrix> listMatrix) { //单位一定要是MM。 g.PageUnit = GraphicsUnit.Millimeter; //如下的这个是偏移些位置 //如下是先从绘制矩形中的拷贝的,然后再修改 if (Route != 0) { PointF pZhongXin = getCentrePoint(); g.TranslateTransform(pZhongXin.X, pZhongXin.Y, MatrixOrder.Prepend); g.RotateTransform((float)Route); g.TranslateTransform(-pZhongXin.X, -pZhongXin.Y); } //定义画笔 Pen _myPen = new Pen(PenColor, _penWidth); _myPen.DashStyle = PenDashStyle; #region string strBarcodeNumber = ""; //如果设置变量名,就用变量名的对应的变量值。 if (_strVarName != "") { strBarcodeNumber = _strVarValue; //_strBarcodeNumber = ""; } else//如果没有变量名就用默认的值 { strBarcodeNumber = _strBarcodeNumber; } if (strBarcodeNumber == "") return; //条形码可能有异常,比如说位数不符等等 try { RectangleF rect = getGraphicsPath(listMatrix).GetBounds(); float fltx = rect.X; float flty = rect.Y; float fltw = rect.Width; float flth = rect.Height; /** float fltx = _X + _XAdd; float flty = _Y + _YAdd; float fltw = _Width + _WidthAdd; float flth = _Height + _HeightAdd; * */ PointF poingStr = new PointF(fltx, flty);//实际位置 int intBarCodeWidth = (int)(fltw * g.DpiX / 25.4); int intBarCodeHeight = (int)(flth * g.DpiY / 25.4); ; string strEncoding = _BarcodeEncoding; BarcodeLib.TYPE myType = BarcodeLib.TYPE.EAN13; switch (_BarcodeEncoding) { case "EAN13": myType = BarcodeLib.TYPE.EAN13; break; case "EAN8": myType = BarcodeLib.TYPE.EAN8; break; case "FIM": myType = BarcodeLib.TYPE.FIM; break; case "Codabar": myType = BarcodeLib.TYPE.Codabar; break; case "UPCA": myType = BarcodeLib.TYPE.UPCA; break; case "UPCE": myType = BarcodeLib.TYPE.UPCE; break; case "UPC_SUPPLEMENTAL_2DIGIT": myType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break; case "UPC_SUPPLEMENTAL_5DIGIT": myType = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break; case "CODE39": myType = BarcodeLib.TYPE.CODE39; break; case "CODE39Extended": myType = BarcodeLib.TYPE.CODE39Extended; break; case "CODE128": myType = BarcodeLib.TYPE.CODE128; break; case "CODE128A": myType = BarcodeLib.TYPE.CODE128A; break; case "CODE128B": myType = BarcodeLib.TYPE.CODE128B; break; case "CODE128C": myType = BarcodeLib.TYPE.CODE128C; break; case "ISBN": myType = BarcodeLib.TYPE.ISBN; break; case "Interleaved2of5": myType = BarcodeLib.TYPE.Interleaved2of5; break; case "Standard2of5": myType = BarcodeLib.TYPE.Standard2of5; break; case "Industrial2of5": myType = BarcodeLib.TYPE.Industrial2of5; break; case "PostNet": myType = BarcodeLib.TYPE.PostNet; break; case "BOOKLAND": myType = BarcodeLib.TYPE.BOOKLAND; break; case "JAN13": myType = BarcodeLib.TYPE.JAN13; break; case "MSI_Mod10": myType = BarcodeLib.TYPE.MSI_Mod10; break; case "MSI_2Mod10": myType = BarcodeLib.TYPE.MSI_2Mod10; break; case "MSI_Mod11": myType = BarcodeLib.TYPE.MSI_Mod11; break; case "MSI_Mod11_Mod10": myType = BarcodeLib.TYPE.MSI_Mod11_Mod10; break; case "Modified_Plessey": myType = BarcodeLib.TYPE.Modified_Plessey; break; case "CODE11": myType = BarcodeLib.TYPE.CODE11; break; case "USD8": myType = BarcodeLib.TYPE.USD8; break; case "UCC12": myType = BarcodeLib.TYPE.UCC12; break; case "UCC13": myType = BarcodeLib.TYPE.UCC13; break; case "LOGMARS": myType = BarcodeLib.TYPE.LOGMARS; break; case "ITF14": myType = BarcodeLib.TYPE.ITF14; break; case "TELEPEN": myType = BarcodeLib.TYPE.TELEPEN; break; case "QR_CODE": //如下得判断长度和宽度是否可以显示,我得茶皂他们最短需要多少。 if ((intBarCodeWidth < 21) || (intBarCodeHeight < 21)) { g.DrawString("图像太小显示不了", new Font("Arial", 6), new SolidBrush(Color.Black), poingStr); g.DrawRectangle(new Pen(Color.Black, 0.5f), fltx, flty, fltw, flth); } else { //只有在这两个中有一个不相等的情况下才需要更新。 if ((_fltOldW != _Width) || (_fltOldh != _Height) || isChangeed) { isChangeed = false;//重新设置成没有更新。 _fltOldW = _Width; _fltOldh = _Height; Hashtable hints = new Hashtable(); //hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错能力 hints.Add(EncodeHintType.ERROR_CORRECTION, _QRCODEErrorLevel);//设置容错率 //如下是读取编码,只有在这个不为空的时候才选择 if (_strLanguageEncodingName != "") { hints.Add(EncodeHintType.CHARACTER_SET, _strLanguageEncodingName);//字符集 } COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(strBarcodeNumber, BarcodeFormat.QR_CODE, intBarCodeWidth, intBarCodeHeight, hints); _imageOld = toBitmap(byteMatrix, g.DpiX, g.DpiY); g.DrawImage(_imageOld, rect); } else//如果没有更新,就直接绘图就可以了。 { g.DrawImage(_imageOld, rect); } } return;//这个是直接返回的。因为下边的是调用的一维码的程序 } BarcodeLib.Barcode bar = new BarcodeLib.Barcode(); bar.IncludeLabel = _isIncludeLabel; bar.LabelFont = _RealFont; bar.LabelPosition = LabelPosition;//条形码文字的位置 //不能少于这个大小 /**因为跟放大缩小相冲突,所以注释掉了,如果图像小,就显示显示不了。 if (intBarCodeWidth < 100) { intBarCodeWidth = 100; _Width = intBarCodeWidth / g.DpiX * 25.4f; _WidthAdd = 0; } if (intBarCodeHeight < 30) { intBarCodeHeight = 30; _Height = intBarCodeHeight / g.DpiY * 25.4f; _HeightAdd = 0; } * */ if ((intBarCodeWidth < 100) || (intBarCodeHeight < 30)) { g.DrawString("图像太小显示不了", new Font("Arial", 6), new SolidBrush(Color.Black), poingStr); g.DrawRectangle(new Pen(Color.Black, 0.5f), fltx, flty, fltw, flth); } else { Image myImage = bar.Encode(myType, strBarcodeNumber, intBarCodeWidth, intBarCodeHeight, g.DpiX, g.DpiY); //将最新的宽度更新。好像不用更新。 g.DrawImage(myImage, rect); } bar.Dispose(); } catch (Exception e) { //因为这个Draw是持续刷新的,为了在条形码数字出错是只提示依次,在此需要这个,而我在条形码数字更改的时候,重新设置那个为空了 if (_strBarcodeErrorMessage != e.Message) { _strBarcodeErrorMessage = e.Message; MessageBox.Show(e.Message); return; } } #endregion //throw new NotImplementedException(); g.ResetTransform();//恢复原先的坐标系。 //base.Draw(g, arrlistMatrix); }