Code Snippets – Text on images
.NET, Code Snippet, R&D Lab May 4th, 2009Two code snippets for you:
private void drawTextWithBackground (string Text, Font font, Graphics grpaphics, Brush backgroundBrush, Brush foregroundBrush, int x, int y)
{
SizeF size = grpaphics.MeasureString(Text, font);
grpaphics.FillRectangle(backgroundBrush, x,y,size.Width, size.Height);
grpaphics.DrawString(Text,font,foregroundBrush,x+1,y+1);
}
private void drawTextAtBottom (String Text, Font font, Graphics graphics, Image sourceImage, Brush backgroundBrush, Brush foregroundBrush)
{
SizeF size = graphics.MeasureString(Text, font);
int y = (int) (sourceImage.Height – size.Height);
drawTextWithBackground(Text,font,graphics,backgroundBrush,foregroundBrush,1,y);
}
Does exactly what it says on the tin!


Recent Comments