Solved

Center Diagonal Text on Picture?

Posted on 2009-07-01
4
473 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
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
DaemonFish earned 250 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now