private byte[] DrawImage() { // Determine barcode symbology BarcodeSymbology symbology = BarcodeSymbology.Unknown; string symbologyText = (string)GetCustomProperty("barcode:Symbology"); symbology = (BarcodeSymbology)Enum.Parse(typeof(BarcodeSymbology), symbologyText); if (symbology != BarcodeSymbology.Unknown) { // Create draw object BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(symbology); // Get default metrics and override with values specified in CRI BarcodeMetrics metrics = drawObject.GetDefaultMetrics( GetCustomPropertyInt32("barcode:MaximumBarHeight", 30)); metrics.MinHeight = GetCustomPropertyInt32("barcode:MinimumBarHeight", metrics.MinHeight); metrics.MinWidth = GetCustomPropertyInt32("barcode:MinimumBarWidth", metrics.MinWidth); metrics.MaxWidth = GetCustomPropertyInt32("barcode:MaximumBarWidth", metrics.MaxWidth); metrics.InterGlyphSpacing = GetCustomPropertyInt32("barcode:InterGlyphSpacing", metrics.InterGlyphSpacing); // Get the text to render string textToRender = (string)GetCustomProperty("barcode:Text"); // Determine available space for rendering int criWidth = (int)(_cri.Width.ToInches() * DPI); int criHeight = (int)(_cri.Height.ToInches() * DPI); // Create bitmap of the appropriate size System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( criWidth, criHeight, PixelFormat.Format32bppArgb); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { // Clear entire background g.Clear(System.Drawing.Color.White); // Get barcode image System.Drawing.Image barcodeImage = drawObject.Draw(textToRender, metrics); // Centre the image int x = (bmp.Width - barcodeImage.Width) / 2; int y = (bmp.Height - barcodeImage.Height) / 2; g.DrawImageUnscaled(barcodeImage, x, y); } // Create memory stream for new image using (MemoryStream stream = new MemoryStream()) { // Save image and setup CRI image bmp.Save(stream, ImageFormat.Bmp); return(stream.ToArray()); } } return(null); }
/// <summary> /// Draws the design-time representation of the barcode. /// </summary> /// <param name="g"> /// A <see cref="Graphics"/> object representing the designer surface. /// </param> /// <param name="dp"> /// A <see cref="ReportItemDrawParams"/> containing draw parameters. /// </param> public override void Draw(Graphics g, ReportItemDrawParams dp) { // Our background is always white if (dp.DrawBackground) { g.Clear(Color.White); } // Delegate drawing of outlines if (dp.DrawOutlines) { base.Draw(g, dp.AsOutlinesOnly()); } // Draw content if we can... if (dp.DrawContent && Symbology != BarcodeSymbology.Unknown && !string.IsNullOrEmpty(Text)) { BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(Symbology); BarcodeMetrics metrics = drawObject.GetDefaultMetrics(30); metrics.Scale = Scale; BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { metrics1d.InterGlyphSpacing = InterGlyphSpacing; metrics1d.MaxHeight = MaximumBarHeight; metrics1d.MinHeight = MinimumBarHeight; metrics1d.MaxWidth = MaximumBarWidth; metrics1d.MinWidth = MinimumBarWidth; metrics1d.RenderVertically = RenderVertically; } else if (Symbology == BarcodeSymbology.CodeQr) { BarcodeMetricsQr qrMetrics = (BarcodeMetricsQr)metrics; if (QrVersion != null) { qrMetrics.Version = QrVersion.Value; } if (QrEncodingMode != null) { qrMetrics.EncodeMode = QrEncodingMode.Value; } if (QrErrorCorrectionMode != null) { qrMetrics.ErrorCorrection = QrErrorCorrectionMode.Value; } } using (System.Drawing.Image image = drawObject.Draw(Text, metrics)) { g.DrawImage(image, new Point(0, 0)); } } }
private void button1_Click(object sender, EventArgs e) { //Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; //pictureBox1.Image = barcode.Draw(textBox1.Text, 50); if (textBox1.Text != "") { BarcodeSymbology s = BarcodeSymbology.Code128; BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(s); var metrics = drawObject.GetDefaultMetrics(60); metrics.Scale = 2; pictureBox1.Image = drawObject.Draw(textBox1.Text, metrics); } }
private static string getBarcode(string barcode) { BarcodeSymbology s = BarcodeSymbology.Code39NC; BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(s); var metrics = drawObject.GetDefaultMetrics(45); metrics.Scale = 1; var barcodeImage = drawObject.Draw(barcode, metrics); using (MemoryStream ms = new MemoryStream()) { barcodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] imageBytes = ms.ToArray(); return(Convert.ToBase64String(imageBytes)); } }
public string getBarcode(int type, long n) { Zen.Barcode.BarcodeSymbology s = Zen.Barcode.BarcodeSymbology.Code39C; BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(s); var metrics = drawObject.GetDefaultMetrics(100); metrics.Scale = 1; var barcodeImage = drawObject.Draw(n.ToString(), metrics); Bitmap bm = ResizeImage(barcodeImage, 398, 60); using (MemoryStream ms = new MemoryStream()) { //barcodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //bm.Save(@"C:\test\BarCode(3).jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); byte[] imageBytes = ms.ToArray(); return(string.Format("data:image/png;base64,{0}", Convert.ToBase64String(imageBytes))); } }
public static System.IO.MemoryStream GetQRCodeImage(string serialNo) { //Code128BarcodeDraw bdf = BarcodeDrawFactory.Code128WithChecksum; //System.Drawing.Image img = bdf.Draw(serialNo, 55, 2); BarcodeSymbology s = BarcodeSymbology.Code128; BarcodeDraw drawObject = BarcodeDrawFactory.CodeQr; var metrics = drawObject.GetDefaultMetrics(50); metrics.Scale = 5; var img = drawObject.Draw(serialNo, metrics); ImageConverter ic = new ImageConverter(); Byte[] ba = (Byte[])ic.ConvertTo(img, typeof(Byte[])); MemoryStream memoryStream = new MemoryStream(ba); memoryStream.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); return(memoryStream); }
public byte[] CreateBarcode() { if (!String.IsNullOrEmpty(Code)) { MemoryStream ms = new MemoryStream(); BarcodeDraw d = BarcodeDrawFactory.Code128WithChecksum; var metrics = d.GetDefaultMetrics(60); metrics.Scale = 2; var i = d.Draw(Code, metrics); i.Save(ms, ImageFormat.Jpeg); return(ms.ToArray()); } else { return(new byte[0]); } }
public static Image GetImageBarcode(string barcode) { BarcodeSymbology s = BarcodeSymbology.CodeEan13; BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(s); var metrics = drawObject.GetDefaultMetrics(60); metrics.Scale = 2; string temp = barcode.Remove(barcode.Length - 1, 1); var img = drawObject.Draw(temp, metrics); var resultImage = new Bitmap(img.Width, img.Height + 20); // 20 is bottom padding, adjust to your text using (var graphics = Graphics.FromImage(resultImage)) using (var font = new Font("Consolas", 10)) using (var brush = new SolidBrush(Color.Black)) using (var format = new StringFormat { Alignment = StringAlignment.Center, // Also, horizontally centered text, as in your example of the expected output LineAlignment = StringAlignment.Far }) { graphics.Clear(Color.White); graphics.DrawImage(img, 0, 0); graphics.DrawString(barcode, font, brush, resultImage.Width / 2, resultImage.Height, format); } //Image img = code.Draw(barcode, 20); //float scale = 120F/25.4F; //float widthImg = 37.29F; //float heightImg = 25.93F; //float heightLine = 22.85F * scale; //float widthLine = 0.33F* scale; //string encrypteBarcode = GrtEncryptBarcode(GetType(int.Parse(barcode[0].ToString())), barcode); ////Bitmap img = new Bitmap((int)(widthImg * scale), (int)(heightImg * scale)); //Graphics drawing = Graphics.FromImage(img); //GraphicsState gs = drawing.Save(); //drawing.PageUnit = GraphicsUnit.Pixel; //drawing.PageScale = 1; //RectangleF rect = new RectangleF(0, 0, widthImg * scale, heightImg * scale); //drawing.FillRectangle(new SolidBrush(Color.White), rect); //float curPosX = 11 * widthLine; //foreach (var enBar in encrypteBarcode) //{ // if (enBar == '1') // { // rect = new RectangleF(curPosX, 0, widthLine, heightLine); // drawing.FillRectangle(new SolidBrush(Color.Black), rect); // } // else if (enBar == '2') // { // rect = new RectangleF(curPosX, 0, widthLine, heightLine + 5 * widthLine); // drawing.FillRectangle(new SolidBrush(Color.Black), rect); // } // curPosX += widthLine; //} //curPosX = 11 * widthLine - 3.63F* scale; //for (int i = 0; i < barcode.Length; i++) //{ // drawing.DrawString(barcode[i].ToString(), new Font("Arial", 2.75F * scale), new SolidBrush(Color.Black), curPosX, // heightLine + 0.5F * widthLine); // if (i == 0) // { // curPosX = 14 * widthLine; // } // if(i != 0) // { // curPosX += drawing.MeasureString(barcode[i].ToString(), // new Font("Arial", 2.75F * scale)).Width; // } //} //drawing.Restore(gs); //Bitmap res = new Bitmap((int)Math.Round(widthImg), (int)Math.Round(heightImg)); //Graphics rdrawing = Graphics.FromImage(res); //rdrawing.DrawImage(img, 0, 0, widthImg*scale, heightImg*scale); //drawing.Dispose(); //img.Dispose(); //rdrawing.Dispose(); return(resultImage); }
private byte[] DrawImage() { // Determine barcode symbology BarcodeSymbology symbology = BarcodeSymbology.Unknown; string symbologyText = (string)GetCustomProperty("barcode:Symbology"); symbology = (BarcodeSymbology)Enum.Parse(typeof(BarcodeSymbology), symbologyText); if (symbology != BarcodeSymbology.Unknown) { // Create draw object BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(symbology); // Get default metrics and override with values specified in CRI // TODO: Need more elegant method for doing this... BarcodeMetrics metrics = drawObject.GetDefaultMetrics(30); metrics.Scale = GetCustomPropertyInt32("barcode:Scale", metrics.Scale); BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { metrics1d.MaxHeight = GetCustomPropertyInt32("barcode:MaximumBarHeight", metrics1d.MaxHeight); metrics1d.MinHeight = GetCustomPropertyInt32("barcode:MinimumBarHeight", metrics1d.MinHeight); metrics1d.MinWidth = GetCustomPropertyInt32("barcode:MinimumBarWidth", metrics1d.MinWidth); metrics1d.MaxWidth = GetCustomPropertyInt32("barcode:MaximumBarWidth", metrics1d.MaxWidth); int interGlyphSpacing = GetCustomPropertyInt32("barcode:InterGlyphSpacing", -1); if (interGlyphSpacing >= 0) { metrics1d.InterGlyphSpacing = interGlyphSpacing; } metrics1d.RenderVertically = GetCustomPropertyBool("barcode:RenderVertically", metrics1d.RenderVertically); } else if (symbology == BarcodeSymbology.CodeQr) { BarcodeMetricsQr qrMetrics = (BarcodeMetricsQr)metrics; qrMetrics.Version = GetCustomPropertyInt32("barcode:QrVersion", qrMetrics.Version); qrMetrics.EncodeMode = (QrEncodeMode) GetCustomPropertyInt32("barcode:QrEncodeMode", (int)qrMetrics.EncodeMode); qrMetrics.ErrorCorrection = (QrErrorCorrection) GetCustomPropertyInt32("barcode:QrErrorCorrection", (int)qrMetrics.ErrorCorrection); } // Get the text to render string textToRender = (string)GetCustomProperty("barcode:Text"); // Determine available space for rendering int criWidth = (int)(_cri.Width.ToInches() * DPI); int criHeight = (int)(_cri.Height.ToInches() * DPI); // Create bitmap of the appropriate size System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( criWidth, criHeight, PixelFormat.Format32bppArgb); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { // Clear entire background g.Clear(System.Drawing.Color.White); // Get barcode image System.Drawing.Image barcodeImage = drawObject.Draw(textToRender, metrics); // Centre the image int x = (bmp.Width - barcodeImage.Width) / 2; int y = (bmp.Height - barcodeImage.Height) / 2; g.DrawImageUnscaled(barcodeImage, x, y); } // Create memory stream for new image using (MemoryStream stream = new MemoryStream()) { // Save image and setup CRI image bmp.Save(stream, ImageFormat.Bmp); return(stream.ToArray()); } } return(null); }
private static BarcodeMetrics GetBarcodeMetricsFromContext( HttpContext context, BarcodeSymbology symbology) { BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(symbology); BarcodeMetrics metrics = drawObject.GetDefaultMetrics(30); BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { // Get query parameter strings string barHeightText = context.Request.QueryString["bh"]; string barWidthText = context.Request.QueryString["bw"]; string minimumBarHeightText = context.Request.QueryString["mbh"]; string maximumBarHeightText = context.Request.QueryString["xbh"]; string minimumBarWidthText = context.Request.QueryString["mbw"]; string maximumBarWidthText = context.Request.QueryString["xbw"]; string interGlyphSpaceText = context.Request.QueryString["igs"]; int value; if (int.TryParse(barWidthText, out value)) { metrics1d.MinWidth = metrics1d.MaxWidth = value; } if (int.TryParse(minimumBarWidthText, out value)) { metrics1d.MinWidth = value; } if (int.TryParse(maximumBarWidthText, out value)) { metrics1d.MaxWidth = value; } if (int.TryParse(barHeightText, out value)) { metrics1d.MinHeight = metrics1d.MaxHeight = value; } if (int.TryParse(minimumBarHeightText, out value)) { metrics1d.MinHeight = value; } if (int.TryParse(maximumBarHeightText, out value)) { metrics1d.MaxHeight = value; } if (int.TryParse(interGlyphSpaceText, out value)) { metrics1d.InterGlyphSpacing = value; } } else if (symbology == BarcodeSymbology.CodeQr) { BarcodeMetricsQr qrMetrics = (BarcodeMetricsQr)metrics; string encodeMode = context.Request.QueryString["em"]; string errorCorrect = context.Request.QueryString["ec"]; string scale = context.Request.QueryString["sca"]; string version = context.Request.QueryString["ver"]; int value; if (int.TryParse(encodeMode, out value)) { qrMetrics.EncodeMode = (QrEncodeMode)value; } if (int.TryParse(errorCorrect, out value)) { qrMetrics.ErrorCorrection = (QrErrorCorrection)value; } if (int.TryParse(scale, out value)) { qrMetrics.Scale = value; } if (int.TryParse(version, out value)) { qrMetrics.Version = value; } } return(metrics); }
private void StartAsyncTask(object state) { try { // We want to respond to image requests of the form; // // <encoded barcode>.Barcode // Cache information from context _request = _context.Request; _response = _context.Response; // Filename is the encoded design ID BarcodeImageUri uri = new BarcodeImageUri(_request.Url); // Lookup design and retrieve image data // Stream JPEG image to client _response.ContentType = "image/jpeg"; _response.Clear(); _response.BufferOutput = true; // Get the object capable of rendering the barcode BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(uri.EncodingScheme); BarcodeMetrics metrics = drawObject.GetDefaultMetrics(30); metrics.Scale = uri.Scale; BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { metrics1d.MaxHeight = uri.BarMaxHeight; metrics1d.MinHeight = uri.BarMinHeight; metrics1d.MaxWidth = uri.BarMaxWidth; metrics1d.MinWidth = uri.BarMinWidth; } else if (uri.EncodingScheme == BarcodeSymbology.CodeQr) { BarcodeMetricsQr qrMetrics = (BarcodeMetricsQr)metrics; qrMetrics.EncodeMode = uri.QrEncodingMode; qrMetrics.ErrorCorrection = uri.QrErrorCorrect; qrMetrics.Version = uri.QrVersion; } // Render barcode and save directly onto response stream MemoryStream imageStream = new MemoryStream(); using (Image image = drawObject.Draw(uri.Text, metrics)) { // Save to temporary stream because image tried to seek // during the write operation image.Save(imageStream, ImageFormat.Jpeg); // Move to start of the stream imageStream.Seek(0, SeekOrigin.Begin); // Do synchronous copy to response output stream int blockSize = 1024; byte[] buffer = new byte[blockSize]; while (true) { int bytesRead = imageStream.Read(buffer, 0, blockSize); if (bytesRead == 0) { break; } _response.OutputStream.Write(buffer, 0, bytesRead); } } } catch (Exception e) { _error = e; } finally { _response.End(); SetComplete(); } }