How do I overlay an image on to another image? RRS feed

  • Question

  • User576287374 posted

    Hey all, 

    I'm fairly new to the manipulation of images. At the moment I need to be able to take an uploaded image and paste on to an existing image. Basically merging a smaller image on to a larger one. However with the code I am using I receive

    Parameter must be positive and < Height.
    Parameter name: y

    This is currently what my code looks like:

    protected void saveMergedImages(string strPathToImages)
        string[] images = Directory.GetFiles(strPathToImages);
        Bitmap simulation = combineImages(images);
        simulation.Save(strPathToImages, System.Drawing.Imaging.ImageFormat.Bmp);
    Bitmap combineImages(string[] image)
       Bitmap template = new Bitmap(image[0]);
       Bitmap picture = new Bitmap(image[1]);
       for (int w = 0; w < template.Width; w++)
            for (int H = 0; H < template.Height; H++)
                if (w >= 60 && w < 240)
                    if (H >= 60 && H < 390)
                        template.SetPixel(w, H, picture.GetPixel(w - 60, H - 60));
                  template.SetPixel(w, H, template.GetPixel(w, H));
    return template;

    Please could someone help or point me in the right direction.

    Friday, April 20, 2012 8:25 AM


  • User576287374 posted

    Ok so I scrapped the code above as I find it somewhat messy, apart from that, swapping the images tends to be a pain.
    Also, what if you have more than 2 images in the folder and need to be specific about the images you wish to use?

    The code i'm using now works like a charm, it's straight forward and easy to manipulate:

        //### Merges the uploaded file with the current Billboard choice.
        protected void mergeImages(string strPathToImages, string strImg, string strFilename, string newFileName, int leftFloat, int topFloat)
            //## Create Images from the desired Template and the Uploaded file.
            System.Drawing.Image imgTemplate = System.Drawing.Image.FromFile(Path.GetFullPath(strPathToImages + "\\" + strFilename));
            System.Drawing.Image imgOverlay = System.Drawing.Image.FromFile(Path.GetFullPath(strPathToImages + "\\" + strImg));
            //## Create a graphics object from the template for overlay.
            System.Drawing.Image original = imgTemplate;
            Graphics gra = Graphics.FromImage(original);
            //## Gra.DrawImage, this draws the uploaded image on to the Template, setting it's position and dimensions.
            gra.DrawImage(imgOverlay, leftFloat, topFloat, imgOverlay.Width, imgOverlay.Height);
            //## Saves the newly made image.
            original.Save(strPathToImages + "\\" + newFileName, ImageFormat.Jpeg);

    The method above makes it really simple to achieve stitching an image on to another image as well as position they overlaying image. 
    The goal was to simulate bill board adverts. The project contains the images for the billboards, refered to as imgTemplate in this code, the image uploaded by the user (imgOverlay) is then drawn on to the existing template and saved to a new file.
    Simple yes?

    Just for reference, i found that I absolutely needed to set the width and height in the gra.DrawImage() method, to the overlay's width and height, ie imgOverlay.Width, imgOverlay.Height... When I tried to add my own integer values it would not draw the overlay image. Irrespective of whether the integer values matched the overlay's width and height.

    Hope this helps anyone who needs it :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2012 8:06 AM