c# - 代码使用大内存来显示图像?

标签 c# winforms image picturebox viewer

我正在使用 Winforms 和 C# 制作一个简单的图像查看器和编辑器,但遇到了一个我认为可能出现在这个函数中的问题;

    /// <summary>
    /// Extracts image details from specified image file.
    /// </summary>
    /// <param name="BitmapName">The name of file to process.</param>
    /// <param name="ImageName">The TextBox to recieve image name.</param>
    /// <param name="ImageCreated">The TextBox to recieve image creation date.</param>
    /// <param name="Size">The TextBox to recieve image size.</param>
    /// <param name="Width">The TextBox to recieve image width.</param>
    /// <param name="Height">The TextBox to recieve image height.</param>
    /// <param name="HResolution">The TextBox to recieve image's Horizontal Resolution.</param>
    /// <param name="VResolution">The TextBox to recieve image's Vertical Resolution.</param>
    /// <param name="Type">The TextBox to recieve the type of image.</param>
    /// <param name="Preview">The PictuteBox to display this image as a preview.</param>
    public void GetImageData(string BitmapName, TextBox ImageName, TextBox ImageCreated, TextBox Size, TextBox Width, TextBox Height, TextBox HResolution, TextBox VResolution, TextBox Type, PictureBox Preview)
    {
        try
        {
            FileInfo fileinfo = new FileInfo(BitmapName);
            ImageName.Text = fileinfo.Name;
            ImageCreated.Text = fileinfo.CreationTime.ToString();
            Size.Text = (fileinfo.Length / 1024).ToString() + " Kilobytes";
            Image image = Image.FromFile(BitmapName);
            Bitmap Bit = new Bitmap(image);
            Height.Text = Bit.Height.ToString() + " px";
            Width.Text = Bit.Width.ToString() + " px";
            HResolution.Text = Bit.HorizontalResolution.ToString() + " dpi";
            VResolution.Text = Bit.VerticalResolution.ToString() + " dpi";
            if (fileinfo.Extension == ".bmp" || fileinfo.Extension == ".BMP")
            {
                Type.Text = "Bitmap Image";
            }
            else if (fileinfo.Extension == ".jpeg" || fileinfo.Extension == ".JPEG")
            {
                Type.Text = "Jpeg Image";
            }
            else if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".JPG")
            {
                Type.Text = "Jpg Image";
            }
            else if (fileinfo.Extension == ".png" || fileinfo.Extension == ".PNG")
            {
                Type.Text = "Png Image";
            }
            else if (fileinfo.Extension == ".gif" || fileinfo.Extension == ".GIF")
            {
                Type.Text = "GIF Image";
            }
            Preview.Image = image;
            Bit.Dispose();
        }
        catch (OutOfMemoryException)
        {
            Preview.Image = Properties.Resources.InvalidImage;
        }
    }

,此函数使用 Bitmap 类提取图像详细信息,但在查看 20-30 张图像后,它使用了几乎 600-700 MB RAM。请告诉我哪里出错了。

最佳答案

在更改 Preview.Image 之前不要对其进行处置,请尝试以下操作:

 public void GetImageData(string BitmapName, TextBox ImageName, TextBox ImageCreated, TextBox Size, TextBox Width, TextBox Height, TextBox HResolution, TextBox VResolution, TextBox Type, PictureBox Preview)
    {
      try
      {
        FileInfo fileinfo = new FileInfo(BitmapName);
        ImageName.Text = fileinfo.Name;
        ImageCreated.Text = fileinfo.CreationTime.ToString();
        Size.Text = (fileinfo.Length / 1024).ToString() + " Kilobytes";
        Image image = Image.FromFile(BitmapName);
        using (Bitmap Bit = new Bitmap(image))
        {
          Height.Text = Bit.Height.ToString() + " px";
          Width.Text = Bit.Width.ToString() + " px";
          HResolution.Text = Bit.HorizontalResolution.ToString() + " dpi";
          VResolution.Text = Bit.VerticalResolution.ToString() + " dpi";
          if (fileinfo.Extension == ".bmp" || fileinfo.Extension == ".BMP")
          {
            Type.Text = "Bitmap Image";
          }
          else if (fileinfo.Extension == ".jpeg" || fileinfo.Extension == ".JPEG")
          {
            Type.Text = "Jpeg Image";
          }
          else if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".JPG")
          {
            Type.Text = "Jpg Image";
          }
          else if (fileinfo.Extension == ".png" || fileinfo.Extension == ".PNG")
          {
            Type.Text = "Png Image";
          }
          else if (fileinfo.Extension == ".gif" || fileinfo.Extension == ".GIF")
          {
            Type.Text = "GIF Image";
          }
          if (Preview.Image != null)
            Preview.Image.Dispose();
          Preview.Image = image;
        }
      }
      catch (OutOfMemoryException)
      {
        Preview.Image = Properties.Resources.InvalidImage;
      }
    }

关于c# - 代码使用大内存来显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18316791/

相关文章:

.net - 带有大字体的 Windows 对话框

javascript - 获取图像的第一个像素的颜色(JS/jQuery)

javascript - 图片不在生产构建中显示,仅在开发中显示(webpack/sass-loader)

c# - 为什么 C# 编译器会在 IL 中发出额外的操作码?

c# - 工具窗口的永久焦点

c# - 如何在不定义目标表的情况下使用 BulkCopy

windows - 菜单项的快捷键(alt + 字母)。有命名约定吗?

C# XNA 4.0 异常 : "Cannot Open File"

c# - 如何让c#程序按顺序运行?堆栈,自上而下

c# - WPF - 按多列排序时使用自定义比较器