Solved

Center Diagonal Text on Picture?

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

19 Experts available now in Live!

Get 1:1 Help Now