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 static BarcodeMetrics GetBarcodeMetricsFromContext(HttpContext context) { // 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"]; // Initialise metrics BarcodeMetrics metrics = new BarcodeMetrics(); int value; if (int.TryParse(barWidthText, out value)) { metrics.MinWidth = metrics.MaxWidth = value; } if (int.TryParse(minimumBarWidthText, out value)) { metrics.MinWidth = value; } if (int.TryParse(maximumBarWidthText, out value)) { metrics.MaxWidth = value; } if (int.TryParse(barHeightText, out value)) { metrics.MinHeight = metrics.MaxHeight = value; } if (int.TryParse(minimumBarHeightText, out value)) { metrics.MinHeight = value; } if (int.TryParse(maximumBarHeightText, out value)) { metrics.MaxHeight = value; } if (int.TryParse(interGlyphSpaceText, out value)) { metrics.InterGlyphSpacing = value; } else { metrics.InterGlyphSpacing = -1; } return(metrics); }
public byte[] creabarcode(String barcode) { BarcodeMetrics tamccbb = new BarcodeMetrics(2, 90); System.Drawing.Image imagen; imagen = BarcodeDrawFactory.GetSymbology(BarcodeSymbology.Code128).Draw(barcode, tamccbb); ImageFormat format = ImageFormat.Bmp; MemoryStream mm = new MemoryStream(); imagen.Save(mm, format); imagen.Dispose(); byte[] bytearray = mm.ToArray(); mm.Close(); mm.Dispose(); return(bytearray); }
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); }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler /// that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="context"> /// An <see cref="T:System.Web.HttpContext"/> object that provides /// references to the intrinsic server objects (for example, Request, /// Response, Session, and Server) used to service HTTP requests. /// </param> public void ProcessRequest(HttpContext context) { try { // We want aggressive caching since the barcode will not change // for a given set of request parameters however we disable // caching on proxy servers... context.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); // Determine the symbology desired string symbologyText = context.Request.QueryString["sym"]; BarcodeSymbology symbology; if (!TryParseEnum <BarcodeSymbology>(symbologyText, true, out symbology) || symbology == BarcodeSymbology.Unknown) { throw new ArgumentException("Unable to determine symbology."); } // Get the text to render from the context // NOTE: We URL decode it first... string textToRender = context.Server.UrlDecode( context.Request.QueryString["text"]); if (string.IsNullOrEmpty(textToRender)) { throw new ArgumentException("Must have text to render as barcode."); } // Get the rendering metrics from the context BarcodeMetrics metrics = GetBarcodeMetricsFromContext(context, symbology); // Determine the image format (default is jpeg) RenderImageFormat format; string imageFormatText = context.Request.QueryString["itype"]; if (!TryParseEnum <RenderImageFormat>(imageFormatText, true, out format)) { format = RenderImageFormat.Jpeg; } // Setup content-type and image format for saving ImageFormat imageFormat; switch (format) { case RenderImageFormat.Jpeg: imageFormat = ImageFormat.Jpeg; context.Response.ContentType = "image/jpeg"; break; case RenderImageFormat.Png: imageFormat = ImageFormat.Png; context.Response.ContentType = "image/png"; break; default: throw new ArgumentException( "Unexpected rendering image format encountered."); } // If we can find an encoder for the image type then attempt // to set top quality and monochrome colour depth. ImageCodecInfo codecInfo = ImageCodecInfo.GetImageEncoders() .FirstOrDefault((item) => item.FormatID == imageFormat.Guid); EncoderParameters codecParameters = null; if (codecInfo != null) { // Two parameters; maximum quality and monochrome codecParameters = new EncoderParameters(2); codecParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); codecParameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, 2); } // Create instance of barcode rendering engine BarcodeDraw drawObject = BarcodeDrawFactory.GetSymbology(symbology); using (Image image = drawObject.Draw(textToRender, metrics)) { // Save image to the response stream directly // TODO: Should the response have buffer enabled? if (codecInfo == null) { image.Save(context.Response.OutputStream, imageFormat); } else { image.Save(context.Response.OutputStream, codecInfo, codecParameters); } } // Set status and finalise request handling. context.Response.StatusCode = 200; context.Response.End(); } catch (Exception) { // TODO: Log the error and return a 500... context.Response.StatusCode = 500; context.Response.End(); } }
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); }
/// <summary> /// Draws the specified text using the supplied barcode metrics. /// </summary> /// <param name="text">The text.</param> /// <param name="metrics">A <see cref="T:Zen.Barcode.BarcodeMetrics"/> object.</param> /// <returns></returns> public override sealed Image Draw(string text, BarcodeMetrics metrics) { return DrawQr(text, (BarcodeMetricsQr)metrics); }
public ActionResult Sliting(CoilSlits slits) { ViewBag.Title = "Coil Slit"; Session["CurrForm"] = "CoilSlit"; slits.errMsg = ""; if (slits.inputHidden != null) { //char[] delimiters = { ' ', '+' }; //string[] inputArray = slits.input.Split(delimiters); // split the input string by using the delimiter '+' if (slits.inputHidden.Length < 9) { slits.errMsg = "Wrong Coil ID."; return(View(slits)); } string coilID = slits.inputHidden.Substring(0, 9); int cover_width = 0; int base_width = 0; if (slits.slitWidth == 118) { cover_width = 51; base_width = 67; } if (slits.slitWidth == 150) { cover_width = 67; base_width = 83; } if (slits.slitWidth == 200) { cover_width = 92; base_width = 108; } CodeQrBarcodeDraw QRcode = BarcodeDrawFactory.CodeQr; // to generate QR code Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum; // to generate barcode Image img_QRcode = null; Image img_Barcode = null; byte[] imgBytes; string imgString; var sql = "select * from GRAM_SYD_LIVE.dbo.X_COIL_MASTER where COILID = '" + coilID + "'"; try { using (var context = new DbContext(Global.ConnStr)) { slits.CoilDetails = context.Database.SqlQuery <CoilMaster>(sql).ToList <CoilMaster>(); } } catch (Exception e) { slits.errMsg = "SQL Exception: " + e + ";"; } if (slits.CoilDetails.Count == 0) { slits.errMsg = "No information found in the database."; return(View(slits)); } string M_color = ""; string S_color = ""; int uniqueLocation = 0; if (slits.CoilDetails[0].COLOR.Length == 4) { M_color = slits.CoilDetails[0].COLOR.Substring(0, 2); S_color = slits.CoilDetails[0].COLOR.Substring(2, 2); } switch (slits.CoilDetails[0].TYPE) { case "RA": slits.slitNumber = 4; slits.slitWidth = 170; cover_width = slits.slitWidth; base_width = slits.slitWidth; break; case "SM": break; case "PO": slits.slitNumber = 8; slits.slitWidth = 135; cover_width = slits.slitWidth; base_width = slits.slitWidth; break; case "PL": slits.slitNumber = 4; slits.slitWidth = 255; cover_width = slits.slitWidth; base_width = slits.slitWidth; break; case "IS": slits.slitNumber = 8; slits.slitWidth = 116; cover_width = slits.slitWidth; base_width = slits.slitWidth; break; default: slits.errMsg = "Type not exist."; break; } if (slits.slitNumber > 0 && slits.CoilDetails != null) { string[] slitIDs = new string[slits.slitNumber]; string[] slitLabels = new string[slits.slitNumber]; slits.QRcodes = new string[8]; slits.Barcodes = new string[8]; for (int i = 1; i < slitIDs.Length + 1; i++) { slitIDs[i - 1] = slits.CoilDetails[0].COILID + "_" + i; slits.slits.Add(new CoilSlit()); slits.slits[i - 1].COIL_SLIT_ID = slitIDs[i - 1]; slits.slits[i - 1].TYPE = slits.CoilDetails[0].TYPE; slits.slits[i - 1].COLOR = slits.CoilDetails[0].COLOR; slits.slits[i - 1].M_COLOR = M_color; slits.slits[i - 1].S_COLOR = S_color; slits.slits[i - 1].WEIGHT = (int)(slits.CoilDetails[0].WEIGHT / slits.slitNumber); slits.slits[i - 1].GAUGE = slits.CoilDetails[0].GAUGE; slits.slits[i - 1].LENGTH = slits.CoilDetails[0].CLENGTH; slits.slits[i - 1].UNIQUELOCATION = uniqueLocation; if (i % 2 != 0) { slits.slits[i - 1].WIDTH = cover_width; } else { slits.slits[i - 1].WIDTH = base_width; } slits.slits[i - 1].STATUS = 0; // new -> 0, used -> 1 slitLabels[i - 1] = slits.CoilDetails[0].COILID + "_" + i + "+" + slits.CoilDetails[0].TYPE + "+" + slits.CoilDetails[0].COLOR + "+" + (int)(slits.CoilDetails[0].WEIGHT / slits.slitNumber) + "+" + slits.CoilDetails[0].GAUGE + "+" + slits.slits[i - 1].WIDTH; BarcodeMetrics barcodeMetrics = QRcode.GetDefaultMetrics(150); barcodeMetrics.Scale = 3; //qrcode size img_QRcode = QRcode.Draw(slitLabels[i - 1], barcodeMetrics); imgBytes = turnImageToByteArray(img_QRcode); imgString = Convert.ToBase64String(imgBytes); slits.QRcodes[i - 1] = String.Format("<img src=\"data:image/png;base64,{0}\"/>", imgString); img_Barcode = barcode128.Draw(slitLabels[i - 1], 100); imgBytes = turnImageToByteArray(img_Barcode); imgString = Convert.ToBase64String(imgBytes); slits.Barcodes[i - 1] = String.Format("<img src=\"data:image/png;base64,{0}\"/>", imgString); } slits.CoilSlitIDs = slitIDs; slits.CoilSlitLabels = slitLabels; if (slits.printFlag == "print") { for (int i = 0; i < slits.CoilSlitIDs.Count; i++) { var coilID_sql = new SqlParameter("@coilID", slits.CoilDetails[0].COILID); var coilSlitID_sql = new SqlParameter("@coilSlitID", slits.slits[i].COIL_SLIT_ID); var type_sql = new SqlParameter("@type", slits.slits[i].TYPE); var color_sql = new SqlParameter("@color", slits.slits[i].COLOR); var m_color_sql = new SqlParameter("@m_color", slits.slits[i].M_COLOR); var s_color_sql = new SqlParameter("@s_color", slits.slits[i].S_COLOR); var weight_sql = new SqlParameter("@weight", slits.slits[i].WEIGHT); var gauge_sql = new SqlParameter("@gauge", slits.slits[i].GAUGE); var width_sql = new SqlParameter("@width", slits.slits[i].WIDTH); var status_sql = new SqlParameter("@status", slits.slits[i].STATUS); var userID_sql = new SqlParameter("@userID", ((Scanner.Models.User)Session["User"]).UserName); var length_sql = new SqlParameter("@length", DBNull.Value); var uniqueLocation_sql = new SqlParameter("@uniqueLocation", slits.slits[i].UNIQUELOCATION); if (slits.slits[i].LENGTH != null) { length_sql = new SqlParameter("@length", slits.slits[i].LENGTH); } else { length_sql = new SqlParameter("@length", DBNull.Value); } var sql_update = "exec GramOnline.dbo.proc_Y_AddCoilSlit " + "@coilID, " + "@coilSlitID, " + "@type, " + "@color, " + "@m_color, " + "@s_color, " + "@weight, " + "@gauge, " + "@width, " + "@status, " + "@userID, " + "@length, " + "@uniqueLocation "; try { using (var context = new DbContext(Global.ConnStr)) { context.Database.ExecuteSqlCommand(sql_update, coilID_sql, coilSlitID_sql, type_sql, color_sql, m_color_sql, s_color_sql, weight_sql, gauge_sql, width_sql, status_sql, userID_sql, length_sql, uniqueLocation_sql); } } catch (Exception e) { slits.errMsg = "SQL Exception: " + e.Message + ";"; } } } } } return(View(slits)); }
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(); } }