private Bitmap GenerateImageFromTextWithStyle(string text, bool bold) { bool subtitleFontBold = bold; bool subtitleAlignLeft = true; bool subtitleAlignRight = false; text = Utilities.RemoveHtmlTags(text); Font font; try { var fontStyle = FontStyle.Regular; if (subtitleFontBold) { fontStyle = FontStyle.Bold; } font = new Font(_subtitleFontName, _subtitleFontSize, fontStyle); } catch (Exception exception) { MessageBox.Show(exception.Message); font = new Font(FontFamily.Families[0].Name, _subtitleFontSize); } var bmp = new Bitmap(400, 200); var g = Graphics.FromImage(bmp); SizeF textSize = g.MeasureString("Hj!", font); var lineHeight = (textSize.Height * 0.64f); textSize = g.MeasureString(Utilities.RemoveHtmlTags(text), font); g.Dispose(); bmp.Dispose(); int sizeX = (int)(textSize.Width * 0.8) + 40; int sizeY = (int)(textSize.Height * 0.8) + 30; if (sizeX < 1) { sizeX = 1; } if (sizeY < 1) { sizeY = 1; } bmp = new Bitmap(sizeX, sizeY); g = Graphics.FromImage(bmp); var lefts = new List <float>(); foreach (string line in Utilities.RemoveHtmlFontTag(text.Replace("<i>", string.Empty).Replace("</i>", string.Empty)).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { if (subtitleAlignLeft) { lefts.Add(5); } else if (subtitleAlignRight) { lefts.Add(bmp.Width - (TextDraw.MeasureTextWidth(font, line, subtitleFontBold) + 15)); } else { lefts.Add((float)(bmp.Width - g.MeasureString(line, font).Width * 0.8 + 15) / 2); } } g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; var sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Near;// draw the text to a path var path = new GraphicsPath(); // display italic var sb = new StringBuilder(); int i = 0; bool isItalic = false; float left = 5; if (lefts.Count > 0) { left = lefts[0]; } float top = 5; bool newLine = false; int lineNumber = 0; float leftMargin = left; int newLinePathPoint = -1; Color c = _subtitleColor; var colorStack = new Stack <Color>(); var lastText = new StringBuilder(); while (i < text.Length) { if (text.Substring(i).StartsWith(Environment.NewLine)) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); top += lineHeight; newLine = true; i += Environment.NewLine.Length - 1; lineNumber++; if (lineNumber < lefts.Count) { leftMargin = lefts[lineNumber]; left = leftMargin; } } else { sb.Append(text.Substring(i, 1)); } i++; } if (sb.Length > 0) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); } if (_borderWidth > 0) { g.DrawPath(new Pen(_borderColor, _borderWidth), path); } g.FillPath(new SolidBrush(c), path); g.Dispose(); var nbmp = new NikseBitmap(bmp); nbmp.CropTransparentSidesAndBottom(2, true); return(nbmp.GetBitmap()); }
private void GeneratePreviewReal() { if (listViewStyles.SelectedItems.Count != 1) { return; } if (pictureBoxPreview.Image != null) { pictureBoxPreview.Image.Dispose(); } var bmp = new Bitmap(pictureBoxPreview.Width, pictureBoxPreview.Height); using (Graphics g = Graphics.FromImage(bmp)) { // Draw background const int rectangleSize = 9; for (int y = 0; y < bmp.Height; y += rectangleSize) { for (int x = 0; x < bmp.Width; x += rectangleSize) { Color c = Color.DarkGray; if (y % (rectangleSize * 2) == 0) { if (x % (rectangleSize * 2) == 0) { c = Color.LightGray; } } else { if (x % (rectangleSize * 2) != 0) { c = Color.LightGray; } } g.FillRectangle(new SolidBrush(c), x, y, rectangleSize, rectangleSize); } } // Draw text Font font; try { double fontSize = 20; if (Utilities.IsInteger(textBoxFontSize.Text.Replace("px", string.Empty))) { fontSize = Convert.ToInt32(textBoxFontSize.Text.Replace("px", string.Empty)); } else if (textBoxFontSize.Text.EndsWith('%')) { int num; if (int.TryParse(textBoxFontSize.Text.TrimEnd('%'), out num)) { fontSize = fontSize * num / 100.0; } } font = new Font(comboBoxFontName.Text, (float)fontSize); } catch { font = new Font(Font, FontStyle.Regular); } g.TextRenderingHint = TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias; var sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near }; var path = new GraphicsPath(); bool newLine = false; var sb = new StringBuilder(); sb.Append("This is a test!"); var measuredWidth = TextDraw.MeasureTextWidth(font, sb.ToString(), comboBoxFontWeight.Text == "bold") + 1; var measuredHeight = TextDraw.MeasureTextHeight(font, sb.ToString(), comboBoxFontWeight.Text == "bold") + 1; float left = 5; //if (radioButtonTopLeft.Checked || radioButtonMiddleLeft.Checked || radioButtonBottomLeft.Checked) // left = (float)numericUpDownMarginLeft.Value; //else if (radioButtonTopRight.Checked || radioButtonMiddleRight.Checked || radioButtonBottomRight.Checked) // left = bmp.Width - (measuredWidth + ((float)numericUpDownMarginRight.Value)); //else // left = ((float)(bmp.Width - measuredWidth * 0.8 + 15) / 2); float top = 2; //if (radioButtonTopLeft.Checked || radioButtonTopCenter.Checked || radioButtonTopRight.Checked) // top = (float)numericUpDownMarginVertical.Value; //else if (radioButtonMiddleLeft.Checked || radioButtonMiddleCenter.Checked || radioButtonMiddleRight.Checked) // top = (bmp.Height - measuredHeight) / 2; //else // top = bmp.Height - measuredHeight - ((int)numericUpDownMarginVertical.Value); int leftMargin = 0; int pathPointsStart = -1; //if (radioButtonOpaqueBox.Checked) //{ // if (_isSubStationAlpha) // g.FillRectangle(new SolidBrush(panelBackColor.BackColor), left, top, measuredWidth + 3, measuredHeight + 3); // else // g.FillRectangle(new SolidBrush(panelOutlineColor.BackColor), left, top, measuredWidth + 3, measuredHeight + 3); //} TextDraw.DrawText(font, sf, path, sb, comboBoxFontStyle.Text == "italic", comboBoxFontWeight.Text == "bold", false, left, top, ref newLine, leftMargin, ref pathPointsStart); int outline = 0; // (int)numericUpDownOutline.Value; if (outline > 0) { Color outlineColor = Color.White; g.DrawPath(new Pen(outlineColor, outline), path); } g.FillPath(new SolidBrush(panelFontColor.BackColor), path); } pictureBoxPreview.Image = bmp; }
private void DrawText(string text, Graphics g, Bitmap bmp, ComboBox comboboxfontName, decimal fontSize, CheckBox checkBoxFontBold, RadioButton radioButtonAlignTop, CheckBox checkBoxFontItalic, CheckBox checkBoxFontUnderline, Color fontColor, Color backColor) { Font font; try { font = new Font(comboboxfontName.Text, (float)fontSize); } catch { font = new Font(Font, FontStyle.Regular); } g.TextRenderingHint = TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias; var sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near }; var path = new GraphicsPath(); bool newLine = false; var sb = new StringBuilder(); sb.Append(text); var measuredWidth = TextDraw.MeasureTextWidth(font, sb.ToString(), checkBoxFontBold.Checked) + 1; var measuredHeight = TextDraw.MeasureTextHeight(font, sb.ToString(), checkBoxFontBold.Checked) + 1; //if (radioButtonTopLeft.Checked || radioButtonMiddleLeft.Checked || radioButtonBottomLeft.Checked) // left = (float)numericUpDownMarginLeft.Value; //else if (radioButtonTopRight.Checked || radioButtonMiddleRight.Checked || radioButtonBottomRight.Checked) // left = bmp.Width - (measuredWidth + ((float)numericUpDownMarginRight.Value)); //else float left = ((float)(bmp.Width - measuredWidth * 0.8 + 15) / 2); float top; if (radioButtonAlignTop.Checked) { top = 10; } //else if (radioButtonMiddleLeft.Checked || radioButtonMiddleCenter.Checked || radioButtonMiddleRight.Checked) // top = (bmp.Height - measuredHeight) / 2; else { top = bmp.Height - measuredHeight - 10; } //top -= (int)numericUpDownShadowWidth.Value; //if (radioButtonTopCenter.Checked || radioButtonMiddleCenter.Checked || radioButtonBottomCenter.Checked) // left -= (int)(numericUpDownShadowWidth.Value / 2); const int leftMargin = 0; int pathPointsStart = -1; //if (false) //radioButtonOpaqueBox.Checked) //{ // g.FillRectangle(new SolidBrush(backColor), left, top, measuredWidth + 3, measuredHeight + 3); //} TextDraw.DrawText(font, sf, path, sb, checkBoxFontItalic.Checked, checkBoxFontBold.Checked, checkBoxFontUnderline.Checked, left, top, ref newLine, leftMargin, ref pathPointsStart); var outline = (int)numericUpDownOutline1.Value; // draw shadow if (numericUpDownShadowWidth1.Value > 0) // && radioButtonOutline1.Checked) { var shadowPath = (GraphicsPath)path.Clone(); for (int i = 0; i < (int)numericUpDownShadowWidth1.Value; i++) { var translateMatrix = new Matrix(); translateMatrix.Translate(1, 1); shadowPath.Transform(translateMatrix); using (var p1 = new Pen(Color.FromArgb(250, Color.Black), outline)) //(()) panelBackColor.BackColor), outline); g.DrawPath(p1, shadowPath); } } if (outline > 0) { g.DrawPath(new Pen(backColor, outline), path); } g.FillPath(new SolidBrush(fontColor), path); }
protected override void GeneratePreviewReal() { pictureBoxPreview.Image?.Dispose(); var bmp = new Bitmap(pictureBoxPreview.Width, pictureBoxPreview.Height); using (var g = Graphics.FromImage(bmp)) { // Draw background const int rectangleSize = 9; for (int y = 0; y < bmp.Height; y += rectangleSize) { for (int x = 0; x < bmp.Width; x += rectangleSize) { var c = Color.WhiteSmoke; if (y % (rectangleSize * 2) == 0) { if (x % (rectangleSize * 2) == 0) { c = Color.LightGray; } } else { if (x % (rectangleSize * 2) != 0) { c = Color.LightGray; } } using (var brush = new SolidBrush(c)) { g.FillRectangle(brush, x, y, rectangleSize, rectangleSize); } } } // Draw text Font font; try { font = new Font(comboBoxFontName.Text, (float)numericUpDownFontSize.Value); } catch { font = new Font(Font, FontStyle.Regular); } g.TextRenderingHint = TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias; var sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near }; var path = new GraphicsPath(); bool newLine = false; var sb = new StringBuilder(); sb.Append(Configuration.Settings.General.PreviewAssaText); var measuredWidth = TextDraw.MeasureTextWidth(font, sb.ToString(), checkBoxFontBold.Checked) + 1; var measuredHeight = TextDraw.MeasureTextHeight(font, sb.ToString(), checkBoxFontBold.Checked) + 1; float left; if (radioButtonTopLeft.Checked || radioButtonMiddleLeft.Checked || radioButtonBottomLeft.Checked) { left = (float)numericUpDownMarginLeft.Value; } else if (radioButtonTopRight.Checked || radioButtonMiddleRight.Checked || radioButtonBottomRight.Checked) { left = bmp.Width - (measuredWidth + ((float)numericUpDownMarginRight.Value)); } else { left = (bmp.Width - measuredWidth) / 2; } float top; if (radioButtonTopLeft.Checked || radioButtonTopCenter.Checked || radioButtonTopRight.Checked) { top = (float)numericUpDownMarginVertical.Value; } else if (radioButtonMiddleLeft.Checked || radioButtonMiddleCenter.Checked || radioButtonMiddleRight.Checked) { top = (bmp.Height - measuredHeight) / 2; } else { top = bmp.Height - measuredHeight - ((int)numericUpDownMarginVertical.Value); } top -= (int)numericUpDownShadowWidth.Value; if (radioButtonTopCenter.Checked || radioButtonMiddleCenter.Checked || radioButtonBottomCenter.Checked) { left -= (int)(numericUpDownShadowWidth.Value / 2); } const int leftMargin = 0; int pathPointsStart = -1; if (radioButtonOpaqueBox.Checked) { using (var brush = new SolidBrush(_isSubStationAlpha ? panelBackColor.BackColor : panelOutlineColor.BackColor)) { g.FillRectangle(brush, left, top, measuredWidth + 3, measuredHeight + 3); } } TextDraw.DrawText(font, sf, path, sb, checkBoxFontItalic.Checked, checkBoxFontBold.Checked, checkBoxFontUnderline.Checked, left, top, ref newLine, leftMargin, ref pathPointsStart); int outline = (int)numericUpDownOutline.Value; // draw shadow if (numericUpDownShadowWidth.Value > 0 && radioButtonOutline.Checked) { using (var shadowPath = (GraphicsPath)path.Clone()) using (var p1 = new Pen(Color.FromArgb(250, panelBackColor.BackColor), outline)) { for (int i = 0; i < (int)numericUpDownShadowWidth.Value; i++) { var translateMatrix = new Matrix(); translateMatrix.Translate(1, 1); shadowPath.Transform(translateMatrix); g.DrawPath(p1, shadowPath); } } } if (outline > 0 && radioButtonOutline.Checked) { using (var pen = new Pen(_isSubStationAlpha ? panelBackColor.BackColor : panelOutlineColor.BackColor)) { g.DrawPath(pen, path); } } using (var brush = new SolidBrush(panelPrimaryColor.BackColor)) { g.FillPath(brush, path); } font.Dispose(); } pictureBoxPreview.Image = bmp; }
private Bitmap GenerateImageFromTextWithStyle(string text) { bool subtitleFontBold = false; bool subtitleAlignLeft = comboBoxHAlign.SelectedIndex == 0; bool subtitleAlignRight = false; // remove styles for display text (except italic) text = RemoveSubStationAlphaFormatting(text); text = text.Replace("<b>", string.Empty); text = text.Replace("</b>", string.Empty); text = text.Replace("<B>", string.Empty); text = text.Replace("</B>", string.Empty); text = text.Replace("<u>", string.Empty); text = text.Replace("</u>", string.Empty); text = text.Replace("<U>", string.Empty); text = text.Replace("</U>", string.Empty); Font font; try { var fontStyle = FontStyle.Regular; if (subtitleFontBold) { fontStyle = FontStyle.Bold; } font = new Font(_subtitleFontName, _subtitleFontSize, fontStyle); } catch (Exception exception) { MessageBox.Show(exception.Message); font = new Font(FontFamily.Families[0].Name, _subtitleFontSize); } var bmp = new Bitmap(400, 200); var g = Graphics.FromImage(bmp); SizeF textSize = g.MeasureString("Hj!", font); var lineHeight = (textSize.Height * 0.64f); textSize = g.MeasureString(Utilities.RemoveHtmlTags(text), font); g.Dispose(); bmp.Dispose(); int sizeX = (int)(textSize.Width * 0.8) + 40; int sizeY = (int)(textSize.Height * 0.8) + 30; if (sizeX < 1) { sizeX = 1; } if (sizeY < 1) { sizeY = 1; } bmp = new Bitmap(sizeX, sizeY); g = Graphics.FromImage(bmp); var lefts = new List <float>(); foreach (string line in Utilities.RemoveHtmlFontTag(text.Replace("<i>", string.Empty).Replace("</i>", string.Empty)).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { if (subtitleAlignLeft) { lefts.Add(5); } else if (subtitleAlignRight) { lefts.Add(bmp.Width - (TextDraw.MeasureTextWidth(font, line, subtitleFontBold) + 15)); } else { lefts.Add((float)(bmp.Width - g.MeasureString(line, font).Width * 0.8 + 15) / 2); } } g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; var sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Near;// draw the text to a path var path = new GraphicsPath(); // display italic var sb = new StringBuilder(); int i = 0; bool isItalic = false; float left = 5; if (lefts.Count > 0) { left = lefts[0]; } float top = 5; bool newLine = false; int lineNumber = 0; float leftMargin = left; bool italicFromStart = false; int newLinePathPoint = -1; Color c = _subtitleColor; var colorStack = new Stack <Color>(); var lastText = new StringBuilder(); while (i < text.Length) { if (text.Substring(i).ToLower().StartsWith("<font ")) { float addLeft = 0; int oldPathPointIndex = path.PointCount; if (oldPathPointIndex < 0) { oldPathPointIndex = 0; } if (sb.Length > 0) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); } if (path.PointCount > 0) { PointF[] list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!! for (int k = oldPathPointIndex; k < list.Length; k++) { if (list[k].X > addLeft) { addLeft = list[k].X; } } } if (addLeft == 0) { addLeft = left + 2; } left = addLeft; if (_borderWidth > 0) { g.DrawPath(new Pen(_borderColor, _borderWidth), path); } g.FillPath(new SolidBrush(c), path); path.Reset(); path = new GraphicsPath(); sb = new StringBuilder(); int endIndex = text.Substring(i).IndexOf(">"); if (endIndex == -1) { i += 9999; } else { string fontContent = text.Substring(i, endIndex); if (fontContent.Contains(" color=")) { string[] arr = fontContent.Substring(fontContent.IndexOf(" color=") + 7).Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (arr.Length > 0) { string fontColor = arr[0].Trim('\'').Trim('"').Trim('\''); try { colorStack.Push(c); // save old color if (fontColor.StartsWith("rgb(")) { arr = fontColor.Remove(0, 4).TrimEnd(')').Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); } else { c = System.Drawing.ColorTranslator.FromHtml(fontColor); } } catch { c = _subtitleColor; } } } i += endIndex; } } else if (text.Substring(i).ToLower().StartsWith("</font>")) { if (text.Substring(i).ToLower().Replace("</font>", string.Empty).Length > 0) { if (lastText.ToString().EndsWith(" ") && !sb.ToString().StartsWith(" ")) { string t = sb.ToString(); sb = new StringBuilder(); sb.Append(" " + t); } float addLeft = 0; int oldPathPointIndex = path.PointCount - 1; if (oldPathPointIndex < 0) { oldPathPointIndex = 0; } if (sb.Length > 0) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); } if (path.PointCount > 0) { PointF[] list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!! for (int k = oldPathPointIndex; k < list.Length; k++) { if (list[k].X > addLeft) { addLeft = list[k].X; } } } if (addLeft == 0) { addLeft = left + 2; } left = addLeft; if (_borderWidth > 0) { g.DrawPath(new Pen(_borderColor, _borderWidth), path); } g.FillPath(new SolidBrush(c), path); path.Reset(); //path = new GraphicsPath(); sb = new StringBuilder(); if (colorStack.Count > 0) { c = colorStack.Pop(); } } i += 6; } else if (text.Substring(i).ToLower().StartsWith("<i>")) { italicFromStart = i == 0; if (sb.Length > 0) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); } isItalic = true; i += 2; } else if (text.Substring(i).ToLower().StartsWith("</i>") && isItalic) { if (lastText.ToString().EndsWith(" ") && !sb.ToString().StartsWith(" ")) { string t = sb.ToString(); sb = new StringBuilder(); sb.Append(" " + t); } TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); isItalic = false; i += 3; } else if (text.Substring(i).StartsWith(Environment.NewLine)) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); top += lineHeight; newLine = true; i += Environment.NewLine.Length - 1; lineNumber++; if (lineNumber < lefts.Count) { leftMargin = lefts[lineNumber]; left = leftMargin; } if (isItalic) { italicFromStart = true; } } else { sb.Append(text.Substring(i, 1)); } i++; } if (sb.Length > 0) { TextDraw.DrawText(font, sf, path, sb, isItalic, subtitleFontBold, false, left, top, ref newLine, leftMargin, ref newLinePathPoint); } if (_borderWidth > 0) { g.DrawPath(new Pen(_borderColor, _borderWidth), path); } g.FillPath(new SolidBrush(c), path); g.Dispose(); var nbmp = new NikseBitmap(bmp); nbmp.CropTransparentSidesAndBottom(2, true); return(nbmp.GetBitmap()); }