【问题标题】:Is there any way to change images size faster?有什么方法可以更快地更改图像大小?
【发布时间】:2013-10-08 15:35:09
【问题描述】:

也许在内存流中?

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 并将其显示在图片框中。 然后,当我将鼠标移到图片框区域时,图片框正在移动到窗体的中心,我将图片框的大小调整为 500,500,并将图像大小再次更改为 500,500 并将它们显示在图片框中。

问题是每次更改/转换硬盘上的图像大小几乎需要 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));
        }

【问题讨论】:

  • 提前将图片加载到内存中。
  • 不确定这是否更快...不过可能更简单...看看 Image.GetThumbnailImage();
  • 当图片框只能显示其中一个时,为什么要迭代和调整整个目录的图像文件大小,这很不清楚。预计这将是缓慢的。编写更智能的代码,只调整显示图像的大小。
  • 为什么要物理调整磁盘上的图像大小然后重新加载它? .NET 可以为您拉伸框中的图像,因此您最好使用最大图像尺寸并让 .NET 为您将其调整为 100x100。话虽这么说...event .NET 在这方面确实很慢,因为它依赖 GDI+(软件渲染)来调整图像大小。因此,您可能希望自定义创建控件以实现快速图像渲染,或使用 WPF
  • 另外,您没有处理您的位图,这将导致您泄漏非托管资源时的性能损失。

标签: c# winforms


【解决方案1】:

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

  • 您正在从 500x500 调整到 100x100,然后再调整回 500x500 > 图像质量下降。
  • 你没有处理你的图像。
  • 如果您只想显示一张图片,为什么还要加载所有图片并调整它们的大小?只需调整悬停图像的大小,甚至无需将它们保存到磁盘。

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

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

【讨论】:

  • 卡拉对。我从 500x500 更改为 100x100 的原因是,当您将鼠标移到图片框上时,我想将图片框和里面的图像显示得更大。如果我将使用拉伸或其他东西,然后转换图像的大小,质量也不会很好。如果我更改为 500x500,质量会非常好。代码应该是怎样的?
猜你喜欢
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 2017-09-28
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多