Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found C# code for add a text watermark to photos of a directory by
C#
FileStream
.
But I have problem with that...

- How can I coordinate the picture from bottom left(for adding an "Image watermark"(not text watermark) to the "bottom left" corner of all pictures)?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
 
namespace WatermarkImages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.folderBrowserDialog1.Description =
            "Select the images directory";
 
            // Disallow creation of new files using the FolderBrowserDialog.
            this.folderBrowserDialog1.ShowNewFolderButton = false;
        }
 
       
        private void btnWatermark_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = folderBrowserDialog1.SelectedPath;
            }
 
            if (path == String.Empty)
            {
                lblStatus.Text = "Invalid directory..";
                return;
            }
 
 
            lblStatus.Text = String.Empty;                       
            Image img = null;
            string fullPath = String.Empty;
           
            try
            {
                string[] imgExtension = { "*.jpg", "*.jpeg", ".gif", "*.bmp" };
                List<FileInfo> files = new List<FileInfo>();               
               
                DirectoryInfo dir = new DirectoryInfo(path);
               
                foreach (string ext in imgExtension)
                {
                    FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
                    foreach (FileInfo file in folder)
                    {
                        FileStream fs = file.OpenRead();
                        fullPath = path + @"\" + file.Name ;
           
                        Stream outputStream = new MemoryStream();
                        AddWatermark(fs, "www.dotnetcurry.com", outputStream);
                        fs.Close();
                        file.Delete();
                        img = Image.FromStream(outputStream);
 
                        using (Bitmap savingImage = new Bitmap(img.Width, img.Height, img.PixelFormat))
                        {
                            using (Graphics g = Graphics.FromImage(savingImage))
                                g.DrawImage(img, new Point(0, 0));
                            savingImage.Save(fullPath, ImageFormat.Jpeg);            
                           
                        }
                      
                        img.Dispose();
                       
                    }
                }
                lblStatus.Text = "Processing Completed";
            }
            catch (Exception ex)
           {
               lblStatus.Text = "There was an error during processing..";           
            }
            finally
            {
                if (img != null)
                    img.Dispose();
            }
      }
 
 
        public void AddWatermark(FileStream fs, string watermarkText, Stream outputStream)
        {
            Image img = Image.FromStream(fs);               
            Font font = new Font("Verdana", 16, FontStyle.Bold, GraphicsUnit.Pixel);
            //Adds a transparent watermark with an 100 alpha value.
            Color color = Color.FromArgb(100, 0, 0, 0);
            //The position where to draw the watermark on the image
            Point pt = new Point(10, 30);
            SolidBrush sbrush = new SolidBrush(color);
 
            Graphics gr = null;
            try
            {
                gr = Graphics.FromImage(img);
            }
            catch
            {
                // http://support.microsoft.com/Default.aspx?id=814675
                Image img1 = img;
                img = new Bitmap(img.Width, img.Height);
                gr = Graphics.FromImage(img);
                gr.DrawImage(img1, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                img1.Dispose();
            }
                       
            gr.DrawString(watermarkText, font, sbrush, pt);
            gr.Dispose();
           
            img.Save(outputStream, ImageFormat.Jpeg);
        }
    
    }
}
Posted
Updated 23-Mar-19 3:05am

1 solution

Two things: you can't "add a watermark to a directory", you would have to add it to each individual picture separately, and you have code for that.

But the other things is that you are reading and writing JPEG files, and JPEG is a "lossy compression" format - which means that each time you write the file, you throw away information and the image quality is degraded - it doesn't take many read-write cycles to get a very poor image indeed.

You would be much, much better off applying your watermark when you originally save the file, and using a non-lossy format such as GIF or PNG instead of JPEG.
 
Share this answer
 
Comments
Member 14190402 23-Mar-19 9:52am    
The code I sent here adding text watermark to directory successfully by using filestream. It's Ok.
I just want to find the resolution of any pictures in the directory one by one and insert watermark to bottom left.
finding images height and weight will work. but i don't know how find height and weight from filestream.
OriginalGriff 23-Mar-19 11:02am    
You load the file stream into an Image - and that Image has Width and Height properties which tell you how big it is ... so why do you not use them?
Member 14190402 23-Mar-19 11:40am    
actually I don't know how should I use them, and where to add the code.
please tell me if you know that exactly. thanks.
OriginalGriff 23-Mar-19 11:50am    
You wrote the code and you don't know how it works?

Oh, come on ...

Look at teh code, and see where you create savingImage - it even uses the width and height of the image in that line!
Member 14190402 23-Mar-19 12:22pm    
I just found the code on the Internet.
and I'm really confused about this one

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900