Solved

Center Diagonal Text on Picture?

Posted on 2009-07-01
4
475 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Keyboard 2 41
Programmatically storing deadlock graphs with SQL Server 6 52
How to repeat the data 4 17
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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