I'm trying to add copyright to an image. If the resolution of image is less than 4592x2576, it works as I want. But in second case (in this case if the resolution is equals 4592x2576) it adds copyright too big size.
Look attachments for details.
class Program
{
private static string file5 = "d:\\DSC01305.JPG";
private static string file6 = "d:\\DSC01427.JPG";
static void Main(string[] args)
{
AddCopyrightWithText(file5);//good
AddCopyrightWithText(file6);//not good
}
private const string CopyrightText = "mysite.com";
private const int MaxFontSize = 190;
const int coefficient = 20;
public static void AddCopyrightWithText(string fileName)
{
using (var img = Image.FromFile(fileName))
{
using (var gr = Graphics.FromImage(img))
{
var color = Color.FromArgb(90, 241, 235, 105);
int fontSize = img.Width / coefficient;
if (fontSize > MaxFontSize)
fontSize = MaxFontSize;
var font = new Font("Comic Sans MS", (float)fontSize, FontStyle.Bold);
var stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
gr.SmoothingMode = SmoothingMode.AntiAlias;
int y = img.Height - (int)gr.MeasureString(CopyrightText, font, int.MaxValue).Height;
int x = img.Width / 2;
gr.DrawString(CopyrightText, font, new SolidBrush(color), new Point(x, y), stringFormat);
}
using (var ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
img.Dispose();
File.Delete(fileName);
var file = new FileStream(fileName, FileMode.Create, FileAccess.Write);
ms.Seek(0, SeekOrigin.Begin);
ms.WriteTo(file);
file.Close();
file.Dispose();
}
}
}
}
In the second case if I set fontSize = 182 manually (becase in the first case fontSize is equals 182), there is no effect, the result is the same!
How do I fix it?
P.S. First and second attachments show the result I want, third and fourth shows the bug. Pay attention in third attachment I changed the fontSize manually from 190 to 182.
Original image below
Original image below
Original image below
Original image below
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)