/// <summary> /// Gets the parameters. /// </summary> /// <returns></returns> private static TextImageParameters GetParameters(string text, DataTypeDefinition typeDefinition) { TextImageParameters parameters; try { var datatype = (TextImageDataType)typeDefinition.DataType; var prevalueEditor = (TextImagePrevalueEditor)datatype.PrevalueEditor; parameters = new TextImageParameters(text, prevalueEditor.OutputFormat, prevalueEditor.CustomFontPath, prevalueEditor.FontName, prevalueEditor.FontSize, prevalueEditor.FontStyles, prevalueEditor.ForegroundColor, prevalueEditor.BackgroundColor, prevalueEditor.ShadowColor, prevalueEditor.HorizontalAlignment, prevalueEditor.VerticalAlignment, prevalueEditor.ImageHeight, prevalueEditor.ImageWidth, prevalueEditor.BackgroundMedia); } catch (Exception ex) { parameters = new TextImageParameters(ex.Message, OutputFormat.Jpg, string.Empty, "ARIAL", 12, new[] { FontStyle.Regular }, "000", "FFF", "transparent", HorizontalAlignment.Left, VerticalAlignment.Top, -1, -1, null); } return parameters; }
/// <summary> /// Saves the text image. /// </summary> /// <param name = "parameters">The parameters.</param> /// <param name = "imageName">Name of the image.</param> /// <returns></returns> public static string SaveTextImage(TextImageParameters parameters, string imageName) { imageName = LegalizeString(imageName.Replace(" ", "-")); Bitmap textImage; try { textImage = ImageGenerator.GenerateTextImage(parameters); } catch (Exception ex) { HttpContext.Current.Response.Write("Error: " + ex.Message); textImage = ImageGenerator.GenerateTextImage(new TextImageParameters(ex.Message, parameters.OutputFormat, string.Empty, "ARIAL", 12, new[] { new FontStyle() }, "000", "FFF", "transparent", HorizontalAlignment.Left, VerticalAlignment.Top, -1, -1, null)); } var textImageClientFolder = string.Format("~/media/TextImages"); var textImageServerFolder = IOHelper.MapPath(textImageClientFolder); if (!Directory.Exists(textImageServerFolder)) { Directory.CreateDirectory(textImageServerFolder); } var textImageFileName = string.Format("{0}.{1}", imageName, parameters.OutputFormat); var textImageClientFile = string.Format("{0}/{1}", textImageClientFolder, textImageFileName); var textImageServerFile = IOHelper.MapPath(textImageClientFile); textImage.Save(textImageServerFile, ConvertToImageFormat(parameters.OutputFormat)); return(textImageClientFile); }
/// <summary> /// Gets the parameters. /// </summary> /// <returns></returns> private static TextImageParameters GetParameters(string text, DataTypeDefinition typeDefinition) { TextImageParameters parameters; try { var datatype = (TextImageDataType)typeDefinition.DataType; var prevalueEditor = (TextImagePrevalueEditor)datatype.PrevalueEditor; parameters = new TextImageParameters(text, prevalueEditor.OutputFormat, prevalueEditor.CustomFontPath, prevalueEditor.FontName, prevalueEditor.FontSize, prevalueEditor.FontStyles, prevalueEditor.ForegroundColor, prevalueEditor.BackgroundColor, prevalueEditor.ShadowColor, prevalueEditor.HorizontalAlignment, prevalueEditor.VerticalAlignment, prevalueEditor.ImageHeight, prevalueEditor.ImageWidth, prevalueEditor.BackgroundMedia); } catch (Exception ex) { parameters = new TextImageParameters(ex.Message, OutputFormat.Jpg, string.Empty, "ARIAL", 12, new[] { FontStyle.Regular }, "000", "FFF", "transparent", HorizontalAlignment.Left, VerticalAlignment.Top, -1, -1, null); } return(parameters); }
/// <summary> /// Gets the font family. /// </summary> /// <param name = "parameters">The parameters.</param> /// <returns></returns> private static FontFamily GetFontFamily(TextImageParameters parameters) { // Get Font Family FontFamily fontFamily; if (parameters.CustomFontPath != string.Empty) { try { // try to get custom font file var pfc = new PrivateFontCollection(); pfc.AddFontFile(IOHelper.MapPath(parameters.CustomFontPath)); fontFamily = pfc.Families[0]; } catch { // fallback to selected font fontFamily = GetNewFontFamily(parameters.FontName); } } else { fontFamily = GetNewFontFamily(parameters.FontName); } return(fontFamily); }
/// <summary> /// Gets the image with background. /// </summary> /// <param name = "parameters">The parameters.</param> /// <param name = "textImage">The text image.</param> /// <param name = "rectF">The rect F.</param> /// <returns></returns> private static Graphics GetGraphicsWithBackground(TextImageParameters parameters, out Bitmap textImage, out RectangleF rectF) { // set base textImage to background image var umbracoFile = parameters.BackgroundMedia.getProperty(Constants.Umbraco.Media.File).Value.ToString(); var serverFile = IOHelper.MapPath(umbracoFile); textImage = new Bitmap(serverFile); rectF = new RectangleF(0, 0, textImage.Width, textImage.Height); // Initialize graphics return(Graphics.FromImage(textImage)); }
/// <summary> /// Saves the image using the specified parameters /// </summary> /// <param name = "parameters">The parameters.</param> public void SaveImage(TextImageParameters parameters) { //Save image and set the url var imageName = string.Format("{0}-{1}-{2}", MediaHelper.CurrentNode.Id, Guid.NewGuid(), _textBox.Text); var imageUrl = MediaHelper.SaveTextImage(parameters, imageName); // Delete previous image if (ImageUrl != imageUrl && ImageUrl != string.Empty) { DeleteImage(); } ImageUrl = imageUrl; if (Text == string.Empty) { DeleteImage(); } }
/// <summary> /// Saves this instance. /// </summary> public void Save() { BackgroundMediaChooser.Save(); _dataType.DBType = DBTypes.Nvarchar; UpdatePreValue(0, FontNameDropDownList.SelectedItem.Text); UpdatePreValue(1, CustomFontPathTextBox.Text); UpdatePreValue(2, FontSizeTextBox.Text); var commaSepratedList = string.Join(",", (from ListItem listItem in FontStyleCheckBoxList.Items where listItem.Selected select listItem.Value).ToArray()); UpdatePreValue(3, commaSepratedList); UpdatePreValue(4, ForegroundColorPicker.Value); UpdatePreValue(5, BackgroundColorPicker.Value); UpdatePreValue(6, ShadowColorPicker.Value); UpdatePreValue(7, HorizontalAlignmentDropDownList.SelectedItem.Text); UpdatePreValue(8, VerticalAlignmentDropDownList.SelectedItem.Text); UpdatePreValue(9, ImageHeightTextBox.Text); UpdatePreValue(10, ImageWidthTextBox.Text); UpdatePreValue(11, BackgroundMediaChooser.Value); UpdatePreValue(12, ImageFormatDropDownList.SelectedItem.Text); var sampleText = FontName + " " + FontSize + " " + OutputFormat; var imageParameters = new TextImageParameters(sampleText, OutputFormat, CustomFontPath, FontName, FontSize, FontStyles, ForegroundColor, BackgroundColor, ShadowColor, HorizontalAlignment, VerticalAlignment, ImageHeight, ImageWidth, BackgroundMedia); var imageUrl = MediaHelper.SaveTextImage(imageParameters, "Preview" + Page.Request["id"]); UpdatePreValue(13, imageUrl); }
/// <summary> /// Gets the image without background. /// </summary> /// <param name = "parameters">The parameters.</param> /// <param name = "textImage">The text image.</param> /// <param name = "rectF">The rect F.</param> /// <returns></returns> /// Convert.ToFloat private static Graphics GetGraphicsWithoutBackground(TextImageParameters parameters, out Bitmap textImage, out RectangleF rectF) { // Initialize graphics with extra large canvas to // set actual canvas size later from measured string var sizeF = new SizeF(5000, 5000); textImage = GetNewBitmap(sizeF); var graphics = Graphics.FromImage(textImage); // Measure string using text and font size var font = new Font(GetFontFamily(parameters), parameters.FontSize, GetFontStyle(parameters.FontStyles), GraphicsUnit.Point); var measureString = graphics.MeasureString(parameters.Text, font); // Set actual canvas size based on the measured string var canvasSize = new SizeF { Height = parameters.CanvasHeight > 0 ? parameters.CanvasHeight : measureString.Height, Width = parameters.CanvasWidth > 0 ? parameters.CanvasWidth : measureString.Width }; // Reinitialize graphics with actual canvas size textImage = GetNewBitmap(canvasSize); graphics = Graphics.FromImage(textImage); // Set background color var bgBrush = parameters.BackColor.StartsWith("#") ? new SolidBrush(ColorTranslator.FromHtml(parameters.BackColor)) : new SolidBrush(Color.Transparent); // Create and Fill Rectangle with background color rectF = new RectangleF(0, 0, canvasSize.Width, canvasSize.Height); graphics.FillRectangle(bgBrush, rectF); return(graphics); }
/// <summary> /// Saves this instance. /// </summary> public void Save() { BackgroundMediaChooser.Save(); _dataType.DBType = DBTypes.Nvarchar; UpdatePreValue(0, FontNameDropDownList.SelectedItem.Text); UpdatePreValue(1, CustomFontPathTextBox.Text); UpdatePreValue(2, FontSizeTextBox.Text); var commaSepratedList = string.Join(",", (from ListItem listItem in FontStyleCheckBoxList.Items where listItem.Selected select listItem.Value).ToArray()); UpdatePreValue(3, commaSepratedList); UpdatePreValue(4, ForegroundColorPicker.Value); UpdatePreValue(5, BackgroundColorPicker.Value); UpdatePreValue(6, ShadowColorPicker.Value); UpdatePreValue(7, HorizontalAlignmentDropDownList.SelectedItem.Text); UpdatePreValue(8, VerticalAlignmentDropDownList.SelectedItem.Text); UpdatePreValue(9, ImageHeightTextBox.Text); UpdatePreValue(10, ImageWidthTextBox.Text); UpdatePreValue(11, BackgroundMediaChooser.Value); UpdatePreValue(12, ImageFormatDropDownList.SelectedItem.Text); var sampleText = FontName + " " + FontSize + " " + OutputFormat; var imageParameters = new TextImageParameters(sampleText, OutputFormat, CustomFontPath, FontName, FontSize, FontStyles, ForegroundColor, BackgroundColor, ShadowColor, HorizontalAlignment, VerticalAlignment, ImageHeight, ImageWidth, BackgroundMedia); var imageUrl = MediaHelper.SaveTextImage(imageParameters, "Preview" + Page.Request["id"]); UpdatePreValue(13, imageUrl); }
/// <summary> /// Saves the image using the specified parameters /// </summary> /// <param name = "parameters">The parameters.</param> public void SaveImage(TextImageParameters parameters) { //Save image and set the url var imageName = string.Format("{0}-{1}-{2}", MediaHelper.CurrentNode.Id, Guid.NewGuid(), _textBox.Text); var imageUrl = MediaHelper.SaveTextImage(parameters, imageName); // Delete previous image if (ImageUrl != imageUrl && ImageUrl != string.Empty) { DeleteImage(); } ImageUrl = imageUrl; if (Text == string.Empty) { DeleteImage(); } }
/// <summary> /// Saves the text image. /// </summary> /// <param name = "parameters">The parameters.</param> /// <param name = "imageName">Name of the image.</param> /// <returns></returns> public static string SaveTextImage(TextImageParameters parameters, string imageName) { imageName = LegalizeString(imageName.Replace(" ", "-")); Bitmap textImage; try { textImage = ImageGenerator.GenerateTextImage(parameters); } catch (Exception ex) { HttpContext.Current.Response.Write("Error: " + ex.Message); textImage = ImageGenerator.GenerateTextImage(new TextImageParameters(ex.Message, parameters.OutputFormat, string.Empty, "ARIAL", 12, new[] {new FontStyle()}, "000", "FFF", "transparent", HorizontalAlignment.Left, VerticalAlignment.Top, -1, -1, null)); } var textImageClientFolder = string.Format("~/media/TextImages"); var textImageServerFolder = IOHelper.MapPath(textImageClientFolder); if (!Directory.Exists(textImageServerFolder)) { Directory.CreateDirectory(textImageServerFolder); } var textImageFileName = string.Format("{0}.{1}", imageName, parameters.OutputFormat); var textImageClientFile = string.Format("{0}/{1}", textImageClientFolder, textImageFileName); var textImageServerFile = IOHelper.MapPath(textImageClientFile); textImage.Save(textImageServerFile, ConvertToImageFormat(parameters.OutputFormat)); return textImageClientFile; }
/// <summary> /// Generates the text image. /// </summary> /// <param name = "parameters">The parameters.</param> /// <returns></returns> public static Bitmap GenerateTextImage(TextImageParameters parameters) { Graphics graphics; Bitmap textImage; RectangleF rectF; if (parameters.BackgroundMedia == null) { graphics = GetGraphicsWithoutBackground(parameters, out textImage, out rectF); } else { try { graphics = GetGraphicsWithBackground(parameters, out textImage, out rectF); } catch { graphics = GetGraphicsWithoutBackground(parameters, out textImage, out rectF); } } // Render Quality graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Set fore colors var foreColor = parameters.ForeColor; var fgBrush = foreColor.StartsWith("#") ? new SolidBrush(ColorTranslator.FromHtml(foreColor)) : new SolidBrush(Color.Transparent); // Get Font Family var fontFamily = GetFontFamily(parameters); // Get font style var fontStyle = GetFontStyle(parameters.FontStyles); // Set Font var font = new Font(fontFamily, parameters.FontSize, fontStyle, GraphicsUnit.Point); // Set font format var format = GetNewStringFormat(parameters.HAlign, parameters.VAlign); // Draw drop-shadow if (parameters.ShadowColor != "transparent") { DrawDropShadow(parameters.Text, graphics, rectF, DropShadow.BottomRight, font, parameters.ShadowColor, format); } // Finally, draw the textString to the bitmap graphics.DrawString(parameters.Text, font, fgBrush, rectF, format); return(textImage); }