?
Solved

Center Diagonal Text on Picture?

Posted on 2009-07-01
4
Medium Priority
?
486 Views
Last Modified: 2012-06-22
Me again, now trying to Center the diagonal text. Problem: the following code results in diagonal, but stacked and uncentered text in upper left corner of photo. Doesn't seem to matter what font size either...

[Example: Stacked (although diagonal]
This
Photo
Is
My
Property!

[Code]
private void btnWriteText_Click(object sender, System.EventArgs e)
{
                Bitmap bm = (Bitmap)PictureBox1.Image;
                Font f = new Font("Arial", 70);
                StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center;
                Graphics g = Graphics.FromImage(bm);
                g.RotateTransform(45);
                g.DrawString("This Photo Is My Property", f, Brushes.Gray, ClientRectangle, sf);

      this.PictureBox1.Image = bm;
}
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
DaemonFish earned 1000 total points
ID: 24756632
Use the Graphics object TranslateTransform to move the origin of the transformation matrix to the center of the image( so transform it half of the width, and half of the height of the image. Then apply the RotateTransform.

The reason why it is not centered after your transform is that the text is not rotered from the text center, but from the transform origin that is upper left corner.

Hope this helps solve your problem :)
0
 

Author Comment

by:pointeman
ID: 24757879
Okay, somewhat better, but still seems to have a 'word-wrap' effect...
[New Code]
private void btnWriteText_Click(object sender, System.EventArgs e)
{
                Bitmap bm = (Bitmap)PictureBox1.Image;
                Font f = new Font("Arial", 70);
                StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center;
                Graphics g = Graphics.FromImage(bm);
               
               g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
              g.TranslateTransform(this.Width / 2, this.Height / 2);
              g.RotateTransform(320);
              g.DrawString("This Photo Is My Property", f, Brushes.Gray, ClientRectangle, sf);

      this.PictureBox1.Image = bm;
}
 
 
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 24758905
What size images are you working with here?...

This worked nicely with a 600x600 image and a Font size of 20:
        private void btnWrite_Click(object sender, EventArgs e)
        {
            Font f = new Font("Arial",20);
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
 
            string msg = "This\r\nPhoto\r\nIs\r\nMy\r\nProperty";
            Bitmap bm = (Bitmap)PictureBox1.Image;
            Graphics g = Graphics.FromImage(bm);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            g.TranslateTransform(bm.Width / 2, bm.Height / 2);
            g.RotateTransform(320);
            Rectangle rc = new Rectangle(new Point(-(bm.Width / 2), -(bm.Height / 2)), bm.Size);
            g.DrawString(msg, f, Brushes.Gray, rc, sf);
            g.Dispose();
 
            this.PictureBox1.Image = bm;
        }

Open in new window

0
 

Author Comment

by:pointeman
ID: 24759404
Yea, I'm finding the Image size is critical, so I must settle for a specific size.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question