如果用Image.FromFile(filePath)打开文件,如this.pictureBox1.Image = Image.FromFile(filePath);,在this.pictureBox1依然保持显示的状态下(也就是图片一直使用),这时filePath文件是一直打开的,无法进行删除等操作?怎么关闭打开的filePath文件?希望达人指点。

解决方案 »

  1.   

    这样不行。
    Image openImage = Image.FromFile(openFileDialog.FileName);
    this.pictureBox1.Image = (Image)openImage.Clone();
    openImage = null;
    GC.Collect();
    这样也不行。
    Image image = Image.FromFile(openFileDialog.FileName);
    this.pictureBox1.Image = (Image)image.Clone();
    image.Dispose();
    GC.Collect();
    这么笨的方法都式过了,疑惑为什么微软的FromFile把图片读到内存后不关闭文件,违反常理啊,又不能
    this.pictureBox1.Image = null;
    GC.Collect();
    因为这时候我还要显示图片。环境是.net framework 1.0(vs.net2002)
      

  2.   

    写了个测试的project给大家试试
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    namespace CloseFromFile
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // pictureBox1
    // 
    this.pictureBox1.Location = new System.Drawing.Point(0, 8);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(144, 88);
    this.pictureBox1.TabIndex = 0;
    this.pictureBox1.TabStop = false;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(208, 64);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 262);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.pictureBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "打开";
    if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    {
    this.pictureBox1.Image = Image.FromFile(openFileDialog.FileName);
    // 这时候不关窗口的话openFileDialog.FileName这个文件是打开的,删不掉 }
    }
    }
    }
      

  3.   

    //because image.Clone will send Image.Handle to new object, u can't delete it
    //because pictualbox.Image.Headle is image, u can't do "image.Dispose()"//u must try this:string filenameOf = @"D:\images\dao.gif";
    FileStream fs = new FileStream(filenameOf,FileMode.Open, FileAccess.Read);
    int byteLength = (int)fs.Length;
    byte[] wf = new byte[byteLength];
    fs.Read(wf,0,byteLength);
    fs.Close();Image img = Image.FromStream(new MemoryStream(wf));
    this.pictureBox1.Image = img;
      

  4.   

    非常感谢,这个方法可以,
    不过我认为FromFile应该读入文件就立即关闭的,搞不懂微软
      

  5.   

      /// <summary>
            /// 打开图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog obj = new OpenFileDialog();
                if(obj.ShowDialog()== DialogResult .OK)
                {
                    Image image = Image.FromFile(obj.FileName);
                    Image bmp = new Bitmap(image);
                    image.Dispose(); //这句话很重要
                    pictureBox1.Image = bmp;
                    this.Text = obj.FileName;
                }
            }        /// <summary>
            /// 保存图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                pictureBox1.Image.Save(this.Text);
            }