{"id":2222,"date":"2015-09-03T13:58:42","date_gmt":"2015-09-03T13:58:42","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2222"},"modified":"2015-09-03T13:58:42","modified_gmt":"2015-09-03T13:58:42","slug":"watermark-an-image-in-c","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/03\/watermark-an-image-in-c\/","title":{"rendered":"Watermark an image in C#"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushCSharp.js\"><\/script>\n<p>Function to add a text watermark in C#. It is a generic function which can be used in either a desktop application or an ASP.NET website. It works with JPG and PNG files.<\/p>\n<p>GIF files are also processed without any errors or Exceptions but no watermark is added. This is due to the common issue &#8220;<em>A Graphics object cannot be created from an image that has an indexed pixel format.<\/em>&#8221; There are ways around this problem by first converting the image object into a generic bitmap, doing the transformations and then saving it as\u00a0 GIF, but somehow that approach didnt work either. If anyone has a solution to it, I will be glad to modify the code to incorporate it.<\/p>\n<p>The test images are shown below:<\/p>\n<h4>Example JPG Image<\/h4>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/clouds.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2224\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/clouds-620x465.jpg\" alt=\"clouds\" width=\"620\" height=\"465\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/clouds-620x465.jpg 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/clouds-300x225.jpg 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/clouds.jpg 626w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2225\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked-620x465.jpg\" alt=\"watermarked\" width=\"620\" height=\"465\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked-620x465.jpg 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked-300x225.jpg 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked.jpg 626w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>Example PNG image<\/h4>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/house.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2226\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/house.png\" alt=\"house\" width=\"600\" height=\"400\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/house.png 600w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/house-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2227\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked.png\" alt=\"watermarked\" width=\"600\" height=\"400\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked.png 600w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/watermarked-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The code is given below:<br \/>\n<pre class=\"brush: csharp\">using System.Drawing;\r\nusing System.Drawing.Imaging;\r\nusing System.IO;\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Add a text watermark to an image\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;sourceImage&quot;&gt;path to source image&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;text&quot;&gt;text to add as a watermark&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;targetImage&quot;&gt;path to the modified image&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;fmt&quot;&gt;ImageFormat type&lt;\/param&gt;\r\n        public void watermarkImage(string sourceImage, string text, string targetImage, ImageFormat fmt) {\r\n\r\n            try\r\n            {\r\n                \/\/ open source image as stream and create a memorystream for output\r\n                FileStream source = new FileStream(sourceImage, FileMode.Open);\r\n                Stream output = new MemoryStream();\r\n                Image img = Image.FromStream(source);\r\n\r\n                \/\/ choose font for text\r\n                Font font = new Font(&quot;Arial&quot;, 20, FontStyle.Bold, GraphicsUnit.Pixel);\r\n\r\n                \/\/choose color and transparency\r\n                Color color = Color.FromArgb(100, 255, 0, 0);\r\n\r\n                \/\/location of the watermark text in the parent image\r\n                Point pt = new Point(10, 5);\r\n                SolidBrush brush = new SolidBrush(color);\r\n\r\n                \/\/draw text on image\r\n                Graphics graphics = Graphics.FromImage(img);\r\n                graphics.DrawString(text, font, brush, pt);\r\n                graphics.Dispose();\r\n\r\n                \/\/update image memorystream\r\n                img.Save(output, fmt);\r\n                Image imgFinal = Image.FromStream(output);\r\n                \r\n                \/\/write modified image to file\r\n                Bitmap bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);\r\n                Graphics graphics2 = Graphics.FromImage(bmp);\r\n                graphics2.DrawImage(imgFinal, new Point(0, 0));\r\n                bmp.Save(targetImage, fmt);\r\n\r\n                imgFinal.Dispose();\r\n                img.Dispose();\r\n\r\n\r\n\r\n            }\r\n            catch (Exception ex) {\r\n                MessageBox.Show(ex.Message);\r\n            }\r\n        }\r\n\r\n        private void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            string sourceImage = &quot;d:\\\\junk\\\\Watermark\\\\clouds.jpg&quot;;\r\n            string targetImage = &quot;d:\\\\junk\\\\watermark\\\\watermarked.jpg&quot;;\r\n            watermarkImage(sourceImage, &quot;This is a watermark&quot;, targetImage, ImageFormat.Jpeg);\r\n\r\n            sourceImage = &quot;d:\\\\junk\\\\Watermark\\\\house.png&quot;;\r\n            targetImage = &quot;d:\\\\junk\\\\watermark\\\\watermarked.png&quot;;\r\n            watermarkImage(sourceImage, &quot;This is a watermark&quot;, targetImage, ImageFormat.Png);\r\n            \r\n            sourceImage = &quot;d:\\\\junk\\\\Watermark\\\\garden.gif&quot;;\r\n            targetImage = &quot;d:\\\\junk\\\\watermark\\\\watermarked.gif&quot;;\r\n            watermarkImage(sourceImage, &quot;This is a watermark&quot;, targetImage, ImageFormat.Gif);\r\n\r\n\r\n        }\r\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Function to add a text watermark in C#. It is a generic function which can be used in either a desktop application or an ASP.NET <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/03\/watermark-an-image-in-c\/\" title=\"Watermark an image in C#\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,15],"tags":[],"class_list":["post-2222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-windows"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2222"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2222\/revisions"}],"predecessor-version":[{"id":2229,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2222\/revisions\/2229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2219"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}