Click here to Skip to main content
15,895,011 members
Articles / Multimedia / GDI+
Article

Stamp Images Using VB.NET GDI+

Rate me:
Please Sign up or sign in to vote.
1.35/5 (12 votes)
22 Aug 2007Ms-PL2 min read 48.7K   877   20   3
An article on image stamping with VB.NET GDI+
Screenshot - ScreenStamp.jpg

Introduction

Recently I came across the task of stamping image(s) without disturbing the image's contents at all. I was wondering about how I could do it. I searched a lot, but did not find a solution exactly how I wanted. So, after studying about the GDI+ library and spending some time and effort, I got my own solution. I think this code can help others who have the same or similar kinds of tasks on their hands. This code helps to stamp an image without disturbing its original contents. A stamp can be a blank image or an image with text written on it.

Using the Code

There are many classes like Bitmap, Image, Graphics, Font, Brush, etc. to perform graphics operations using the .NET framework. To simplify the image-stamping process, let's look at the code that demonstrates how to stamp images.

Creating a stamp is as simple as creating the Bitmap object itself by specifying its height and width. This task involves only three steps:

  1. Create the Bitmap object
  2. Create the Graphics object using the Bitmap object
  3. Use the DrawString() method of the Graphics class

VB.NET
'Create Bitmap object of specific width and height
Dim ImgStamp As New Bitmap(intWidth, intHeight)

'Obtain Graphics object to perform graphics opration
Dim g As Graphics = Graphics.FromImage(ImgStamp)

'Use drawString method of the graphics object to write text on target bitmap
g.DrawString("Test", New Font("Arial", 15, FontStyle.Bold), _
    New SolidBrush(Color.Red), 25, 35)

'Save this bitmap using its save method as .Tiff,.jpg or any other image
ImgStamp.Save(YourPath & "\MyStamp.Tiff" )

'You can optionally specify the ImageFormat using ImageFormat Class
ImgStamp.Save(YourPath & "\MyStamp.Tiff",_
    System.Drawing.Imaging.ImageFormat.Tiff)

The DrawString() method takes Font and Brush objects to draw the text on the Bitmap. Here you can create your own Font object by using FontDialog's properties like fontName, size and style in same way you can create a Brush object using the Color object. The last two parameters are a starting X and Y position for the text to be written on the Bitmap. Here's the code to stamp the original image:

VB.NET
'Construct a bitmap object using original image so
'you can draw this object on the target bitmap
Dim OrgImg As New Bitmap(strImagePath)

'Construct a bitmap object for the stamp image
Dim StampImg As New Bitmap(StampImagePath & "\MyStamp.Tiff")

'Create target bitmap to draw both original image and stamp image.
Dim TarImg As New Bitmap(OrgImg.Width, OrgImg.Height + StampImg.Height)

'Obtain a Graphics object from & for that Bitmap
Dim gr As Graphics = Graphics.FromImage(TarImg)

'Draw the original image first on the target bitmap using graphics object
gr.DrawImage(OrgImg, 0, 0, OrgImg.Width, OrgImg.Height)

'Now draw the stamp image on the target bitmap using graphics object
gr.DrawImage(StampImg, 0, OrgImg.Height, StampImg.Width, StampImg.Height)

'Save the target bitmap as image file
TarImg.Save(YourPath & "\MyStamppedImage.Tiff")

The above code will create the new image file named MyStamppedImage.jpg with original content and additional stamp image pasted below, without disturbing content of original image file.

Points of Interest

This code creates only one stamp at the end of the content of the original image, but you can create more than one stamp on the same image and you can even create a stamp on top of the original content of the image. You can use the Color object to create different colored stamps. By using the Color.FromArgb() method, you can make any color to create background and foreground colors for the stamp. You can even compress the original or stamp image for further clarity.

History

  • 20 August, 2007 -- Original version posted
  • 22 August, 2007 -- Updated version of demo project

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Web Developer
Canada Canada
Tejas has been an ASP.NET,AJAX/C#.NET/VB.NET developer for about 10 years for windows, WCF, RESTful services, windows and web application. Currently, he is working on Distributed Applications as a team lead in his company. He has worked with languages like C,C++,Java,PHP and databases like MS SQL Server, Oracle and MySQL. He especially enjoy web development using service driven architectures. He holds a MCSD certification since July 2013.

In his free time, he likes to learn new technologies including real world business processes, contribute to online forums/blogs, listen to music and hang out with friends.

Comments and Discussions

 
Generalnew images grow considerably in file size Pin
acato18-May-10 10:58
professionalacato18-May-10 10:58 
QuestionNice, any GUI ? Pin
Kochise23-Aug-07 10:43
Kochise23-Aug-07 10:43 
AnswerRe: Nice, any GUI ? [modified] Pin
Tejas Patel29-Sep-07 20:14
Tejas Patel29-Sep-07 20:14 
Hi Kochise,

You are right kochise that GUI certainly increase user interaction but this code is mainly for bunch image processing. As we had already stamp configuration exist in database. So all we need to do is retrieve all settings for stamp create it runtime and process images without any user interaction.





General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.