News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Nov 30 2014 8:26AM     Prashanth Krish
  0 Comments    3314 Views  
Here Prashanth Krish provided a short tutorial with example, how to create Image watermark in Windows Forms applicaton C#.
Create a new windows forms application and do the steps given below to create a Menu
Adding a Controls to the Windows Form
We have to drag and drop MenuStrip, OpenFileDialog, SaveFileDialog, ColorDialog and FontDialog to the windows forms
  1. MenuStrip - Used to create Menu
  2. FontDialog - Used to change the font size, font and etc
  3. OpenFileDialog - Used to open files
  4. SaveFileDialog - Used to Save files
  5. ColorDialog - Used to set Colors
The image given below shows the control, which we have to drag and drop to the windows forms application C#










After placing all the controls, Below your form it displays like this.


Form Design
Design a form as shown below,
Menu Design
  1. File -> Open and Exit
Namespaces
You will need to import the following namespace.
C#

using System.Drawing.Imaging;

To create a Image WaterMark in C#
Below is the sample code for creating a Image WaterMarking in C#.
C#
Declare the variables globaly and Create a constructor [WaterMark()]

public partial class WaterMark : Form
{
        string CurrentFile;
        Image img;       
        System.Drawing.Color myWatermarkColor;
        System.Drawing.Font myFont;

        public WaterMark()
        {
            InitializeComponent();

            myWatermarkColor = Color.SteelBlue;
            comboBoxOpacity.SelectedIndex = 2;
            rdTop.Checked = true;
            txtboxWaterMark.Text = "Your Name " + char.ConvertFromUtf32(169).ToString() + " " + 
            DateTime.Now.Year.ToString() + ", All Rights Reserved";
            myFont = txtboxWaterMark.Font;
        }
}

To Open a Image file

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
          // Filter Common Usable Formats
            openFileDialog1.Title = "Open Image File";
            openFileDialog1.Filter = "Bitmap Files|*.bmp" +
                "|Enhanced Windows MetaFile|*.emf" +
                "|Exchangeable Image File|*.exif" +
                "|Gif Files|*.gif|JPEG Files|*.jpg" +
                "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
            openFileDialog1.DefaultExt = "bmp";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();

            // If the user did not select a file, return
            if (openFileDialog1.FileName == "")
                return;

            // Update the current file and form caption text
            CurrentFile = openFileDialog1.FileName.ToString();
            this.Text = "Watermark Utility: " + CurrentFile.ToString();

            try
            {
                // Open the image into the picture box
                img = Image.FromFile(openFileDialog1.FileName, true);
                pictureBox.Image = img;

                // Resize the picture box to support scrolling large images                
                pictureBox.Size = img.Size;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "File Open Error");
            }
}

To Change the font or color of the text

private void btnFont_Click(object sender, EventArgs e)
{
            // Default font and color applied to the textbox 
            fontDialog1.ShowColor = true;
            fontDialog1.Font = txtboxWaterMark.Font;
            fontDialog1.Color = txtboxWaterMark.ForeColor;

            if (fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                myFont = fontDialog1.Font;
                myWatermarkColor = fontDialog1.Color;
                txtboxWaterMark.Font = fontDialog1.Font;
                txtboxWaterMark.ForeColor = fontDialog1.Color;
            }
}

To Preview

private void btnPreview_Click(object sender, EventArgs e)
{
            // Reload the image in PictureBox
            pictureBox.Image = Image.FromFile(CurrentFile);

            int opac = 0;
            string sOpacity = comboBoxOpacity.Text;

            // Set the opacity level
            switch (sOpacity)
            {
                case "100%":
                    opac = 255; // 1 * 255
                    break;
                case "75%":
                    opac = 191; // .75 * 255
                    break;
                case "50%":
                    opac = 127; // .5 * 255
                    break;
                case "25%":
                    opac = 64;  // .25 * 255
                    break;
                case "10%":
                    opac = 25;  // .10 * 255
                    break;
                default:
                    opac = 127; // default at 50%; .5 * 255
                    break;
            }

            // Get a graphics context
            Graphics g = Graphics.FromImage(pictureBox.Image);

            // Create a solid brush to write the watermark text on the image
            Brush myBrush = new SolidBrush(Color.FromArgb(opac, myWatermarkColor));

            // Calculate the size of the text
            SizeF sz = g.MeasureString(txtboxWaterMark.Text, myFont);
            
            // drawing position (X,Y)
            int X;
            int Y;
           
             //Set the drawing position based on the users
             //selection of placing the text at the bottom or top of the image
            if (rdTop.Checked == true)
            {
                X = (int)(pictureBox.Image.Width - sz.Width) / 2;
                Y = (int)(pictureBox.Top + sz.Height) / 2;
            }
            else 
            {
                X = (int)(pictureBox.Image.Width - sz.Width) / 2;
                Y = (int)(pictureBox.Image.Height - sz.Height);
            }           

            // draw the water mark text
            g.DrawString(txtboxWaterMark.Text, myFont, myBrush, new Point(X, Y));
}

To Save

private void btnSave_Click(object sender, EventArgs e)
{
            try
            {                
                string strExt;
                strExt = System.IO.Path.GetExtension(CurrentFile);
                strExt = strExt.ToUpper();
                strExt = strExt.Remove(0, 1);

                // if the current image is, for example, a JPG, only
                // allow the user to save the file with the watermark
                // as a JPG
                SaveFileDialog1.Title = "Save File";
                SaveFileDialog1.DefaultExt = strExt;
                SaveFileDialog1.Filter = strExt + " Image Files|*." + strExt;
                SaveFileDialog1.FilterIndex = 1;

                if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (SaveFileDialog1.FileName == "")
                    {
                        return;
                    }
                    else
                    {
                        // save the file with the name supplied by the user
                        pictureBox.Image.Save(SaveFileDialog1.FileName);
                    }

                    // update the current image file to point to the newly saved
                    // image
                    CurrentFile = SaveFileDialog1.FileName;
                    this.Text = "Watermark Utility: " + CurrentFile;
                    MessageBox.Show(CurrentFile.ToString() + " saved.""File Save");
                }
                else
                {
                    MessageBox.Show("The save file request was cancelled by user.""Save Cancelled");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Image Save Error");
            }
}

To Exit

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
            this.Dispose();
}  

Result
I hope this page will helps to create Image Watermark in windows Forms Application in C#. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: