Doron Muzar
Doron Muzar

Reputation: 443

Is there any way to change images size faster?

Maybe in memory stream somehow ?

private void satellitesToolStripMenuItem_Click(object sender, EventArgs e)
{
    file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
    for (int i = 0; i < file_array_satellite.Length; i++)
    {
        Image s = new Bitmap(file_array_satellite[i]);
        s = resizeImage(s, new Size(100, 100));
        s.Save(UrlsPath + "Changed" + i.ToString("D6") + ".jpg");
    }
    file_array_satellite = Directory.GetFiles(UrlsPath, "Changed*.*");
    if (file_array_satellite.Length > 0)
    {
        DateTime[] creationTimes8 = new DateTime[file_array_satellite.Length];
        for (int i = 0; i < file_array_satellite.Length; i++)
            creationTimes8[i] = new FileInfo(file_array_satellite[i]).CreationTime;
        Array.Sort(creationTimes8, file_array_satellite);
        file_indxs_satellite = 0;
        file_indxs_satellite = file_array_satellite.Length - 1;
        timer1.Enabled = true;
    }
}

public static Image resizeImage(Image imgToResize, Size size)
{
    return (Image)(new Bitmap(imgToResize, size));
}

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
    file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
    for (int i = 0; i < file_array_satellite.Length; i++)
    {
        Image s = new Bitmap(file_array_satellite[i]);
        s = resizeImage(s, new Size(500, 500));
    }
    this.pictureBox1.Size = new Size(500, 500);
    pictureBox1.Location = new Point(this.Bounds.Width / 3,
                        this.Bounds.Height / 3);
    this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox1.BringToFront();
}

In this case the pictureBox i'm showing the images in is in size of 100,100 So i changed the images size to 100,100 and show it in the pictureBox. Then when i move the mouse over the pictureBox area the pictureBox is moving to the center of the form and i resize the pictureBox to 500,500 and also change the images size again to 500,500 and show them in the pictureBox.

The problem is that changing/converting the images size on the hard disk take almost 3-5 seconds each time.

Is there any faster way to make the size convertion ?

EDIT**

Changed the code trying to resize only the current display image but the images in the pictureBox1 now are not in the same size and i don't see the animation in the pictureBox.

private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {


                Image s = new Bitmap(file_array_satellite[file_indxs_satellite]);
                s = resizeImage(s, new Size(100, 100));
                pictureBox1.Load(file_array_satellite[file_indxs_satellite]);
                file_indxs_satellite = file_indxs_satellite - 1;
                if (file_indxs_satellite < 0)
                {
                    file_indxs_satellite = file_array_satellite.Length - 1;
                }
            }
            catch
            {
                timer1.Enabled = false;
            }
        }

        private void satellitesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
            if (file_array_satellite.Length > 0)
            {
                DateTime[] creationTimes8 = new DateTime[file_array_satellite.Length];
                for (int i = 0; i < file_array_satellite.Length; i++)
                    creationTimes8[i] = new FileInfo(file_array_satellite[i]).CreationTime;
                Array.Sort(creationTimes8, file_array_satellite);
                file_indxs_satellite = 0;
                file_indxs_satellite = file_array_satellite.Length - 1;
                timer1.Enabled = true;
            }
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

Upvotes: 0

Views: 164

Answers (1)

Carra
Carra

Reputation: 17964

A few problems with your implementation, fix these first:

  • You're resizing from 500x500 to 100x100 and then back to 500x500 > quality loss of your image.
  • You're not disposing your images.
  • Why do you load all images and resize them if you only want to show one image? Just resize the image you hover over and then you don't even have to save them to disk.

Some general hints if you do want to show a LOT of images:

  • Only load those images that you actually view in the GUI, load more once they start scrolling.
  • If it's still slow, load them in a background thread and show them image by image once they are done.

Upvotes: 3

Related Questions