var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far; // This will algin Left(Near), Center, Right (Far) strFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.White;
SolidBrush brush = new SolidBrush(color);
thumbnailGraph.DrawString("Your Text", font, brush, new RectangleF(0, 0, newWidth, newHeight), strFormat);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far; // This will algin Left(Near), Center, Right (Far) strFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.White;
SolidBrush brush = new SolidBrush(color);
thumbnailGraph.DrawString("Your Text", font, brush, new RectangleF(0, 0,
Rectangle width here, Rectangle height here), strFormat);
Member
455 Points
216 Posts
Align Text on generated image
May 05, 2010 10:20 AM|pyu.agrawal|LINK
Hi All
I'm creating an image using the code:
I also need to align (all three left, right, center) the text that's written on the image. The text can also be written in inverted position.
Any help there ?
Thanks.
Do mark the most helpful reply/replies as "Answer".
Member
455 Points
216 Posts
Re: Align Text on generated image
May 05, 2010 11:01 PM|pyu.agrawal|LINK
Please have a look at the images.

Image 1
Image 2
Do mark the most helpful reply/replies as "Answer".
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 06, 2010 07:37 AM|Vipindas|LINK
First calculate string width & height.
SizeF oSize = oGrp1.MeasureString("sText", oFont);
PointF oPoint = new PointF((75/2) - (oSize.Width/2), (340/2) - (oSize.Height/2));
oGrp1.DrawString(sText, oFont, oBrushWrite, oPoint, drawFormat);
Member
455 Points
216 Posts
Re: Align Text on generated image
May 06, 2010 02:26 PM|pyu.agrawal|LINK
I suppose this will only help to center align the text. What about left/right align ?
Thanks
Do mark the most helpful reply/replies as "Answer".
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 10, 2010 07:56 AM|Vipindas|LINK
Left Align
SizeF oSize = oGrp1.MeasureString("sText", oFont);
PointF oPoint = new PointF(0, (340/2) - (oSize.Height/2));
oGrp1.DrawString(sText, oFont, oBrushWrite, oPoint, drawFormat);
Right Align
SizeF oSize = oGrp1.MeasureString("sText", oFont);
PointF oPoint = new PointF(75 - oSize.Width, (340/2) - (oSize.Height/2));
oGrp1.DrawString(sText, oFont, oBrushWrite, oPoint, drawFormat);
Member
455 Points
216 Posts
Re: Align Text on generated image
May 12, 2010 03:02 AM|pyu.agrawal|LINK
Even this did not help. The text is placed somewhere outside the generated image.
Do mark the most helpful reply/replies as "Answer".
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 12, 2010 06:24 AM|Vipindas|LINK
http://aspalliance.com/413_Fills_shading_and_alignment_with_GDI
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 12, 2010 06:43 AM|Vipindas|LINK
protected void btnAdd_Click(object sender, EventArgs e)
{
var image = Image.FromFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
int thumbnailSize = 400;
int newWidth, newHeight;
if (image.Width > image.Height)
{
newWidth = thumbnailSize;
newHeight = image.Height * thumbnailSize / image.Width;
}
else
{
newWidth = image.Width * thumbnailSize / image.Height;
newHeight = thumbnailSize;
}
var thumbnailBitmap = new Bitmap(newWidth, newHeight);
var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far; // This will algin Left(Near), Center, Right (Far)
strFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.White;
SolidBrush brush = new SolidBrush(color);
thumbnailGraph.DrawString("Your Text", font, brush, new RectangleF(0, 0, newWidth, newHeight), strFormat);
thumbnailBitmap.Save(Server.MapPath("~/Output.jpg"), image.RawFormat);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
}
I have checked and its working correct
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 12, 2010 06:51 AM|Vipindas|LINK
you can also use strFormat.FormatFlags property to change text direction
strFormat.FormatFlags = StringFormatFlags.DirectionVertical;
Member
455 Points
216 Posts
Re: Align Text on generated image
May 14, 2010 01:31 AM|pyu.agrawal|LINK
Thanks. Your code did help.
Please refer the image, wherein the text Asp.net is now aligned, but the last character 't' appears to be left aligned even though its' not.
Is there any way to make it appear center aligned.
Do mark the most helpful reply/replies as "Answer".
Contributor
4392 Points
933 Posts
Re: Align Text on generated image
May 14, 2010 02:43 AM|Vipindas|LINK
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far; // This will algin Left(Near), Center, Right (Far)
strFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.White;
SolidBrush brush = new SolidBrush(color);
thumbnailGraph.DrawString("Your Text", font, brush, new RectangleF(0, 0, Rectangle width here, Rectangle height here), strFormat);