Click here to Skip to main content
Click here to Skip to main content

Merging Images in .NET

By , 28 Jun 2006
 
Sample Image - output.jpg

Introduction

Sometimes, we need to place one or more images on the other image’s surface or create an image from a few image files by joining them together. For example, we have a.jpg, b.jpg and c.jpg files and we need to create an image with the word “abc”. In this short tutorial, I will show how to do this.

The System.Drawing namespace in the .NET Framework provides access to basic graphics functionality and System.Drawing.Imagine namespace includes classes and interfaces that provide imaging functionality. To use those namespaces, you need to add a reference to System.Drawing.dll in the Solution Explorer.

Coding

Now let’s discuss the code. At first, we need to create a new image on the surface of which we will place all three images (a, b, c) to create the word “abc”:

// Create a new image
Image img = new Bitmap(300, 100);

Two parameters determine the dimension of the new image. If we want to use the already existing image as background, we can create an image from file:

// Create image object from existing image
Image img = Image.FromFile("Image.jpg");

Before make changes with img, we need to convert it to a Graphics object:

Graphics g = Graphics.FromImage(img);

Now using Graphics.DrawImage method, we can put all letters on the image:

// Place a.gif
g.DrawImage(Image.FromFile("a.gif"), new Point(10, 10));
   
// Place b.jpg
g.DrawImage(Image.FromFile("b.jpg"), new Point(70, 10));
   
// Place c.jpg
g.DrawImage(Image.FromFile("c.jpg"), new Point(130, 10));

The first parameter is the image object, the second – object Point that we use to determine a position by rectangular coordinates.

To save the result in a file, call the Image.Save method:

// Save changes as output.jpg
img.Save("output.jpg", ImageFormat.Jpeg);

GDI library supports a lot of graphic formats. For instance, you can save the result as GIF or PNG:

// Save changes as output.gif
img.Save("output.gif", ImageFormat.Gif);

// Save changes as output.png
img.Save("output.png", ImageFormat.Png);

To display graphics without a white border, I saved letter A as a GIF file and used transparency. As we see far in the result, Graphics.DrawImage method detects that Image object includes a transparency layer and places it in the right way.

All source files with example images can be download from here.

License

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

About the Author

Alexander Kleshchevnikov
Web Developer
Ukraine Ukraine
Member
Alexander is freelance web developer with 4 years experience. He has skills in ASP.NET/MS SQL Server, PHP/MySQL, Ruby On Rails, XHTML, CSS. You can contact with me by seigo.ua@gmail.com. Also check my homepage at www.klalex.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionsir is it possible to using drag ans drop to pictureboxmembergunakar11 May '13 - 5:57 
QuestionHow did you enable the transparency layer for a.gif??memberRajiv Charan Tej K21 Oct '12 - 20:25 
GeneralMy vote of 5memberAshish Tripathi13 Sep '12 - 6:40 
Questionoverlay image on videomemberjymitra13 Feb '11 - 19:40 
GeneralMy vote of 5memberMichael Boyd22 Sep '10 - 4:29 
GeneralForgot to votememberDonDriskell22 Jul '09 - 4:53 
GeneralNice ArticlememberDonDriskell22 Jul '09 - 4:48 
GeneralRelease MemorymemberJesseZ1 May '07 - 9:52 
GeneralUpdated June 29, 2006memberfirmwaredsp29 Jun '06 - 18:03 
GeneralJust curious...memberJun Du7 Jun '06 - 8:53 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 29 Jun 2006
Article Copyright 2006 by Alexander Kleshchevnikov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid