C# 使用大内存显示图像的代码?

C# 使用大内存显示图像的代码?,c#,winforms,image,picturebox,viewer,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> /// <

我正在使用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;
        }
    }
//
///从指定的图像文件中提取图像详细信息。
/// 
///要处理的文件的名称。
///用于接收图像名称的文本框。
///用于接收图像创建日期的文本框。
///用于接收图像大小的文本框。
///用于接收图像宽度的文本框。
///用于接收图像高度的文本框。
///用于接收图像水平分辨率的文本框。
///用于接收图像垂直分辨率的文本框。
///用于接收图像类型的文本框。
///使用PictureBox将此图像显示为预览。
public void GetImageData(字符串位图名称、文本框图像名称、文本框图像创建、文本框大小、文本框宽度、文本框高度、文本框HResolution、文本框VResolution、文本框类型、图片框预览)
{
尝试
{
FileInfo FileInfo=新文件信息(位图名称);
ImageName.Text=fileinfo.Name;
ImageCreated.Text=fileinfo.CreationTime.ToString();
Size.Text=(fileinfo.Length/1024).ToString()+“KB”;
Image Image=Image.FromFile(位图名称);
位图位=新位图(图像);
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=“位图图像”;
}
else if(fileinfo.Extension==“.jpeg”| | fileinfo.Extension==“.jpeg”)
{
Type.Text=“Jpeg图像”;
}
else if(fileinfo.Extension==”.jpg“| | fileinfo.Extension==”.jpg”)
{
Type.Text=“Jpg Image”;
}
else if(fileinfo.Extension==“.png”| | fileinfo.Extension==“.png”)
{
Type.Text=“Png图像”;
}
else if(fileinfo.Extension==”.gif“| | fileinfo.Extension==”.gif”)
{
Type.Text=“GIF图像”;
}
Preview.Image=Image;
Bit.Dispose();
}
捕获(OutOfMemoryException)
{
Preview.Image=Properties.Resources.InvalidImage;
}
}

,此函数使用位图类提取图像详细信息,但在查看20-30幅图像后,它使用了将近600-700 MB的RAM。请告诉我哪里出了问题。

您不处理图像,只使用位图

bit = new Bitmap(filename);

要加载图像广告位图。

您不处理预览。更改图像之前,请尝试以下操作:

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

这可能是解决方案,但您也希望使用内存分析器来真正确保。你在处理图像,因此你必须注意这些事情。内存分析器非常易于使用,它们为您显示了许多改进空间。