c# Graphics and GraphicsPath Alignment - c#

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

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;
}

Sporadical weird text while writing on bitmap

I am looking for some explanation about some weird texts that appears when I write on a bitmap and then upload it to azure blob.
Sometimes when I create an imagem, it comes with theses weird texts where there should be accents. But I recreate it right after and it works...
Here is the imagem gone wrong: https://www.dropbox.com/s/8an62ygys5nnwow/uploaded-image-636488352376069747.png?dl=0
And here is the correct image:
https://www.dropbox.com/s/q3sdsqo3d05skz8/uploaded-image-636493880034621618.png?dl=0
I convert the image url to bitmap:
var request = System.Net.WebRequest.Create(url);
var response = request.GetResponse();
using (var responseStream = response.GetResponseStream())
{
var bitmap = new Bitmap(responseStream);
return bitmap;
}
And then I write the text on it:
private void AddText(System.Drawing.Image image, string text, int x, int y, int width, int height, int fontSize, Color fontColor, Color borderColor, FontFamily fontFamily, FontStyle fontStyle, StringAlignment horizontalAligment, StringAlignment verticalAligment, bool autoFit, int shadowOffsetX, int shadowOffsetY, Color shadowColor, int? maxHeight, out float finalHeight, out float finalWidth)
{
var graphics = Graphics.FromImage(image);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
var rect = new Rectangle(x, y, width, height);
StringFormat stringFormat = new StringFormat
{
Alignment = horizontalAligment,
LineAlignment = verticalAligment
};
var font = new System.Drawing.Font(fontFamily, graphics.DpiY * fontSize / 72, fontStyle);
if (autoFit)
{
font = FindFont(graphics, text, rect.Size, font);
}
var hasShadow = shadowOffsetX > 0 && shadowOffsetY > 0;
var hasMaxHeight = maxHeight != null && maxHeight.Value > 0;
if (hasShadow)
{
var shadowGraphicsPath = new GraphicsPath();
var shadowRect = new Rectangle(x + shadowOffsetX, y + shadowOffsetY, width, height);
shadowGraphicsPath.AddString(text, fontFamily, (int)fontStyle, font.Size, shadowRect, stringFormat);
if (hasMaxHeight)
{
shadowGraphicsPath = GetTextWithMaxHeight(text, fontFamily, fontStyle, shadowRect, stringFormat, shadowGraphicsPath, font, maxHeight.Value, out float newFontSize);
font = new System.Drawing.Font(font.FontFamily, newFontSize);
}
graphics.FillPath(new SolidBrush(shadowColor), shadowGraphicsPath);
graphics.Flush();
shadowGraphicsPath.Dispose();
}
var graphicsPath = new GraphicsPath();
graphicsPath.AddString(text, fontFamily, (int)fontStyle, font.Size, rect, stringFormat);
if (hasMaxHeight && !hasShadow)
{
graphicsPath = GetTextWithMaxHeight(text, fontFamily, fontStyle, rect, stringFormat, graphicsPath, font, maxHeight.Value, out float newFontSize);
}
graphics.DrawPath(new Pen(borderColor), graphicsPath);
graphics.FillPath(new SolidBrush(fontColor), graphicsPath);
finalHeight = graphicsPath.GetBounds().Height;
finalWidth = graphicsPath.GetBounds().Width;
graphics.Flush();
graphicsPath.Dispose();
}
And then I upload to azure blob
var container = _blobClient.GetContainerReference(_imageConfiguration.BlobContainer);
var blockBlob = container.GetBlockBlobReference($"uploaded-image-{DateTime.Now.ToUniversalTime().Ticks}.png");
using (var imageStream = ConvertToStream(image, ImageFormat.Png))
{
blockBlob.UploadFromStream(imageStream);
var uri = blockBlob.Uri;
return uri;
}
Can someone help me find an explanation for this?

Drawing to a new "layer" in C#

Building a little paint program and am trying to incorporate the concept of layers.
I'm using a PictureBox control to display the image, and getting the Graphics object from the image being displayed by the PictureBox and drawing to that.
My problem is I'm trying to figure out how to draw to a new Graphics object that is overlayed on top of the picture box, and be able to get the newly drawn image without the original image absorbed into the graphic.
If I do something like:
Graphics gr = Graphics.FromImage(myPictureBox.image);
gr.DrawRectangle(blah blah)
...I am editing the original image in the picture box. I want a way to only capture the new stuff being drawn as a separate image, but still have it displayed as an overlay over top of what was already there.
Anyone able to point me in the right direction? Thanks!
I would reckon to use the transparent control and do some modification so it can be used as image layers:
http://www.codeproject.com/Articles/26878/Making-Transparent-Controls-No-Flickering
Probably something like this (make any modification as necessary).
class LayerControl : UserControl
{
private Image image;
private Graphics graphics;
public LayerControl(int width, int height)
{
this.Width = width;
this.Height = height;
image = new Bitmap(width, height);
graphics = Graphics.FromImage(image);
// Set style for control
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
}
// this function will draw your image
protected override void OnPaint(PaintEventArgs e)
{
var bitMap = new Bitmap(image);
// by default the background color for bitmap is white
// you can modify this to follow your image background
// or create a new Property so it can dynamically assigned
bitMap.MakeTransparent(Color.White);
image = bitMap;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.GammaCorrected;
float[][] mtxItens = {
new float[] {1,0,0,0,0},
new float[] {0,1,0,0,0},
new float[] {0,0,1,0,0},
new float[] {0,0,0,1,0},
new float[] {0,0,0,0,1}};
ColorMatrix colorMatrix = new ColorMatrix(mtxItens);
ImageAttributes imgAtb = new ImageAttributes();
imgAtb.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
g.DrawImage(image,
ClientRectangle,
0.0f,
0.0f,
image.Width,
image.Height,
GraphicsUnit.Pixel,
imgAtb);
}
// this function will grab the background image to the control it self
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (Parent != null)
{
BackColor = Color.Transparent;
int index = Parent.Controls.GetChildIndex(this);
for (int i = Parent.Controls.Count - 1; i > index; i--)
{
Control c = Parent.Controls[i];
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
Bitmap bmp = new Bitmap(c.Width, c.Height, g);
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
bmp.Dispose();
}
}
}
else
{
g.Clear(Parent.BackColor);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, Color.Transparent)), this.ClientRectangle);
}
}
// simple drawing circle function
public void DrawCircles()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Green, 3))
{
this.graphics.DrawEllipse(p, 25, 25, 20, 20);
}
}
}
// simple drawing rectable function
public void DrawRectangle()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Red, 3))
{
this.graphics.DrawRectangle(p, 50, 50, 40, 40);
}
}
}
// Layer control image property
public Image Image
{
get
{
return image;
}
set
{
image = value;
// this will make the control to be redrawn
this.Invalidate();
}
}
}
Example how to use it:
LayerControl lc = new LayerControl(100, 100);
lc.Location = new Point(0, 0);
lc.DrawRectangle();
LayerControl lc2 = new LayerControl(100, 100);
lc2.Location = new Point(0, 0);
lc2.DrawCircles();
LayerControl lc3 = new LayerControl(100, 100);
lc3.Location = new Point(0, 0);
lc3.Image = new Bitmap(#"<Image Path>");
// adding control
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);
With this method you can have multiple layers that can put overlapping each other (due to the transparency feature it has).
If you want to add it in top of your PictureBox make sure to re-order the control. The Layer Control should be added before your PictureBox control.
// adding control
this.Controls.Clear();
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);
this.Controls.Add(PictureBox1);
Hopefully it help.
example code which working fine - take dummy image and layered the original image with custom text
public void LayerImage(System.Drawing.Image Current, int LayerOpacity)
{
Bitmap bitmap = new Bitmap(Current);
int h = bitmap.Height;
int w = bitmap.Width;
Bitmap backg = new Bitmap(w, h + 20);
Graphics g = null;
try
{
g = Graphics.FromImage(backg);
g.Clear(Color.White);
Font font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
RectangleF rectf = new RectangleF(70, 90, 90, 50);
Color color = Color.FromArgb(255, 128, 128, 128);
Point atpoint = new Point(backg.Width / 2, backg.Height - 10);
SolidBrush brush = new SolidBrush(color);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("BRAND AMBASSADOR", font, brush, atpoint, sf);
g.Dispose();
MemoryStream m = new MemoryStream();
backg.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch { }
Color pixel = new Color();
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
pixel = bitmap.GetPixel(x, y);
backg.SetPixel(x, y, Color.FromArgb(LayerOpacity, pixel));
}
}
MemoryStream m1 = new MemoryStream();
backg.Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg);
m1.WriteTo(Response.OutputStream);
m1.Dispose();
base.Dispose();
}
Got it working, perhaps I wasn't clear enough in my original question.
Essentially what I ended up doing was storing each layer as a separate Image object, then just hooking into the OnPaint method of my control and manually drawing the graphics in order, instead of just drawing to PictureBox.Image. Works like a charm!
The graphics capabilities of .NET drawing libraries are simple. Their main purpose is direct drawing of GUI. If you want to have layering, alpha transparency or advanced filters, then you should either use 3rd party library or roll your own drawing code.

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

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.

Font is very ugly

I'm drawing a string with the folowing code:
public Image DrawString(String lString)
{
Image lImage = new Bitmap(128, 128);
Rectangle rec = new Rectangle(0, 0, lImage.Width, lImage.Height);
Graphics g = Graphics.FromImage(lImage);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 20, FontStyle.Regular);
font = FindBestFitFont(g, lString, font, rec.Size);
g.DrawString(lString, font, Brushes.Red, rec, drawFormat);
return lImage;
}
The font looks very ugly even when i use:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
Is there a way to make the font more smooth?
Try
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
instead.
Have you tried AntiAliasing or ClearType?
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

Categories