Solved

Center Diagonal Text on Picture?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

786 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