Tuesday, October 6, 2009

 

Watermark and Add Copyright Info to your Images on your Local drive using C# and VB.NET

collected from http://www.dotnetcurry.com/ShowArticle.aspx?ID=243&AspxAutoDetectCookieSupport=1


Prevention is better than cure! Most of us, who run websites/blogs or host technical content, face issues of our images being copied and displayed on other blogs. There are a couple of techniques we can adopt to safeguard our copyrighted content and prevent people from doing so. One of them is watermarking our images with our logo or watermarking the website name on the images. A few weeks ago, I had discussed one such technique using ASP.NET. In this article, I will propose a desktop solution using Windows Forms, which can be used to watermark images before you upload the images to the site. This desktop solution basically saves us some processing and response time while watermarking images. Let us get started.
Create a Windows application. Drop a Button control (btnWatermark), a label(lblStatus) and a FolderBrowserDialog component from the ToolBox to the Form.
In the button click, open a dialog box using the FolderBrowerDialog component, which enables the user to select a folder containing images. We then loop through all images in that folder, create a filestream object and then pass it to the AddWatermark(). The AddWatermark method has been taken from a code sample posted by Rong-Chun over here
The AddWatermark accepts a FileStream and then draws the watermark on the image using the Graphics class. Here’s the code to do draw watermark on your images.


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 files = new List();

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);
}

}
}

VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO
Imports System.Drawing.Imaging

Namespace WatermarkImages
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Me.folderBrowserDialog1.Description = "Select the images directory"

' Disallow creation of new files using the FolderBrowserDialog.
Me.folderBrowserDialog1.ShowNewFolderButton = False
End Sub
Private Sub btnWatermark_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim path As String = String.Empty
Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
If result = DialogResult.OK Then
path = folderBrowserDialog1.SelectedPath
End If

If path = String.Empty Then
lblStatus.Text = "Invalid directory.."
Return
End If


lblStatus.Text = String.Empty
Dim img As Image = Nothing
Dim fullPath As String = String.Empty

Try
Dim imgExtension() As String = { "*.jpg", "*.jpeg", ".gif", "*.bmp" }
Dim files As List(Of FileInfo) = New List(Of FileInfo)()

Dim dir As New DirectoryInfo(path)

For Each ext As String In imgExtension
Dim folder() As FileInfo = dir.GetFiles(ext, SearchOption.AllDirectories)
For Each file As FileInfo In folder
Dim fs As FileStream = file.OpenRead()
fullPath = path & "\" & file.Name

Dim outputStream As Stream = New MemoryStream()
AddWatermark(fs, "www.dotnetcurry.com", outputStream)
fs.Close()
file.Delete()
img = Image.FromStream(outputStream)

Using savingImage As New Bitmap(img.Width, img.Height, img.PixelFormat)
Using g As Graphics = Graphics.FromImage(savingImage)
g.DrawImage(img, New Point(0, 0))
End Using
savingImage.Save(fullPath, ImageFormat.Jpeg)

End Using

img.Dispose()

Next file
Next ext
lblStatus.Text = "Processing Completed"
Catch ex As Exception
lblStatus.Text = "There was an error during processing.."
Finally
If img IsNot Nothing Then
img.Dispose()
End If
End Try
End Sub


Public Sub AddWatermark(ByVal fs As FileStream, ByVal watermarkText As String, ByVal outputStream As Stream)
Dim img As Image = Image.FromStream(fs)
Dim font As New Font("Verdana", 16, FontStyle.Bold, GraphicsUnit.Pixel)
'Adds a transparent watermark with an 100 alpha value.
Dim color As Color = Color.FromArgb(100, 0, 0, 0)
'The position where to draw the watermark on the image
Dim pt As New Point(10, 30)
Dim sbrush As New SolidBrush(color)

Dim gr As Graphics = Nothing
Try
gr = Graphics.FromImage(img)
Catch
' http://support.microsoft.com/Default.aspx?id=814675
Dim img1 As Image = 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()
End Try

gr.DrawString(watermarkText, font, sbrush, pt)
gr.Dispose()

img.Save(outputStream, ImageFormat.Jpeg)
End Sub

End Class
End Namespace

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]