I'm using the Drawstring method of the Graphics Class to Draw a Text on an Image.The Font is Specified before drawing.
G.DrawString(mytext, font, brush, 0, 0)
The Problem arises when the same text is drawn on an image with smaller size.The Text drawn appears to be larger.I'm looking for a solution to alter the font size according to the image size so that the text don't appear larger or smaller when drawn on images of different sizes.
I'm attaching the images with different sizes with the text of same font size drawn on it. http://i.stack.imgur.com/ZShUI.jpg
http://i.stack.imgur.com/GUfbM.jpg
I can't directly post the image because I'm not allowed.
In my opinion you have two ways:
newImageWidth/originalImageWidth
.It's not perfect because you could have some problem with text height (if new image is not just scaled but with different aspect ratio), but it's an idea
You would get most precise scaling by drawing on separate image and then slapping that image onto original one. You'd do that as follows:
Code:
Take notice of last line and
Rectangle
parameters. Use them to scale your text bitmap onto original image. Alternatively, you can also chooseGraphics.MeasureString
method to determine how wide your text would be and make attempts until you get best one you can.As you requested in comment I'll give you more detailed suggestion here. Say your original image width is WI1, and width of text on it using
Graphics.MeasureString
is WT1. If you resize your image to width WI2, then your perfect text width would beWT2 = WT1 * WI2 / WI1
. UsingDrawText
method you may not be able to get this exact width because when you increase font by 1 it may jump over that value. So you have to make several attempts and find best. Pick a size of font, if resulting text width is smaller (measure with MeasureString), increase it until it becomes bigger than target and you've got about closest match. Same thing goes if it's too big. Decrease font step by step.This is quick and dirty as you see, because you have many draws, but I can't think of better solution, unless you're using monospaced fonts.
Difference between those solutions would be that in first you can get text to fit EXACT size you need, but you probably would loose some font readability due to scaling. Second solution would give good readability, but you can't get pixel perfect size of text.