Adding copyright to an image in C# - strange behaviour

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

Answers


It's necessary to use GraphicsUnit.Pixel

var font = new Font("Comic Sans MS", (float)fontSize, FontStyle.Bold, GraphicsUnit.Pixel);

The reason is, the constructor for Font that doesn't take a GraphicsUnit parameter, will use GraphicsUnit.Point.

This relates to the DPI information which for some image formats is stored in the image (for others it uses defaults from the system). Points are relative to inches, so different DPIs for the image means different sizes in pixels. Therefore different images end up having the text in different sizes. This would be wonderful if we wanted to print the images according to their DPI information and have the same size text in each printout. For on-screen use to be consistent, it's pixel-sizes that are important, so we make that explicit.


Need Your Help

MySQL pro rata counting of distinct values in last period

mysql count sum distinct

This one is quite tricky i've been scratching my head all day.

C# serial port, simulation of serialport write

c# serial-port emulator simulation com0com

I have a c# app that connects pc to a device, through a serial port.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.