Powered By Blogger

C#SHARP ANSWER

Friday, June 24, 2011

extracting all .jpg images from a folder

trying to extract all the images from a folder then follow up the following code and extract all the .jpg images from a folder

 String[] athar = Directory.GetFiles(@"D:\\", "*.JPG");
        PictureBox[] pb = new PictureBox[300];

int j = 100, l = 0,k=0;
    for (k=0; k <300; k++)
    {
        pb[k] = new PictureBox();
        if (k % 5 == 0)
        {
            j += 200;
            l = 0;
        }
        pb[k].Location = new Point((l * 190) + 200, j);
        pb[k].Size = new Size(150, 100);
        pb[k].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        pb[k].BackColor = Color.Black;
        pb[k].BorderStyle = BorderStyle.FixedSingle;
        pb[k].Image = Image.FromFile(athar[k]);
        pb[k].Visible = true;
        this.Controls.Add(pb[k]);
        pb[k].Cursor = System.Windows.Forms.Cursors.Hand;

        // Panel1.Controls.Add(pb[i]);

        pb[k].BringToFront();
      l++;
    }

creating thumbnail of a image

Feeling that you website is taking much time to load due to heavy size images then i have a solution to make your website load faster by just creating thumbnail of those images you want to add to your website.
C# code to create thumbnail of image is as follows

            string tostore="D:\\DSCF2088";
            int ImageHeight=200,n=0;
            foreach (string at in athar)
            {
                System.Drawing.Image i = System.Drawing.Image.FromFile(at);
                i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                System.Drawing.Image th = i.GetThumbnailImage
                                    (
                                    ImageHeight * i.Width / i.Height,
                                    ImageHeight,
                                    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
                                    IntPtr.Zero
                                    );
                i.Dispose();
                EncoderParameters ep = new EncoderParameters(1);
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)80);
                ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
                th.Save(tostore+n+".jpg", ici, ep);
                th.Dispose();
                n++;
            }