Click to See Complete Forum and Search --> : Why is text not being displayed correctly
loftty
Aug 21st, 2007, 07:56 AM
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.
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
gep13
Aug 22nd, 2007, 12:51 AM
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
loftty
Aug 22nd, 2007, 04:01 AM
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
jmcilhinney
Aug 22nd, 2007, 04:31 AM
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.
Andy_P
Aug 22nd, 2007, 05:09 AM
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.
wossname
Aug 22nd, 2007, 06:50 AM
The graphics path's handling of text is awful. Avoid if possible.
loftty
Aug 22nd, 2007, 07:08 AM
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
jmcilhinney
Aug 22nd, 2007, 07:58 AM
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.
loftty
Aug 22nd, 2007, 08:41 AM
Hi,
That is the problem I want to use multiple shapes
Thanks
Loftty
Andy_P
Aug 22nd, 2007, 09:06 AM
It's a bit of a hack, but try setting the origin point with negative values, eg:
new PointF(-8,-8)
This might not be good for you though.
loftty
Aug 22nd, 2007, 09:13 AM
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
wossname
Aug 28th, 2007, 06:12 AM
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?
loftty
Sep 12th, 2007, 10:05 AM
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
jmcilhinney
Sep 12th, 2007, 06:11 PM
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.
loftty
Sep 19th, 2007, 02:59 AM
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
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();
}
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.