using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
// CREATE ENUM FOR SHADOW STYLE
    enum ShadowStyle
    {
        Top_Left,
        Top_Right,
        Bottom_Left,
        Bottom_Right,
        None
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Color fontColor = Color.Red;
            Color shadowColor = Color.Blue; // SET SHADOW COLOR
            Color bg_Color = Color.Black;
            bool fontBold = false;
            bool fontItalic = false;
            bool fontUnderline = false;
            string text = "WELCOME";
            string fontFamily = "impact";
            int w = 600;
            int h = 200;
            ShadowStyle shd = ShadowStyle.Top_Left; // SET SHADOW STYLE Top_Left,Top_Right,Bottom_Left, Bottom_Right and None
            SolidBrush color;
            FontStyle fontStyle = FontStyle.Regular;
            if (fontBold) fontStyle |= FontStyle.Bold;
            if (fontItalic) fontStyle |= FontStyle.Italic;
            if (fontUnderline) fontStyle |= FontStyle.Underline;
            Rectangle target = new Rectangle(0, 0, w, h);
            Bitmap bitmap = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(bitmap);
            color = new SolidBrush(bg_Color);
            g.FillRectangle(color, target);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            int trgheight = target.Height;
            int x = 3, y = 3;
            int tLeft = target.Left + x;
            int tTop = target.Top + x;
            int tRight = target.Right - x;
            int tBottom = target.Bottom - x;
            Font the_font = new Font(fontFamily, trgheight, fontStyle, GraphicsUnit.Pixel);
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            GraphicsPath text_path = new GraphicsPath();
            /* Shadow Style */
            if (shd != ShadowStyle.None)
            {
                text_path.AddString(text, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, 0), sf);
                RectangleF text_rectf1 = text_path.GetBounds();
                PointF[] target_pts1 = new PointF[3];
                switch (shd)
                {
                    case ShadowStyle.Top_Right:
                        target_pts1[0] = new PointF(tLeft + y, tTop - y);
                        target_pts1[1] = new PointF(tRight + y, tTop - y);
                        target_pts1[2] = new PointF(tLeft + y, tBottom - y);
                        break;
                    case ShadowStyle.Top_Left:
                        target_pts1[0] = new PointF(tLeft - y, tTop - y);
                        target_pts1[1] = new PointF(tRight - y, tTop - y);
                        target_pts1[2] = new PointF(tLeft - y, tBottom - y);
                        break;
                    case ShadowStyle.Bottom_Right:
                        target_pts1[0] = new PointF(tLeft + y, tTop + y);
                        target_pts1[1] = new PointF(tRight + y, tTop + y);
                        target_pts1[2] = new PointF(tLeft + y, tBottom + y);
                        break;
                    case ShadowStyle.Bottom_Left:
                        target_pts1[0] = new PointF(tLeft - y, tTop + y);
                        target_pts1[1] = new PointF(tRight - y, tTop + y);
                        target_pts1[2] = new PointF(tLeft - y, tBottom + y);
                        break;
                    default:
                        target_pts1[0] = new PointF(tLeft, tTop);
                        target_pts1[1] = new PointF(tRight, tTop);
                        target_pts1[2] = new PointF(tLeft, tBottom);
                        break;
                }
                color = new SolidBrush(shadowColor);
                g.Transform = new Matrix(text_rectf1, target_pts1);
                g.FillPath(color, text_path);
                text_path = new GraphicsPath();
            }
            /* Draw Text */
            text_path.AddString(text, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, 0), sf);
            RectangleF text_rectf = text_path.GetBounds();
            PointF[] target_pts = new PointF[3];
            switch (shd)
            {
                case ShadowStyle.Top_Right:
                    target_pts[0] = new PointF(tLeft, tTop + y);
                    target_pts[1] = new PointF(tRight - y, tTop + y);
                    target_pts[2] = new PointF(tLeft, tBottom);
                    break;
                case ShadowStyle.Top_Left:
                    target_pts[0] = new PointF(tLeft + y, tTop + y);
                    target_pts[1] = new PointF(tRight + y, tTop + y);
                    target_pts[2] = new PointF(tLeft + y, tBottom);
                    break;
                case ShadowStyle.Bottom_Right:
                    target_pts[0] = new PointF(tLeft, tTop - y);
                    target_pts[1] = new PointF(tRight - y, tTop - y);
                    target_pts[2] = new PointF(tLeft, tBottom);
                    break;
                case ShadowStyle.Bottom_Left:
                    target_pts[0] = new PointF(tLeft + y, tTop - y);
                    target_pts[1] = new PointF(tRight + y, tTop - y);
                    target_pts[2] = new PointF(tLeft + y, tBottom);
                    break;
                default:
                    target_pts[0] = new PointF(tLeft, tTop);
                    target_pts[1] = new PointF(tRight, tTop);
                    target_pts[2] = new PointF(tLeft, tBottom);
                    break;
            }
            g.Transform = new Matrix(text_rectf, target_pts);
            color = new SolidBrush(fontColor);
            g.FillPath(color, text_path);
            g.ResetTransform();
            text_path.Dispose();
            sf.Dispose();
            the_font.Dispose();
            g.Dispose();
            bitmap.Save(Server.MapPath("TextWithShadow.png"), System.Drawing.Imaging.ImageFormat.Png);
        }
    }
Monday, December 1, 2008
DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR AND SHADOW
at
2:46 AM
·
      
Category: Graphics GDI+
Subscribe to:
Post Comments (Atom)
Archivo
- 
▼ 
2008
            (20)
          
- 
▼ 
December
            (14)
          
- Building Custom Control : Tell A Friend Custom Con...
 - Watermark on image : Draw string on the image
 - Remember me on this computer - Store username and ...
 - Building URL with www.
 - HIGHLIGHT STRING IN IMAGE [ DRAW STRING WITH HIGHL...
 - LOOPING IN STORED PROCEDURE [CURSOR IN SQL SERVER]
 - CHANGE IMAGE COLOR USING C# ( GRAPHICS GDI+ )
 - DRAW STRING WITH MULTI LINES IN FIX RECTANGLE
 - DRAW STRING WITH TWO LINES IN FIX RECTANGLE
 - DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR...
 - DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR...
 - DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR...
 - DRAW STRING IN FIX RECTANGLE WITH BACKGROUNG COLOR
 - DRAW STRING IN FIX RECTANGLE
 
 
 - 
▼ 
December
            (14)
          
 
Categories
- Custom Control (1)
 - Data Control (1)
 - Graphics GDI+ (11)
 - Other (6)
 - SQL SERVER (1)
 
0 comments:
Post a Comment