How I prepare photos for my web gallery.

 

As you can see, for example here, I need small clickable thumbnails and big size photos.

The thumbnails are 228x171 (approx. 10kB), while the big photos 1424x1068 (approx. 150kB). To make things easier I keep a constant name convention through all my galleries (y01s.jpg, y02s.jpg, …. for the thumbnails and y01.jpg, y02.jpg, …. big photos). I wanted to automate the process of creating them from the original photos I took at 2848x2136 (approx. 3MB), yet having the option to chose which photos I want in the gallery. Additionaly the pictures in the resolution 600x800 are generated for the use with my PhotoFrame. The code below contains the old version, but the downloadable file is up to date.

 

using System;

using System.Drawing;

using System.Windows.Forms;

using System.IO;

 

namespace WebPhotoPreparer

{

    public partial class Form1 : Form

    {

 

        string[] webphp;

        public Form1()

        {

            InitializeComponent();

            Width = 1024;

            Height = 768;

            pictureBox1.Width = 712; //original width by 4

            pictureBox1.Height = 534; //original height by 4

            webphp = new string[1];

            if (File.Exists("webphp.txt"))

                 webphp = File.ReadAllLines("webphp.txt");

 

        }

 

        string dir1 = "";

        int nri = 1;

        int nrClick = 0;

 

        private void button1_Click(object sender, EventArgs e)

        {

            folderBrowserDialog1.SelectedPath = webphp[0];

            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)

                return;

 

            webphp[0] = folderBrowserDialog1.SelectedPath;

            listBox1.Items.Clear();

            DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);

            webphp[0] = folderBrowserDialog1.SelectedPath;

            FileInfo[] fi = di.GetFiles("*.JPG");

          

 

            dir1 = di.FullName;

 

            int nrt = 0;

            foreach (FileInfo f in fi)

                if (f.ToString().StartsWith("y"))

                    nrt++;

                else

                    listBox1.Items.Add(f);

 

            nri = (int) (nrt/2);

 

            string nrs1 = Convert.ToString(nri);

            if (nri + 1 < 10)

                nrs1 = "y0" + nrs1 + ".jpg";

            else

                nrs1 = "y" + nrs1 + ".jpg";

 

            button2.Text = nrs1;

                 

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            pictureBox1.Image = null;

            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            pictureBox1.Image = Image.FromFile(dir1 + "\\" + listBox1.SelectedItem.ToString());

            nrClick++;

            if (nrClick == 20)

            {

                nrClick = 0;

                System.GC.Collect();

            }

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

          

            Image src_image = Image.FromFile(dir1 + "\\" + listBox1.SelectedItem.ToString());

            Bitmap bitmap = new Bitmap(2 * pictureBox1.Width, 2 * pictureBox1.Height, src_image.PixelFormat);

            Bitmap bitmaps = new Bitmap(228, 171, src_image.PixelFormat);

            Graphics new_g = Graphics.FromImage(bitmap);

            Graphics new_gs = Graphics.FromImage(bitmaps);

            new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            new_gs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            new_gs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            new_g.DrawImage(src_image, 0, 0, bitmap.Width, bitmap.Height);

            new_gs.DrawImage(src_image, 0, 0, bitmaps.Width, bitmaps.Height);

            src_image.Dispose();

           

 

            string nrs = Convert.ToString(nri);

            if (nri < 10)

                nrs = "y0" + nrs + ".jpg";

            else

                nrs = "y" + nrs + ".jpg";

 

            string nrss = Convert.ToString(nri);

            if (nri < 10)

                nrss = "y0" + nrss + "s.jpg";

            else

                nrss = "y" + nrss + "s.jpg";

 

            string nrs1 = Convert.ToString(nri+1);

            if (nri+1 < 10)

                nrs1 = "y0" + nrs1 + ".jpg";

            else

                nrs1 = "y" + nrs1 + ".jpg";

 

            button2.Text = nrs1;

 

            bitmap.Save(dir1 + "\\" + nrs, System.Drawing.Imaging.ImageFormat.Jpeg);

            bitmaps.Save(dir1 + "\\" + nrss, System.Drawing.Imaging.ImageFormat.Jpeg);

            bitmap.Dispose();

            bitmaps.Dispose();

            new_g.Dispose();

            new_gs.Dispose();   

 

            nri++;

        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            File.WriteAllLines("webphp.txt", webphp);           

        }

    }

}

 

Source code

 

Description: D:\Current\www\www.kordos.com.2011-08\net\rams.jpg