C# adding string to image, using the max font size - c#

I want to add 2 string to an image as follows:
This is Text
------------
------------
------------
--Other-----
How do I use the largest font size possible without have the String go off the side of the image?
example: if text is too big then it goes off the image::
This is Text That is too big
------------
------------
------------
--Other-----

I wrote this script on my previous projects to fit some text into the image by calculating its dimensions for each font-size. when the font size is bigger than the image's width, it lowers the font size by 0.1em and tries again until the text fits in the image. Here's the code :
public static string drawTextOnMarker(string markerfile, string text, string newfilename,Color textColor)
{
//Uri uri = new Uri(markerfile, UriKind.Relative);
//markerfile = uri.AbsolutePath;
//uri = new Uri(newfilename, UriKind.Relative);
//newfilename = uri.AbsolutePath;
if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(newfilename)))
{
try
{
Bitmap bmp = new Bitmap(System.Web.HttpContext.Current.Server.MapPath(markerfile));
Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
SolidBrush myBrush = new SolidBrush(textColor);
float fontsize = 10;
bool sizeSetupCompleted = false;
while (!sizeSetupCompleted)
{
SizeF mySize = g.MeasureString(text, new Font("Verdana", fontsize, FontStyle.Bold));
if (mySize.Width > 24 || mySize.Height > 13)
{
fontsize-= float.Parse("0.1");
}
else
{
sizeSetupCompleted = true;
}
}
g.DrawString(text, new Font("Verdana", fontsize, FontStyle.Bold), myBrush, new RectangleF(4, 3, 24, 8), strFormat);
bmp.Save(System.Web.HttpContext.Current.Server.MapPath(newfilename));
return newfilename.Substring(2);
}
catch (Exception)
{
return markerfile.Substring(2);
}
}
return newfilename.Substring(2);
}

Here is a quick solution:
using (Graphics g = Graphics.FromImage(bmp))
{
float width = g.MeasureString(text, font).Width;
float scale = bmp.Width / width;
g.ScaleTransform(scale, scale); //Simple trick not to use other Font instance
g.DrawString(text, font, Brushes.Black, PointF.Empty);
g.ResetTransform();
...
}
Your text won't be always 100% width if you use TextRenderingHint.AntiAliasGridFit or similar, but I think that's not a problem as you just want to make sure the text alawys fits in the image.

Related

Color issue when drawing text on an PNG image in C#

I want to draw black text over with grey opacity PNG file so text is BLACK.
What I am getting is the text is some % of grey:
Even if I use Brushes.Black the text is still grey;
My code is following:
List<string> GenerateDeviceIcon(string backgroundImageFile, string deviceImageFile, string deviceNumber, int deviceID, string saveNewFilePath, string fontName, int fontSize, Brush textColor)
{
var r = new List<string>();
try
{
Image background = Image.FromFile(backgroundImageFile);
Image logo = Image.FromFile(deviceImageFile);
PointF firstLocation = new PointF(2f, 2f);
using (background)
{
using (var bitmap = new Bitmap(background.Width, background.Height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font(fontName, fontSize))
{
canvas.DrawString(deviceNumber, arialFont, textColor, firstLocation);
}
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(background, new Rectangle(0, 0, background.Width, background.Height), new Rectangle(0, 0, background.Width, background.Height), GraphicsUnit.Pixel);
canvas.DrawImage(logo, (bitmap.Width / 2) - (logo.Width / 2), (bitmap.Height / 2) - (logo.Height / 2));
canvas.Save();
}
try
{
var filename = Path.Combine(saveNewFilePath, deviceID.ToString() + ".png");
if (File.Exists(filename))
{
File.Delete(filename);
}
bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception ex)
{
r.Add(ex.Message);
}
}
}
}
catch (Exception ex)
{
r.Add(ex.Message);
}
return r;
}
How to fix it?
Many thanks!
Well I found the bug: dont draw text BEFORE you draw a background!
And I've improved the code so it draws multiple lines of a transport ID.
Enjoy if you need create complex icons in .NET!
Code:
static List<string> GenerateDeviceIcon2(string backgroundImageFile, string deviceImageFile,
string deviceNumber, int deviceID, string saveNewFilePath, string fontName, int fontSize, Color textColor)
{
var r = new List<string>();
try
{
Image background = Image.FromFile(backgroundImageFile);
Image logo = Image.FromFile(deviceImageFile);
PointF firstLocation = new PointF(2f, 2f);
#region Create text as Image with Transparancy
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawingText = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
int maxWidth = background.Width - 2;
var font = new Font(fontName, fontSize, new FontStyle());
SizeF textSize = drawingText.MeasureString(deviceNumber, font, maxWidth);
//set the stringformat flags to rtl
StringFormat sf = new StringFormat
{
//uncomment the next line for right to left languages
//sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
Trimming = StringTrimming.Word
};
//free up the dummy image and old graphics object
img.Dispose();
drawingText.Dispose();
//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
// drawingText = Graphics.FromImage(img);
#endregion
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
using (background)
{
using (var bitmap = new Bitmap(background.Width, background.Height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
//Adjust for high quality
canvas.CompositingQuality = CompositingQuality.HighQuality;
canvas.InterpolationMode = InterpolationMode.HighQualityBilinear;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.SmoothingMode = SmoothingMode.HighQuality;
canvas.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//paint the background
canvas.Clear(Color.Transparent);
// First - draw a background!
canvas.DrawImage(background, new Rectangle(0, 0, background.Width, background.Height),
new Rectangle(0, 0, background.Width, background.Height), GraphicsUnit.Pixel);
// Second - draw the text in multiple rows over background
canvas.DrawImage(logo, (bitmap.Width / 2) - (logo.Width / 2), (bitmap.Height / 2) - (logo.Height / 2));
// Third - draw the logo over background
canvas.DrawString(deviceNumber, font, textBrush, new RectangleF(0, 0, textSize.Width, textSize.Height), sf);
canvas.Save();
}
try
{
var filename = Path.Combine(saveNewFilePath, deviceID.ToString() + ".png");
if (File.Exists(filename))
{
File.Delete(filename);
}
bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception ex)
{
r.Add(ex.Message);
}
}
}
textBrush.Dispose();
img.Dispose();
}
catch (Exception ex)
{
r.Add(ex.Message);
}
return r;
}

out of memory with picturebox

I am getting a out of memory every time I try to add a picture to a picture box
I have tried doing a dispose at the beginning of it but it still just gives me out of memory when I try to add a image from file
public void water(string path,string oupath)
{
pictureBox1.Image = Image.FromFile(path);
Font myfont = new Font("Times New Roman", 12.0f);
Graphics g = Graphics.FromImage(pictureBox1.Image);
// Calculate the size of the text
string waterString = waterText.Text;
SizeF sz = g.MeasureString(waterString, myfont);
// drawing position (X,Y)
Brush myBrush = new SolidBrush(Color.White);
int X;
int Y;
//Set the drawing position based on the users
//selection of placing the text at the bottom or top of the image
X = (int)(pictureBox1.Image.Width - sz.Width) / 2;
Y = (int)(pictureBox1.Image.Height - sz.Height);
// draw the water mark text
g.DrawString(waterString, myfont, myBrush, new Point(X, Y));
string filename = Path.GetFileName(path);
path = oupath+"/"+filename;
pictureBox1.Image.Save(#"path",ImageFormat.Jpeg);
pictureBox1.Image.Dispose();
}

c# Graphics and GraphicsPath Alignment

I'm attempting to draw some centered text inside of a custom panel control. I'm able to create the text path using GraphicsPath.AddString and draw the text using Graphics.FillPath. Here's the code I'm using (it resides inside the custom panel control).
public void ApplyCenteredTextMessage(string message, FontStyle fontStyle)
{
using (Font messageFont = new Font("Arial", 36, fontStyle, GraphicsUnit.Point))
{
using (StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
using (Graphics graphics = this.CreateGraphics())
{
using (Font adjustedMessageFont = GetAdjustedFont(graphics, message, messageFont, this.Bounds, 160, 10))
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddString(message, adjustedMessageFont.FontFamily, (int) fontStyle, (graphics.DpiY * adjustedMessageFont.Size) / 72, Point.Empty, stringFormat);
RectangleF graphicsPathBounds = path.GetBounds();
PointF[] targetPoints = { new PointF(0, 0), new PointF(ClientSize.Width, 0), new PointF(0, ClientSize.Height) };
var translate = new Matrix(graphicsPathBounds, targetPoints);
path.Transform(translate);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.FillPath(Brushes.LawnGreen, path);
}
}
}
}
}
}
private Font GetAdjustedFont(Graphics graphicRef, string graphicString, Font originalFont, RectangleF container, int maxFontSize, int minFontSize)
{
Font testFont = null;
// We utilize MeasureString which we get via a control instance
for (int adjustedSize = maxFontSize; adjustedSize >= minFontSize; adjustedSize--)
{
testFont = new Font(originalFont.Name, adjustedSize, originalFont.Style);
// Test the string with the new size
SizeF adjustedSizeNew = graphicRef.MeasureString(graphicString, testFont);
if (container.Width > Convert.ToInt32(adjustedSizeNew.Width) && container.Height > Convert.ToInt32(adjustedSizeNew.Height))
{
// Good font, return it
return testFont;
}
}
return testFont;
}
The result is this
After the graphics.FillPath in the above code I try to use the GraphicsPath path with the IsVisible method to detect if any of the black squares intersect with the drawn text, but the results I get from that have the text in a completely different location than the above result in the image. I can't figure out where the difference, or stretch is coming from.
path.IsVisible result

Write a word from a string in a different color using g.DrawString()

I use the function below to draw a string on an image. It words great for the most part when I draw a string using one color.
However I want to have a word be in a different color. For example I want to draw "This is a TEST", I want TEST to be red.
What accomplish this using this method?
System.Drawing.Image newImg = new System.Drawing.Bitmap(500, 500);
pictureBox1.Image = TextOverlay(newImg, "This is a TEST", this.Font, Color.Black, ContentAlignment.MiddleCenter, 0.6F);
I'm referring to this line of code and not the OverlayColor parameter:
g.DrawString(OverlayText, f, b, rect, strFormat);
Here is the complete function:
public static Bitmap TextOverlay(Image img, string OverlayText, Font OverlayFont, Color OverlayColor, System.Drawing.ContentAlignment Position, float PercentFill)
{
// create bitmap and graphics used for drawing
// "clone" image but use 24RGB format
Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(img, 0, 0);
int alpha = 255;
// Create the brush based on the color and alpha
SolidBrush b = new SolidBrush(Color.FromArgb(alpha, OverlayColor));
// Measure the text to render (unscaled, unwrapped)
StringFormat strFormat = StringFormat.GenericTypographic;
SizeF s = g.MeasureString(OverlayText, OverlayFont, 100000, strFormat);
// Enlarge font to specified fill (estimated by AREA)
float zoom = (float)(Math.Sqrt(((double)(img.Width * img.Height) * PercentFill) / (double)(s.Width * s.Height)));
FontStyle sty = OverlayFont.Style;
Font f = new Font(OverlayFont.FontFamily, ((float)OverlayFont.Size) * zoom, sty);
int charFit;
int linesFit;
float SQRTFill = (float)(Math.Sqrt(PercentFill));
strFormat.FormatFlags = StringFormatFlags.NoClip; //|| StringFormatFlags.LineLimit || StringFormatFlags.MeasureTrailingSpaces;
strFormat.Trimming = StringTrimming.Word;
SizeF layout = new SizeF(((float)img.Width) * SQRTFill, ((float)img.Height) * 1.5F); // fit to width, allow height to go over
s = g.MeasureString(OverlayText, f, layout, strFormat, out charFit, out linesFit);
// Determine draw area based on placement
RectangleF rect = new RectangleF((bmp.Width - s.Width) / 2F,
(bmp.Height - s.Height) / 2F,
layout.Width,
((float)img.Height) * SQRTFill);
// Add rendering hint (thx to Thomas)
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// finally, draw centered text!
g.DrawString(OverlayText, f, b, rect, strFormat);
// clean-up
g.Dispose();
b.Dispose();
f.Dispose();
return bmp;
}

Dynamically resizing font to fit space while using Graphics.DrawString

Does anyone have a tip whereas you could dynamically resize a font to fit a specific area? For example, I have an 800x110 rectangle and I want to fill it with the max size font that would support the entire string I'm trying to display.
Bitmap bitmap = new Bitmap(800, 110);
using (Graphics graphics = Graphics.FromImage(bitmap))
using (Font font1 = new Font("Arial", 120, FontStyle.Regular, GraphicsUnit.Pixel))
{
Rectangle rect1 = new Rectangle(0, 0, 800, 110);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.DrawString("Billy Reallylonglastnameinstein", font1, Brushes.Red, rect1, stringFormat);
}
bitmap.Save(Server.MapPath("~/Fonts/" + System.Guid.NewGuid() + ".png"));
Obviously that whole name won't render in the space provided at the large font size. There has to be a simple way to do this?
You should do a scale transform on Font.Size the following function is an example of doing that but you can improve it to apply better results.
Here is FindFont function which get a room and a text with prefered size and gives you a font in which you can set that whole text fits the room!
// This function checks the room size and your text and appropriate font
// for your text to fit in room
// PreferedFont is the Font that you wish to apply
// Room is your space in which your text should be in.
// LongString is the string which it's bounds is more than room bounds.
private Font FindFont(
System.Drawing.Graphics g,
string longString,
Size Room,
Font PreferedFont
) {
// you should perform some scale functions!!!
SizeF RealSize = g.MeasureString(longString, PreferedFont);
float HeightScaleRatio = Room.Height / RealSize.Height;
float WidthScaleRatio = Room.Width / RealSize.Width;
float ScaleRatio = (HeightScaleRatio < WidthScaleRatio)
? ScaleRatio = HeightScaleRatio
: ScaleRatio = WidthScaleRatio;
float ScaleFontSize = PreferedFont.Size * ScaleRatio;
return new Font(PreferedFont.FontFamily, ScaleFontSize);
}
For your question you can call it like the following code:
Bitmap bitmap = new Bitmap(800, 110);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
using (Font font1 = new Font("Arial", 120, FontStyle.Regular, GraphicsUnit.Pixel))
{
Rectangle rect1 = new Rectangle(0, 0, 800, 110);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
Font goodFont = FindFont(graphics, "Billy Reallylonglastnameinstein", rect1.Size, font1);
graphics.DrawString(
"Billy Reallylonglastnameinstein",
goodFont,
Brushes.Red,
rect1,
stringFormat
);
}
I've adapted Saeed's great function to more suit my requirements. Comments explain all:
// You hand this the text that you need to fit inside some
// available room, and the font you'd like to use.
// If the text fits nothing changes
// If the text does not fit then it is reduced in size to
// make it fit.
// PreferedFont is the Font that you wish to apply
// FontUnit is there because the default font unit is not
// always the one you use, and it is info required in the
// constructor for the new Font.
public static void FindGoodFont(Graphics Graf, string sStringToFit,
Size TextRoomAvail,
ref Font FontToUse,
GraphicsUnit FontUnit)
{
// Find out what the current size of the string in this font is
SizeF RealSize = Graf.MeasureString(sStringToFit, FontToUse);
Debug.WriteLine("big string is {0}, orig size = {1},{2}",
sStringToFit, RealSize.Width, RealSize.Height);
if ((RealSize.Width <= TextRoomAvail.Width) && (RealSize.Height <= TextRoomAvail.Height))
{
Debug.WriteLine("The space is big enough already");
// The current font is fine...
return;
}
// Either width or height is too big...
// Usually either the height ratio or the width ratio
// will be less than 1. Work them out...
float HeightScaleRatio = TextRoomAvail.Height / RealSize.Height;
float WidthScaleRatio = TextRoomAvail.Width / RealSize.Width;
// We'll scale the font by the one which is furthest out of range...
float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;
float ScaleFontSize = FontToUse.Size * ScaleRatio;
Debug.WriteLine("Resizing with scales {0},{1} chose {2}",
HeightScaleRatio, WidthScaleRatio, ScaleRatio);
Debug.WriteLine("Old font size was {0}, new={1} ",FontToUse.Size,ScaleFontSize);
// Retain whatever the style was in the old font...
FontStyle OldFontStyle = FontToUse.Style;
// Get rid of the old non working font...
FontToUse.Dispose();
// Tell the caller to use this newer smaller font.
FontToUse = new Font(FontToUse.FontFamily,
ScaleFontSize,
OldFontStyle,
FontUnit);
}
This is just an update for #Saeed's FindFont function.
GraphicsUnit.Pixel needs to be added to FindFont function return line. Without GraphicsUnit.Pixel, system dpi will effect the drawn string. Problem will arise when the dpi of system and bitmap mismatches. You can see more detail in this Windows DPI setting affects Graphics.DrawString. Since GraphicsUnit of PreferedFont is already set to GraphicsUnit.Pixel and return font is not set with GraphicsUnit.Pixel. In this case text will go out of the Room dimension, if bitmap dpi is larger than system dpi and font size will go smaller than the expected size if bitmap dpi is smaller than system dpi. Here is the updated function.
private Font FindFont( System.Drawing.Graphics g , string longString , Size Room , Font PreferedFont)
{
SizeF RealSize = g.MeasureString(longString, PreferedFont);
float HeightScaleRatio = Room.Height / RealSize.Height;
float WidthScaleRatio = Room.Width / RealSize.Width;
float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;
float ScaleFontSize = PreferedFont.Size * ScaleRatio;
return new Font(PreferedFont.FontFamily, ScaleFontSize,PreferedFont.Style,GraphicsUnit.Pixel);
}
I don't want to bash against saaeds solution which is probably pretty awesome, too. But I found another one on msdn: Dynamic Graphic Text Resizing which worked for me.
public Font GetAdjustedFont(Graphics GraphicRef, string GraphicString, Font OriginalFont, int ContainerWidth, int MaxFontSize, int MinFontSize, bool SmallestOnFail)
{
// We utilize MeasureString which we get via a control instance
for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--)
{
Font TestFont = new Font(OriginalFont.Name, AdjustedSize, OriginalFont.Style);
// Test the string with the new size
SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont);
if (ContainerWidth > Convert.ToInt32(AdjustedSizeNew.Width))
{
// Good font, return it
return TestFont;
}
}
// If you get here there was no fontsize that worked
// return MinimumSize or Original?
if (SmallestOnFail)
{
return new Font(OriginalFont.Name,MinFontSize,OriginalFont.Style);
}
else
{
return OriginalFont;
}
}
Here is my solution that support wrapping.
public static Font GetAdjustedFont(Graphics graphic, string str, Font originalFont, Size containerSize)
{
// We utilize MeasureString which we get via a control instance
for (int adjustedSize = (int)originalFont.Size; adjustedSize >= 1; adjustedSize--)
{
var testFont = new Font(originalFont.Name, adjustedSize, originalFont.Style, GraphicsUnit.Pixel);
// Test the string with the new size
var adjustedSizeNew = graphic.MeasureString(str, testFont, containerSize.Width);
if (containerSize.Height > Convert.ToInt32(adjustedSizeNew.Height))
{
// Good font, return it
return testFont;
}
}
return new Font(originalFont.Name, 1, originalFont.Style, GraphicsUnit.Pixel);
}
How to use:
var font = GetAdjustedFont(drawing, text, originalfont, wrapSize);
drawing.DrawString(text, font, textBrush, new Rectangle(0, 0, wrapSize.Width, wrapSize.Height));

Categories