Results 1 to 15 of 15

Thread: Why is text not being displayed correctly

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Why is text not being displayed correctly

    Hi All,

    I draw some text onto a picture box with the code below but for some reason the text does not get drawn at the position 0,0 why?

    I have attached a image to show the output.

    Code:
    private void button2_Click(object sender, EventArgs e)
            {
     FontFamily fm = new FontFamily("Arial");
                StringFormat st = new StringFormat();
    
                GP.AddString("Hello", fm, 1, 40, new PointF(0, 0), st);
    
    DrawString();
    }
    private void DrawString()
            {
                
    
                Bitmap bat = new Bitmap(pictureEdit1.Width, pictureEdit1.Height);
                Image d = bat;
                Graphics graphics = Graphics.FromImage(d);
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                Brush textBrush = new SolidBrush(Color.Black);
    
                graphics.DrawPath(new Pen(textBrush), GP);
               
                pictureEdit1.Image = d;
                pictureEdit1.Refresh();
            }


    Thanks

    Loftty
    Attached Images Attached Images  

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is text not being displayed correctly

    Hello there,

    I believe i am correct in saying that it has drawn it at co-ordinate 0,0, however, co-ordinates in this sense are calculated from the top left hand corner of the object you are drawing in.

    So it has done exactly what you have asked it to do.

    Hope this helps

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi Gary,

    The Object that i'm drawing to is the picture box, so I would of thought that position 0,0 is like so.

    Thanks

    Loftty
    Attached Images Attached Images  

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why is text not being displayed correctly

    Fonts include space around the characters, otherwise text would be unreadable. The gap between the top of your PictureBox and the actual text is presumably that clearance. That's just an educated guess though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Why is text not being displayed correctly

    Quote Originally Posted by jmcilhinney
    Fonts include space around the characters, otherwise text would be unreadable. The gap between the top of your PictureBox and the actual text is presumably that clearance. That's just an educated guess though.
    It might well be the case, because if you change the size parameter of the .AddString method (a value of 40 in the original post), the space around the drawn text changes proportionally.

    i.e. if you change the size to 10, the text is much nearer the corner.

    I thought that maybe setting the StringFormat Alignment and LineAlignment properties might fix this but there was no effect.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is text not being displayed correctly

    The graphics path's handling of text is awful. Avoid if possible.
    I don't live here any more.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi,

    If I am to avoid the graphics path for handling text then how should I add text to a picture box? I want to be able to move scale and rotate the text?

    Thanks

    Loftty

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why is text not being displayed correctly

    Shouldn't you just be able to call the Graphics.DrawString method? I would think that you'd only use a GraphicsPath if you wanted to combine multiple shapes. If it's just one string then just DrawString alone should be fine I would think.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi,

    That is the problem I want to use multiple shapes

    Thanks

    Loftty

  10. #10
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Why is text not being displayed correctly

    It's a bit of a hack, but try setting the origin point with negative values, eg:
    Code:
    new PointF(-8,-8)
    This might not be good for you though.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi Andy,

    I tried that which worked ok whilst the text has not been rotated, as soon as i add a rotation to the equation the text losses its position it started with?

    Thanks

    Loftty

  12. #12
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Why is text not being displayed correctly

    Quote Originally Posted by loftty
    Hi,

    That is the problem I want to use multiple shapes

    Thanks

    Loftty
    jmcilhinney is correct. Loftty, why does this stop you using multiple shapes?
    I don't live here any more.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi,

    I can't use the grahics.drawstring as I will want to rotate the string, which means if I rotate the graphics object then all the shapes and text would get rotated not just the string.

    Loftty

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why is text not being displayed correctly

    I could be wrong but if you draw all your other bits first, then apply a translation, then draw the string, I think that only the string will affected. You should really apply the complimentary translation afterwards though, because there's no guarantee that that Graphics object won't get used again elsewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Why is text not being displayed correctly

    Hi,

    Sorry to be a pain but I still have not been able to solve this problem. so thought I would post my code here. If you don't mind having a look.

    Thanks

    Loftty

    Code:
    namespace WindowsApplication1
    
    {
    
    public partial class Form1 : Form
    
    {
    
    Bitmap _backbuf;
    
    Graphics _grfx;
    
    GraphicsPath temp;
    
    GraphicsPath _myShape;
    
    bool MouseClicked = false;
    
    float StartX = 0;
    
    float StartY = 0;
    
    float currentX = 0;
    
    float currentY = 0;
    float currentX = 0;
    float currentY = 0;
    
    public Form1()
    
    {
    
    InitializeComponent();
    
    }
    
    
    private void Redraw()
    
    {
    
    float scaleWidth = (float)(((float)trkWidth.Value / (float)trkWidth.Maximum) * 10);
    
    float scaleHeight = (float)(((float)trkHeight.Value / (float)trkHeight.Maximum) * 10);
    
    float rotation = (float)trkRotation.Value;
    
    DrawShape( scaleWidth, scaleHeight, rotation );
    
    pictureBox1.Invalidate();
    
    }
    
    private void DrawShape( float scaleWidth, float scaleHeight, float rotation )
    
    {
    
    temp = (GraphicsPath)_myShape.Clone();
    
    Matrix m1 = new Matrix();
    
    int TX = (int)temp.GetBounds().X + ((int)temp.GetBounds().Width / 2);
    
    int TY = (int)temp.GetBounds().Y + ((int)temp.GetBounds().Height / 2);
    
    m1.Translate(-TX, -TY);
    
    temp.Transform(m1);
    
    Matrix m = new Matrix();
    
    m.Translate(((int)_myShape.GetBounds().X + ((int)_myShape.GetBounds().Width)) / 2, ((int)_myShape.GetBounds().Y + ((int)_myShape.GetBounds().Height) / 2));
    
    m.Rotate(rotation);
    
    m.Scale(scaleWidth, scaleHeight);
    
    temp.Transform(m);
    
    drawPaths();
    
    }
    
     
    
    private void drawPaths()
    
    {
    
    _grfx.Clear(pictureBox1.BackColor);
    
    _grfx.DrawPath(new Pen(Color.Gainsboro, 1), temp);
    
    }
    
    private void pictureBox1_Resize( object sender, EventArgs e )
    
    {
    
    InitBuffer();
    
    }
    
    private void InitBuffer()
    
    {
    
    int w = pictureBox1.Width;
    
    int h = pictureBox1.Height;
    
    _backbuf = new Bitmap( w > 0 ? w : 1, h > 0 ? h : 1 );
    
    _grfx = Graphics.FromImage( _backbuf );
    
    _grfx.SmoothingMode = SmoothingMode.AntiAlias;
    
    }
    
    private void pictureBox1_Paint( object sender, PaintEventArgs e )
    
    {
    
    if ( _backbuf != null )
    
    e.Graphics.DrawImageUnscaled( _backbuf, 0, 0 );
    
    }
    
    private void Form1_Load( object sender, EventArgs e )
    
    {
    
    InitBuffer();
    
    }
    
    private GraphicsPath CreateShape()
    
    {
    
    GraphicsPath temp = new GraphicsPath();
    
    FontFamily f = new FontFamily("Arial");
    
    StringFormat st = new StringFormat();
    
    temp.AddString("Hello", f, 1,40, new PointF(0,0), st);
    
    temp.CloseFigure();
    
    return temp;
    
    }
    
    private void ChangeString()
    
    {
    
    GraphicsPath temp = new GraphicsPath();
    
    FontFamily f = new FontFamily("Arial");
    
    StringFormat st = new StringFormat();
    
    temp.AddString(textBox1.Text, f, 1, 40, new PointF(currentX, currentY), st);
    
    temp.CloseFigure();
    
    _myShape = temp;
    
    Redraw();
    
    }
    
    private void trackBar1_Scroll( object sender, EventArgs e )
    
    {
    
    Redraw();
    
    }
    
    private void trackBar2_Scroll( object sender, EventArgs e )
    
    {
    
    Redraw(); 
    
    }
    
    private void button1_Click( object sender, EventArgs e )
    
    {
    
    _myShape = CreateShape();
    
    Redraw();
    
    }
    
     
    
    private void trackBar3_Scroll( object sender, EventArgs e )
    
    {
    
    drawPaths();
    
    }
    
    private void pictureBox1_MouseDown( object sender, MouseEventArgs e )
    
    {
    
    if ( temp.IsVisible( e.X, e.Y ) )
    
    {
    
    MouseClicked = true;
    
    StartX = e.X - (int)temp.GetBounds().X;
    
    StartY = e.Y - (int)temp.GetBounds().Y;
    
    }
    
    }
    
    private void pictureBox1_MouseMove( object sender, MouseEventArgs e )
    
    {
    
    if ( MouseClicked == true )
    
    {
    
    float GX = e.X - temp.GetBounds().X;
    
    float GY = e.Y - temp.GetBounds().Y;
    
    float distanceX = GX - StartX;
    
    float distanceY = GY - StartY;
    
    Matrix m = new Matrix();
    
    m.Translate(distanceX, distanceY);
    
    _myShape.Transform(m);
    
    Redraw();
    
    }
    
    }
    
    private void pictureBox1_MouseUp( object sender, MouseEventArgs e )
    
    {
    
    MouseClicked = false;
    float GX = temp.GetBounds().X;
    float GY = temp.GetBounds().Y;
    currentX = (GX - MinusX) * 2;
    currentY = GY - MinusY;
    }
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    
    {
    
    ChangeString();
    
    }
    
    }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width