using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Color fontColor = Color.Red; // SET FONT COLOR
Color bg_Color = Color.Black; // SET BACKGROUNG COLOR
bool fontBold = false;
bool fontItalic = false;
bool fontUnderline = false;
string text = "I love"; // TEXT
string text1 = "India";
string fontFamily = "impact"; // FONT FAMILY
int w = 600; // IMAGE WIDTH
int h = 400; // IMAGE HEIGHT
int noOfLines = 2;
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); // GET TARGET RECTANGLE
Bitmap bitmap = new Bitmap(w, h);
Graphics g = Graphics.FromImage(bitmap);
color = new SolidBrush(bg_Color);
g.FillRectangle(color, target); // FILL BACKGROUND
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
int trgheight = target.Height / noOfLines;
int x = 3; // LEAVE SPACE TO LEFT,TOP,RIGHT AND BOTTOM OF IMAGE
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();
int useHeight = 0;
text_path.AddString(text, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, useHeight), sf);
useHeight += trgheight;
text_path.AddString(text1, the_font.FontFamily, (int)fontStyle, trgheight, new PointF(0, useHeight), sf);
RectangleF text_rectf = text_path.GetBounds();
PointF[] target_pts = new PointF[3];
target_pts[0] = new PointF(tLeft, tTop);
target_pts[1] = new PointF(tRight, tTop);
target_pts[2] = new PointF(tLeft, tBottom);
//GET POINTS TO DRAW TEXT INTO RECTANGLE
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("TextWithTwo.png"), System.Drawing.Imaging.ImageFormat.Png); // SAVE IMAGE TO THE ROOT
}
}
Monday, December 1, 2008
DRAW STRING WITH TWO LINES IN FIX RECTANGLE
at
2:51 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