How to mearge two or more Images into a single image in C#/Dotnet.

edited April 2023 in Web Development

Hello guys,

As stated in the title, we will discuss merging two photos for the time being, and with code changes, we will be able to merge more images...

Firstly, we will create a function that will create a bitmap for two images. One of them is static, and the other is dynamic.

firstly we have to inculde some packages like

using iTextSharp.text; 
using iTextSharp.text.pdf;
using System.Drawing;
using System.Drawing.Imaging;

Then..

 private static List<Bitmap> GetBitmap(string filepath)
        {
            // THIS FUNCTION WILL LIST OUT THE FILES BY ADDING IN BITMAP
            var lstbitmap = new List<Bitmap>();//Create a empty bitmap
            var bitmap = new Bitmap("Files/Upload/QrSticker.jpg");  // Adding Static image to the bitmap
            var bitmap2 = new Bitmap(filepath);  // Adding Dynamic inage to the bitmap.
            lstbitmap.Add(bitmap);
            lstbitmap.Add(bitmap2);
            return lstbitmap; // returning Bitmap list
        }

Now by above function we have created an bitmap list with images, Then We are merging them together in the function mentioned below.   

public static string AddDoubleImage(string filepath,string Code) 
        { 
            //THIS FUNCTION WILL APPEND  PIC  TO STATIC PIC AND RETURNS A FILE PATH
            var bitmap = GetBitmap(filepath); // The method that returns List<Bitmap>
            var width = 5603; // This is the view frame Width
            var height = 8191; // This is the view frame height
            var bitmap2 = new Bitmap(width, height);  // Creating a frame by using height and width
            var g = Graphics.FromImage(bitmap2);
            var localWidth = 5; 
            g.DrawImage(bitmap[0], 0,0); //Adding First pic (Static pic) to a frame by using X axis and Y axis eg:. DrawImage(bitmap[1],Y axis postion,X axis Position 
            StringFormat stringformat = new StringFormat();   // creating String to add in Pic frame ( It's optional) 
            stringformat.Alignment = StringAlignment.Far;    
            stringformat.LineAlignment = StringAlignment.Far;
            Color StringColor = System.Drawing.ColorTranslator.FromHtml("#189AB4");  // Selecting String Color
            g.DrawString(Code, new System.Drawing.Font("arial",220,FontStyle.Bold),new SolidBrush(StringColor), new Point(4000, 6860),stringformat); //Adding String to Frame by using  X axis and Y axis .
            g.DrawImage(bitmap[1], 820,770,4000,4000);   // Adding Second Image to frame by using width height and can also increase size of pic. eg..  .DrawImage(bitmap[1],Y axis postion,X axis Position ,Increase size height,Increase size width);
            var ms = new MemoryStream(); 
            bitmap2.Save(ms, ImageFormat.Png);
            FileStream file = new FileStream((filepath.Split("."))[0]+".jpg", FileMode.Create, FileAccess.Write); // Saving two images into one image.
            ms.WriteTo(file); 
            file.Close(); 
            return (filepath.Split("."))[0]+".jpg"; 
        }     

By the above function you will get the path of image by uploading it on that position.

The total function goes like below...

using iTextSharp.text; 
using iTextSharp.text.pdf;
using System.Drawing;
using System.Drawing.Imaging;
public static string AddDoubleImage(string filepath,string Code) 
        { 
            //THIS FUNCTION WILL APPEND  PIC  TO STATIC PIC AND RETURNS A FILE PATH
            var bitmap = GetBitmap(filepath); // The method that returns List<Bitmap>
            var width = 5603; // This is the view frame Width
            var height = 8191; // This is the view frame height
            var bitmap2 = new Bitmap(width, height);  // Creating a frame by using height and width
            var g = Graphics.FromImage(bitmap2);
            var localWidth = 5; 
            g.DrawImage(bitmap[0], 0,0); //Adding First pic (Static pic) to a frame by using X axis and Y axis eg:. DrawImage(bitmap[1],Y axis postion,X axis Position 
            StringFormat stringformat = new StringFormat();   // creating String to add in Pic frame ( It's optional) 
            stringformat.Alignment = StringAlignment.Far;    
            stringformat.LineAlignment = StringAlignment.Far;
            Color StringColor = System.Drawing.ColorTranslator.FromHtml("#189AB4");  // Selecting String Color
            g.DrawString(Code, new System.Drawing.Font("arial",220,FontStyle.Bold),new SolidBrush(StringColor), new Point(4000, 6860),stringformat); //Adding String to Frame by using  X axis and Y axis .
            g.DrawImage(bitmap[1], 820,770,4000,4000);   // Adding Second Image to frame by using width height and can also increase size of pic. eg..  .DrawImage(bitmap[1],Y axis postion,X axis Position ,Increase size height,Increase size width);
            var ms = new MemoryStream(); 
            bitmap2.Save(ms, ImageFormat.Png);
            FileStream file = new FileStream((filepath.Split("."))[0]+".jpg", FileMode.Create, FileAccess.Write); // Saving two images into one image.
            ms.WriteTo(file); 
            file.Close(); 
            return (filepath.Split("."))[0]+".jpg"; 
        }

        private static List<Bitmap> GetBitmap(string filepath)
        {
            // THIS FUNCTION WILL LIST OUT THE FILES BY ADDING IN BITMAP
            var lstbitmap = new List<Bitmap>();//Create a empty bitmap
            var bitmap = new Bitmap("Files/Upload/QrSticker.jpg");  // Adding Static image to the bitmap
            var bitmap2 = new Bitmap(filepath);  // Adding Dynamic inage to the bitmap.
            lstbitmap.Add(bitmap);
            lstbitmap.Add(bitmap2);
            return lstbitmap; // returning Bitmap list
        }

Thank you...

Sign In or Register to comment.