public void PrintLabel(string barcode_str) { //generate barcode image var metrics = new BarcodeMetrics1d(1, 4096, 200); metrics.Scale = 100; var image = BarcodeDrawFactory.Code128WithChecksum.Draw(barcode_str, metrics); using (MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; label.SetImagePngData("GRAPHIC", ms); } label.SetObjectText("TEXT", barcode_str); LabelWriterPrintParams printParams = new LabelWriterPrintParams(); printParams.PrintQuality = LabelWriterPrintQuality.BarcodeAndGraphics; try { label.Print(Framework.GetLabelWriterPrinters().ToArray()[0], printParams); } catch (Exception ex) { } }
/// <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)); } } }
/// <summary> /// Generates a barcode URI. /// </summary> /// <param name="helper">The HTML helper.</param> /// <param name="text">The text to be encoded.</param> /// <param name="symbology">The barcode symbology to use.</param> /// <param name="height">The height.</param> /// <param name="scale"> /// The scale factor to use (null = use default for symbology). /// </param> /// <param name="useExtensionlessUri"> /// <c>true</c> to use extensionless URI; otherwise, <c>false</c>. /// </param> /// <returns></returns> public static string Barcode( this UrlHelper helper, string text, BarcodeSymbology symbology, int height = 30, int?scale = null, bool useExtensionlessUri = true) { BarcodeImageUriBuilder builder = null; // We cheat and get the default metrics var temp = BarcodeDrawFactory.GetSymbology(symbology); var metrics = temp.GetDefaultMetrics(height); if (scale != null) { metrics.Scale = scale.Value; } BarcodeMetrics1d metrics1d = metrics as BarcodeMetrics1d; if (metrics1d != null) { builder = new BarcodeImageUriBuilder { EncodingScheme = symbology, Text = text, BarMaxHeight = metrics1d.MaxHeight, BarMinHeight = metrics1d.MinHeight, BarMaxWidth = metrics1d.MaxWidth, BarMinWidth = metrics1d.MinWidth, Scale = metrics.Scale, UseExtensionlessUri = useExtensionlessUri }; } else { BarcodeMetricsQr metricsQr = metrics as BarcodeMetricsQr; if (metricsQr != null) { builder = new BarcodeImageUriBuilder { EncodingScheme = BarcodeSymbology.CodeQr, Text = text, QrEncodingMode = metricsQr.EncodeMode, QrErrorCorrect = metricsQr.ErrorCorrection, QrVersion = metricsQr.Version, Scale = metrics.Scale, UseExtensionlessUri = useExtensionlessUri }; } } return(helper.Content(builder.ToString())); }
private void buttonGenerate_Click(object sender, EventArgs e) { string barcodeText = textBoxBarcodeText.Text; CodeQrBarcodeDraw barcode = BarcodeDrawFactory.CodeQr; BarcodeMetrics1d metrics = new BarcodeMetrics1d(); try { Random r = new Random(); Image code = BarcodeDrawFactory.CodeQr.Draw(textBoxBarcodeText.Text, 200, 5); pictureBoxBarcode.Image = code; if (dialog.ShowDialog() == DialogResult.OK) { code.Save(dialog.FileName, ImageFormat.Jpeg); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
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(); } }