private LabelParameters GetLabelParameters() { LabelParameters parameters = new LabelParameters { Price = priceBox.Text }; parameters.PriceText = Double.TryParse(parameters.Price, out double parsed) ? "$" + parsed.ToString(parameters.PriceFormat) : "$0"; DataGridViewRow selectedRow = null; if (stockGridView.SelectedCells.Count > 0) { selectedRow = stockGridView.SelectedCells[0].OwningRow; parameters.StockNum = selectedRow.Cells[0].Value.ToString(); } if (selectedRow != null && selectedRow.Cells[4].Value != null) { if (DateTime.TryParse(selectedRow.Cells[4].Value.ToString(), out DateTime dt)) { parameters.DateCode = DateCode(dt); } } return(parameters); }
public Image DrawLabel(LabelTemplate labelTemplate, Image img, bool forPrinting, LabelParameters pars, string printText = null) { Dictionary <string, Rectangle> boundingRects = new Dictionary <string, Rectangle> { { "", new Rectangle() } }; //pictureBox1.Tag = labelTemplate.TopMargin; Bitmap bmp = img != null ? (Bitmap)img : new Bitmap(457, 254); using (Graphics g = Graphics.FromImage(bmp)) { g.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height)); if (!forPrinting) { DrawMargins(g, labelTemplate); } g.TextRenderingHint = TextRenderingHint.AntiAlias; DrawLines(g, labelTemplate); int lineInputCount = 0; foreach (var element in labelTemplate.Elements) { Point p = labelTemplate.GetAbsolutePosition(element, element.StartPos, boundingRects[element.Anchor]); string param = element.Parameter; string paramValue = printText == null ? "" : printText; switch (param) { case "stock": paramValue = pars.StockNum; break; case "date": paramValue = pars.DateCode; break; case "store": paramValue = "Dave's Pawn LLC"; break; case "price": paramValue = FormatPrice(pars.PriceText, (LabelTextElement)element); break; default: continue; } if (element.ElementType == ElementType.Text) { Font font = labelTemplate.GetFont((element as LabelTextElement).Font); Brush textBrush = Brushes.Black; LabelTextElement textElement = element as LabelTextElement; if (textElement.Rotation == 0) { Rectangle rect = GetTextBoundingRect(g, font, paramValue, p, textElement.Alignment); if (textElement.Inverted) { Rectangle fillRect = new Rectangle(rect.X + (int)(rect.Height * 0.1), rect.Y, rect.Width - (int)(rect.Height * 0.1), (int)(rect.Height * 0.85)); g.FillRectangle(Brushes.Black, fillRect); textBrush = Brushes.White; } if (textElement.Alignment == Alignment.LEFT) { g.DrawString(paramValue, font, textBrush, p); } else { rect.Width = 500; g.DrawString(paramValue, font, textBrush, rect); } if (element.ID != "") { boundingRects.Add(element.ID, rect); } } else { if (textElement.Alignment == Alignment.FILL) { font = GetFillFont(g, labelTemplate, textElement, paramValue, font.FontFamily.Name, textElement.FillWidth); } DrawRotatedText(g, font, paramValue, textElement.Rotation, p); } } else if (element.ElementType == ElementType.Barcode) { Rectangle boundingRect = new Rectangle(); if ((element as LabelBarcodeElement).Symbology == BarcodeSymbology.Code39) { using (Font C39 = new Font(fonts.Families[0], (element as LabelBarcodeElement).Size)) { boundingRect = GetTextBoundingRect(g, C39, "*" + paramValue + "*", p, Alignment.LEFT); g.DrawString("*" + paramValue + "*", C39, Brushes.Black, p); } } else if ((element as LabelBarcodeElement).Symbology == BarcodeSymbology.Code128) { int barWeight = 1; Bitmap barcode = (Bitmap)Code128Rendering.MakeBarcodeImage(paramValue, barWeight, false); g.DrawImage(barcode, p); boundingRect = new Rectangle(p.X, p.Y, barcode.Width, barcode.Height); } if (element.ID != "") { boundingRects.Add(element.ID, boundingRect); } } } foreach (var element in labelTemplate.InputElements) { string paramValue = printText == null ? textBoxes[lineInputCount].Text : printText; if (paramValue != "") { LabelTextElement textElement = element as LabelTextElement; Rectangle rect = new Rectangle(); Font font = null; Point p = labelTemplate.GetAbsolutePosition(element, element.StartPos, boundingRects[element.Anchor]); if (textElement.Alignment != Alignment.FILL) { font = labelTemplate.GetFont(element.Font); rect = GetTextBoundingRect(g, font, paramValue.Replace("!", ""), p, (element as LabelTextElement).Alignment); g.DrawString(paramValue.Replace("!", ""), font, Brushes.Black, p); AddHighlights(g, font, new Regex("![^!]+!"), paramValue, p.Y, p.X + 3); } else { font = labelTemplate.GetFont(element.Font); font = GetFillFont(g, labelTemplate, textElement, paramValue, p, font.FontFamily.Name); rect = GetTextBoundingRect(g, font, paramValue, p, Alignment.CENTER); rect.Width = rect.Width + (int)(rect.Height * .05); Brush textBrush = Brushes.Black; if (textElement.Inverted) { textBrush = Brushes.White; g.FillRectangle(Brushes.Black, rect); } rect.Width = 500; g.DrawString(paramValue, font, textBrush, rect); } if (element.ID != "") { boundingRects.Add(element.ID, rect); } } lineInputCount++; } } return(bmp); }