Last post Apr 24, 2012 08:06 AM by Omnicidius
Apr 20, 2012 08:25 AM|Omnicidius|LINK
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
This is currently what my code looks like:
protected void saveMergedImages(string strPathToImages)
string images = Directory.GetFiles(strPathToImages);
Bitmap simulation = combineImages(images);
Bitmap combineImages(string image)
Bitmap template = new Bitmap(image);
Bitmap picture = new Bitmap(image);
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));
Please could someone help or point me in the right direction.
Apr 24, 2012 08:06 AM|Omnicidius|LINK
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.
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 :)