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

SubSet Sum Problem Example in C January 10, 2007

Filed under: C & C++ — Akdora @ 5:32 pm
Tags: , , ,

In this assignment, you will write a program that will fill 3 boxes with fruits. Your program will get the fruits’ names, their weights and the capacities of the boxes from a file. You can get the file from the link :

input.txt

 

4.JPG

 

RULES:

  •  The program will attempt to fill these boxes with fruits one by one, starting from the first (Box1) and continuing with the next (e.g. Box2) until to the last (e.g. Box3) based on the following criteria:
    • Either a box can be filled with the exact weight (ex. Box1 should get exactly 32 kg fruits)., or it will be skipped and left empty and the next box will be tried to be filled.
    • Program will not use same fruit twice. (ex. If program have used apple for Box1, apple can’t be used again for next boxes).
  •  Program will write each boxes’ last situations to the result.txt file. If a box is full, the names of the fruits that are in that box must be written to the result.txt. If the box is empty, “Box-id: empty” must be written to the result.txt. Output file should look like as follows, note that following is sample output not the solution:

Box1: lemon, mango, …

Box2: empty

Box3: apple, …

solution in C: