質問

I am making a simple image viewer and editor using Winforms and C#,but am facing a problem that i think may be in this function ;

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

,this function extracts image details using Bitmap class but after viewing 20-30 images it uses almost 600-700 MB RAM.Please tell where i am going wrong.

役に立ちましたか?

解決

You don't dispose Preview.Image before changing it, Try this:

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

他のヒント

you don't dispose image, only the bitmap use

bit = new Bitmap(filename);

to load image ad bitmap.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top