• We just launched and are currently in beta. Join us as we build and grow the community.

Adding Watermark to Picture using C#

grom2809

SaaS Builder
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
45
Likes
52
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we will create a program that adds a watermark to an image/picture using C#. We all know that watermark is a faint picture or mark made in some paper during manufacture, which is visible when held against the light and typically identifies the maker, it a special design contained in electronic documents, pictures, music etc that is used to stop people from copying them.

Follow the following steps to create the program:

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Add a Watermark.

2. Next, add one PictureBox named PictureBox1 and upload your desired image. Add a TextBox named TextBox1 so that the text that we have inputted from out TextBox will be displayed as our watermark in the PictureBox. Then add a Button named Button1. Design your layout like the image below:

waterdesign.png


3. Now, put this code in Button1_Click. This will trigger to put the text you have inputted in your TextBox to be displayed in your PictureBox as your Watermark.

Initialize variable font that contains a font type of Arial, font size of 36, and font style of italic.
  1. Font font =

    new

    Font(

    "Arial"

    , 36

    , FontStyle.

    Italic

    )

    ;

We will use the SolidBrush class that the brush variable will be held. This SolidBrush class defines a brush of a single color and we access the argb color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths. This class cannot be inherited.
  1. SolidBrush brush =

    new

    SolidBrush(

    Color.

    FromArgb

    (

    64

    , 192

    , 255

    , 255

    )

    )

    ;

Have this color from our RGB color of (128, 0, 0, 0).
  1. brush.

    Color

    =

    Color.

    FromArgb

    (

    128

    , 0

    , 0

    , 0

    )

    ;

Create a method named CreateGraphics().DrawString() of our picturebox. This will create a watermark image as it draws the string in our inputted textbox, the font we have initialized and its brush, the left location of 15, and the top location of 135.
  1. PictureBox1.

    CreateGraphics

    (

    )

    .

    DrawString

    (

    TextBox1.

    Text

    , font, brush, 15

    , 135

    )

    ;


Full source code:

  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Data

    ;
  7. using

    System.Collections.Generic

    ;



  8. namespace

    Add_a_Watemark
  9. {
  10. public

    partial

    class

    Form1
  11. {
  12. public

    Form1(

    )
  13. {
  14. InitializeComponent(

    )

    ;

  15. }


  16. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  17. {
  18. Font font =

    new

    Font(

    "Arial"

    , 36

    , FontStyle.

    Italic

    )

    ;
  19. SolidBrush brush =

    new

    SolidBrush(

    Color.

    FromArgb

    (

    64

    , 192

    , 255

    , 255

    )

    )

    ;
  20. brush.

    Color

    =

    Color.

    FromArgb

    (

    128

    , 0

    , 0

    , 0

    )

    ;
  21. PictureBox1.

    CreateGraphics

    (

    )

    .

    DrawString

    (

    TextBox1.

    Text

    , font, brush, 15

    , 135

    )

    ;
  22. }
  23. }

  24. }

Press F5 to Run the program.


Output:

wateroutput.png


Download the source code below and try it! :)

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top