1

I want to create new image that the two ones will be side to side. And if i have 4 images so to make new image with 4 images inside. Not one over the other but 2 images one near the other or if it's 4 images so also one each near the other one.

What i did so far is to take from two images the pixels:

private void GetPixels(string File1, string File2)
        {
            Bitmap bmp1 = new Bitmap(File1);
            Bitmap bmp2 = new Bitmap(File2);

            for (int i = 0; i < bmp1.Width; i++)
            {
                for (int j = 0; j < bmp1.Height; j++)
                {
                    Color pixel = bmp1.GetPixel(i, j);
                }
            }
            for (int x = 0; x < bmp2.Width; x++)
            {
                for (int y = 0; y < bmp2.Height; y++)
                {
                    Color pixel = bmp1.GetPixel(x, y);
                }
            }
        }

I want to create new bitmap that somehow it's size will be big enought to store the two other bitmaps. How can i calculate the right size of the new bitmap so it will contain excatly the two other bitmaps ?

And how do i add the pixels to the new bitmap ?

EDIT**

Added it to the top level of the Form1 under the namespace and its ok. Now i tried to use it like this:

private void CreateNewImage()
        {
            for (int i = 0; i < imagesRainUrls.Count; i++)
            {
                //Use it
                //Double the same image
                Bitmap doubledBitmap =  imagesRainUrls[i].DoubleBitmap();
                //Append new image
                Bitmap appendedBitmap = imagesRainUrls.AppendBitmap(imagesSatelliteUrls[i]);
            }
        }

imagesRainUrls is List of images and also imagesSatelliteUrls is List of images.

The errors im getting:

Error 1 'string' does not contain a definition for 'DoubleBitmap' and the best extension method overload 'DownloadImages.BitmapExtensions.DoubleBitmap(System.Drawing.Bitmap)' has some invalid arguments

Error 2 Instance argument: cannot convert from 'string' to 'System.Drawing.Bitmap'

Error 3 'System.Collections.Generic.List' does not contain a definition for 'AppendBitmap' and the best extension method overload 'DownloadImages.BitmapExtensions.AppendBitmap(System.Drawing.Bitmap, System.Drawing.Bitmap)' has some invalid arguments

Error 4 Instance argument: cannot convert from 'System.Collections.Generic.List' to 'System.Drawing.Bitmap'

Error 5 Argument 2: cannot convert from 'string' to 'System.Drawing.Bitmap'

And also i have more images in the imagesRainUrls List then the SatelliteUrls List so how can i make that it will merge the two images untill the number of SatelliteUrls images ? Rain images i have 62 and satellite only 9 In the end of this process i will make animated gif from all the gifs the merged and those who are not merged. So the animation will be one gif with the first 9 images merged and the rest of the Rain will be not merged but same animation.

So how do i make it with the FOR loop since i have more rain images then sateliite so i need to merge only 9 images.

user2760148
  • 427
  • 2
  • 9
  • 19
  • Note that using GetPixel like this in a loop is extremely slow. Using the DrawImage method lets you copy pixels from one bitmap to another bitmap much more quickly. – David Sep 18 '13 at 16:59
  • possible duplicate of [Merging two images in C#/.NET](http://stackoverflow.com/questions/465172/merging-two-images-in-c-net) – huMpty duMpty Sep 18 '13 at 17:08
  • @David I am not sure why you are saying that relying on graphics is quicker. From my experience is much slower. Here you have an answer I participated in (although didn't provide the solution) complaining precisely about Graphics being too slow (and coming up with a much quicker solution): http://stackoverflow.com/questions/18083318/fast-way-to-create-a-big-bitmap-from-an-array-of-bitmap/18088950#18088950. Also I have done a quick test of the King King code against a getpixel/setpixel one doing the same process and the set/get one was faster (not too much faster, but the pics were small). – varocarbas Sep 18 '13 at 17:12
  • @David I have done further testing and I have to correct my previous statement: in this specific situation (with the King King code), DrawImage is faster; apparently, the fact of loading/saving images has a big effect on its performance (not in the set/get) and that's why the results of my tests have varied appreciably on account of the specific conditions. – varocarbas Sep 18 '13 at 17:50
  • your `imagesRainUrls[i]` is `string`, it can't use `DoubleBitmap`, your `imagesRainUrls` is a `List<>` so it can't also use `AppendBitmap`. Note that I name it `AppendBitmap` doesn't mean you use it to append some Bitmap to your collection, it can be used only on a `Bitmap`, not on any instance of other types. – King King Sep 18 '13 at 21:08

1 Answers1

2

You can use Graphics.FromImage method to get the Graphics of an Image then use methods of that object to draw everything on the image:

public static class BitmapExtensions {
  public static Bitmap DoubleBitmap(this Bitmap bm){
     Bitmap bitmap = new Bitmap(bm.Width * 2, bm.Height);
     using(Graphics g = Graphics.FromImage(bitmap)){
       Rectangle rect = new Rectangle(Point.Empty, bm.Size);
       g.DrawImage(bm, rect);
       rect.Offset(bm.Width,0);
       g.DrawImage(bm, rect);
       return bitmap;
     }
  }      
  public static Bitmap AppendBitmap(this Bitmap bm, Bitmap rightBitmap){
    Bitmap bitmap = new Bitmap(bm.Width + rightBitmap.Width, Math.Max(bm.Height, rightBitmap.Height));
    using(Graphics g = Graphics.FromImage(bitmap)){
       Rectangle rect = new Rectangle(Point.Empty, bm.Size);
       g.DrawImage(bm, rect);
       rect = new Rectangle(new Point(bm.Width, 0), rightBitmap.Size);
       g.DrawImage(rightBitmap, rect);           
       return bitmap;
     }
  }
}
//Use it
//Double the same image
Bitmap doubledBitmap = yourBitmap.DoubleBitmap();
//Append new image
Bitmap appendedBitmap = yourBitmap.AppendBitmap(yourSecondBitmap);
King King
  • 61,710
  • 16
  • 105
  • 130
  • Im getting an error two errors one on the DoubleBitmap and one on the AppendBitmap both errors are the same : Error 1 Extension method must be defined in a top level static class; BitmapExtensions is a nested class . How can i solve it ? – user2760148 Sep 18 '13 at 20:47
  • @user2760148 the extension class should be placed in top level scope (under some namespace), don't place it in any other class. – King King Sep 18 '13 at 20:48
  • Ok solved now i tried to use it and get some erros. Updating my question with how i tried to use it. – user2760148 Sep 18 '13 at 20:57
  • Updated my question just now. Sorry. – user2760148 Sep 18 '13 at 21:04