CSharp examples for System.Drawing:Image Effect
Draws text on an image within the bounding box specified.
/*//from w w w .j a v a 2 s .c o m Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ using System.Drawing.Imaging; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Drawing; using System; public class Main{ /// <summary> /// Draws text on an image within the bounding box specified. /// </summary> /// <param name="Image">Image to draw on</param> /// <param name="TextToDraw">The text to draw on the image</param> /// <param name="FontToUse">Font in which to draw the text</param> /// <param name="BrushUsing">Defines the brush using</param> /// <param name="BoxToDrawWithin">Rectangle to draw the image within</param> /// <returns>A bitmap object with the text drawn on it</returns> public static Bitmap DrawText(Bitmap Image, string TextToDraw, Font FontToUse, Brush BrushUsing, RectangleF BoxToDrawWithin) { System.Drawing.Bitmap TempBitmap = new Bitmap(Image, Image.Width, Image.Height); System.Drawing.Graphics TempGraphics = System.Drawing.Graphics.FromImage(TempBitmap); TempGraphics.DrawString(TextToDraw, FontToUse, BrushUsing, BoxToDrawWithin); TempGraphics.Dispose(); return TempBitmap; } /// <summary> /// Draws text on an image within the bounding box specified. /// </summary> /// <param name="FileName">Name of the file to load</param> /// <param name="TextToDraw">The text to draw on the image</param> /// <param name="FontToUse">Font in which to draw the text</param> /// <param name="BrushUsing">Defines the brush using</param> /// <param name="BoxToDrawWithin">Rectangle to draw the image within</param> /// <returns>A bitmap object with the text drawn on it</returns> public static Bitmap DrawText(string FileName, string TextToDraw, Font FontToUse, Brush BrushUsing, RectangleF BoxToDrawWithin) { if (!IsGraphic(FileName)) return new Bitmap(1, 1); System.Drawing.Image TempImage = System.Drawing.Image.FromFile(FileName); System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height); System.Drawing.Bitmap ReturnBitmap = Image.DrawText(TempBitmap, TextToDraw, FontToUse, BrushUsing, BoxToDrawWithin); TempBitmap.Dispose(); TempImage.Dispose(); return ReturnBitmap; } /// <summary> /// Draws text on an image within the bounding box specified. /// </summary> /// <param name="FileName">Name of the file to load</param> /// <param name="NewFileName">Name of the file to save to</param> /// <param name="TextToDraw">The text to draw on the image</param> /// <param name="FontToUse">Font in which to draw the text</param> /// <param name="BrushUsing">Defines the brush using</param> /// <param name="BoxToDrawWithin">Rectangle to draw the image within</param> public static void DrawText(string FileName, string NewFileName, string TextToDraw, Font FontToUse, Brush BrushUsing, RectangleF BoxToDrawWithin) { if (!IsGraphic(FileName)) return; ImageFormat FormatUsing = GetFormat(NewFileName); System.Drawing.Bitmap TempBitmap = Image.DrawText(FileName, TextToDraw, FontToUse, BrushUsing, BoxToDrawWithin); TempBitmap.Save(NewFileName, FormatUsing); TempBitmap.Dispose(); } }