in Education by
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)

1 Answer

0 votes
by
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.

Related questions

0 votes
    I'm trying to add copyright to an image. If the resolution of image is less than 4592x2576, it ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    i want to add image inside a span here's what i tried (inside a form i added this script): ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    Which is the correct HTML syntax for adding a link to the image? 1. ... xyz.com/image”>...
asked Jul 12, 2021 in Education by JackTerrance
0 votes
    What is the best way to add a cascading stylesheet (CSS) to a web application project in visual studio ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 14, 2022 in Education by JackTerrance
0 votes
    The primary source code copyright for R is held by the ___________ (a) R Foundation (b) S Foundation (c) ... Started of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Strange binary file generated from machines is an example of tidy data. (a) True (b) False I have ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
0 votes
    im developing a c# application (Photo Album Viewer), Im having Picturebox (Displays the selected image in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I am using C#.Net to send out a HTML email which contains embedded images. These emails work fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am using C#.Net to send out a HTML email which contains embedded images. These emails work fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I am newbie to android development and learning it to my own. I have a very strange problem of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I am newbie to android development and learning it to my own. I have a very strange problem of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    in my project i created a class viewTransistion #import "viewTransition.h" #import "quiz.h" #import "mode.h ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    2. Why did Tommy & Margie find the printed book strange? Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    It is very important to block unknown, strange and ______________ within the corporate network. (a) infected sites (b) ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 3, 2021 in Education by JackTerrance
0 votes
    An instar can respond to a set of input vectors even if its not trained to capture the behaviour of the ... Neural Networks of Neural Networks Please answer the above question....
asked Aug 27, 2022 in Education by JackTerrance
...