Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1 px text border

Status
Not open for further replies.

Ator

Programmer
Jan 23, 2006
26
SE
Hello

Does anyone know how to draw a 1px text-border on a Bitmap?
I've created a Bitmap and want to draw a text with "drawString(...)".

For instance, I want to have 1px black border with white interior.

(This is Windows Forms)

Kind regards!
 
I would create a class that extends the PictureBox. Then override the OnPaint() event.

protected override void OnPaint(......)
{
base.OnPaint(e);

e.Graphics.FillRectangle(Brushes.White, 0,0, 50,10);
e.Graphics.DrawRectangle(Pens.Black, 0,0,50,10);
}

Then use your new PictureBoxControl on your form. In the case above you should see a white Box in the top left corner with a black border (1px).

Hope this gets you started.
 
Monkey, I dont want any border for a rectangle.
It's a text I'm talking about. :)

For instance, "Not a rectangle".
 
Gotcha. You can still use the same idea.

Draw the bitmap first and then draw your text on top of it.

So where i put e.Graphics.FillRectangle(...);

you would put

e.Graphics.DrawString("not a rectangle");

and it should draw the string over the bitmap.
 
No, I dont want to use:

e.Graphics.DrawString("not a rectangle");
e.Graphics.FillRectangle(Brushes.White, 0,0, 50,10);

I want to have 1 pixel border to the text I draw.
But if I first draw the white text (the border), and then the black text (the enterior), the problem is that "drawString()" does not give the size in pixels, but in points.

If I can specify the font-size in pixels, you can do:
1. "not a rectangle", font-size = 22px, x = 10, y = 10
2. "not a rectangle", font-size = 20px, y = 11, y = 11

Is there a way to have the size in pixels?
 
Why not download an outline font? You can include it in the setup of your application.
 
No, I dont want to be dependent on an external font. :)

You dont know if it is possible to draw a string given in size of pixels?
 
I haven't tried this, but create the font like so:

Font f = new Font("Microsoft Sans Serif", 2.0, GraphicsUnit.Pixel);

 
Hi Monkey

No, it doesn't seem to work properly.
But thanks anyway!
 
I've only had 2 hours of sleep today so I'm outta ideas. Keep in mind that when you shrink a font down, it also shrinks the spacing. So you won't get a perfect outline and if the text is long, your letters may be completely offset.


| H | E | L | L | O 12px
| H| E| L| L| O 11px


That's why i recommend an outline font. There may be some kind of transform that deals with this better.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top