Akdora’s Blog

Programming, Oracle, Life, Fun

C#.Net Download and Merge Images into One Bitmap Image April 5, 2011

Filed under: Visual C#.NET — Akdora @ 7:10 pm
Tags: , , ,

Recently, I developed a program that reads imdb.com web page for a specific movie and downloads movie info and images. Then, it merges the thumbnails images into one image file.

Firstly, I write the main code, that downloads the image files and merges them and save the merged image to system.

Note 1: In the code, arr variable is an ArrayList holds the picture urls.

Note 2: Image class is in “System.Drawing”


                                        Image[] images = new Image[arr.Length];
                                        for (int k = 0; k < arr.Length; k++){
                                             images[k] = Utils.DownloadImage(arr[k]);
                                        }
                                        Image mergedImage = Utils.MergeImages(images);
                                        if (mergedImage != null)
                                        {
                                             mergedImage.Save(fileFolder + "\\" + movieTitle + " Thumbnails.jpg");
                                        }

Here is inside of DownloadImage function:

        /// <summary>
        /// Function to download Image from website
        /// </summary>
        /// <param name="_URL">URL address to download image</param>
        /// <returns>Image</returns>
        public static Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 20000;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }

Here is inside of MergeImages function:

        /// <summary>
        /// Merges Images into 1 Image.
        /// </summary>
        /// <param name="images">The Images you want merge</param>
        /// <returns>An Image of all images</returns>
        public static Image MergeImages(Image[] images)
        {
            if (images == null || images.Length <= 0)
            {
                return null;
            }
            Int32 imageWSize = 0;
            Int32 imageHSize = 0;

            for(int i=0;i<images.Length;i++)
            {
                if (images[i].Width > imageWSize)
                    imageWSize = images[i].Width;

                if (images[i].Height > imageHSize)
                    imageHSize = images[i].Height;
            }

            Int32 width = 0;
            Int32 height = 0;
            int picsInOneLine = 10;

            if (images.Length >= picsInOneLine)
            {
                width = picsInOneLine * imageWSize;
                decimal d = (images.Length + picsInOneLine) / picsInOneLine;
                height = (int)Math.Round(d) * imageHSize;
            }else{
                width = images.Length * imageWSize;
                height = imageHSize;
            }

            Bitmap bitmap = new Bitmap(width, height);
            int hhh = -1;
            int www = 0;
            for(int i=0; i<images.Length; i++)
            {
                Bitmap image = new Bitmap(images[i]);
                if (i % picsInOneLine == 0)
                {
                    hhh++;
                    www = 0;
                }

                //Get All of the x Pixels
                for (int w = 0; w < imageWSize; w++)
                {
                    //Get All of the Y Pixels
                    for (int h = 0; h < imageHSize; h++)
                    {
                        //Set the Cooresponding Pixel
                        int ww = w + (www * imageWSize);
                        int hh = h + (hhh * imageHSize);
                        bitmap.SetPixel(ww, hh, image.GetPixel(w, h));
                    }
                }
                www++;

            }
            //Return the new Bitmap
            return bitmap;
        }
Advertisement
 

One Response to “C#.Net Download and Merge Images into One Bitmap Image”

  1. […] C#.Net Download and Merge Images into One Bitmap Image В« Akdora's Blog Apr 5, 2011 … Net Download and Merge Images into One Bitmap Image April 5, 2011. Filed under: Visual C#. … […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s