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!

Text on image - watermark - font size

Status
Not open for further replies.

alexjamesbrown

Programmer
Mar 7, 2007
8
GB
I have images, that are stored in sql server.
One of the images is retrieved, into a memory stream, then returned as an Image object.

I then have a method called addWatermark(string text, Image img)

Graphics g = Graphics.FromImage(image);

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString(text, new Font("Tahoma",fontSize), Brushes.White, new PointF(0, 0));

i want the text to take up around 70% of the width of the image... im trying to calculate the value fontSize (in pink in the code above)

the image thats passed in could be any size.... not always the same size.

Basically, i need to calculate the max font size, then reduce it by 30% i guess.

any ideas?
 
Here's some example code that you can tweak to suit your needs:

// This is the method you were using, taking a string
// and an image as arguments.
private void addWatermark(string text, Image img)
{
// Get the Graphics object from the image
Graphics G = Graphics.FromImage(img);

// Set anti-aliasing on
G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

// Draw the text. The font used is determined by a call
// to the method getFont (see function desription below)
G.DrawString(text, getFont(img.Width, 70, G, text), Brushes.White, new PointF(0, 0));

// This is used by my test project to view the modified
// image in a picturebox.
picMain.Image = img;

// Disposal of the Graphics object.
G.Dispose();
}

// This method will determine the needed font size and
// return a Font object.
// It takes the image's width, the percentage of the width
// to use, an initialized Graphics object and the text
// to measure as arguments.
private Font getFont(int imageWidth, int percentage, Graphics G, string text)
{
// Determine the ideal width of the string on the image
float targetWidth = (float)imageWidth * (percentage / 100f);
// Set the currently used width to 0
float currentWidth = 0;
// Set the font size to zero
int fontSize = 0;
// Initialize a font object to return later
Font font = new Font("Tahoma", 1);

// Loop while the currently measured width is less
// than the ideal target width
while (currentWidth < targetWidth)
{
// Increase the font size by 1
fontSize++;
// Set the new font accordingly
font = new Font("Tahoma", fontSize);

// Measure the width of the text using this new font size.
currentWidth = G.MeasureString(text, font).Width;
}

// When the current width is equal to or slightly larger
// than the target width the loop will exit and return
// the last used Font object.
return font;
}

I hope I made it clear enough!

Regards,
John Willemse

Regards, Ruffnekk
---
Is it true that cannibals don't eat clowns because they taste funny?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top