有没有办法更快地更改图像大小?

时间:2013-10-08 15:35:09

标签: c# winforms

也许在某种情况下在内存流中?

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

在这种情况下,我正在显示图像的图片框大小为100,100 所以我将图像大小更改为100,100并将其显示在pictureBox中。 然后,当我将鼠标移到pictureBox区域时,pictureBox将移动到窗体的中心,我将pictureBox的大小调整为500,500,并将图像大小再次更改为500,500,并将其显示在pictureBox中。

问题是每次更改/转换硬盘上的图像大小几乎需要3-5秒。

有没有更快的方法来进行尺寸转换?

编辑**

更改了试图仅调整当前显示图像大小的代码,但是现在pictureBox1中的图像尺寸不同,我在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));
        }

1 个答案:

答案 0 :(得分:3)

您的实施存在一些问题,请先解决这些问题:

  • 您正在从500x500调整为100x100,然后再调整为500x500&gt;质量损失你的形象。
  • 您没有丢弃图像。
  • 如果您只想展示一张图片,为什么要加载所有图片并调整它们的大小?只需调整您悬停的图像大小,然后您甚至不必将它们保存到磁盘。

如果您想要显示大量图像,请提供一些一般性提示:

  • 仅加载您在GUI中实际查看的图像,一旦开始滚动就加载更多。
  • 如果它仍然很慢,请将它们加载到后台线程中,并在完成后逐个图像显示它们。
相关问题