user2760148
user2760148

Reputation: 427

How can i create one image from two?

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.

Upvotes: 1

Views: 440

Answers (1)

King King
King King

Reputation: 63377

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);

Upvotes: 2

Related Questions