关闭PictureBox中正在使用的文件

时间:2017-08-08 17:08:51

标签: c#

我正在使用此代码将Excel转换为图片并在图片框中预览。 该代码第一次正在运行。但是,当我第二次尝试上传时,我收到的错误表明图像文件正在使用中。特别是在保存点。

  OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "bak files (*.xls)|*.xls|All files (*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            Img1 = openFileDialog1.FileName;


            //Create a new Workbook object and
            //Open a template Excel file.
            Workbook book = new Workbook(Img1);
            //Get the first worksheet.
            Worksheet sheet = book.Worksheets[0];

            //Define ImageOrPrintOptions
            ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
            //Specify the image format
            imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            //Only one page for the whole sheet would be rendered
            imgOptions.OnePagePerSheet = true;

            //Render the sheet with respect to specified image/print options
            SheetRender sr = new SheetRender(sheet, imgOptions);
            //Render the image for the sheet
            Bitmap bitmap = sr.ToImage(0);

            //Save the image file specifying its image format.
            bitmap.Save("C:\\1.jpg");\\in this point i get my error that it says general error  GDI+.

            pictureBox2.Image = Image.FromFile("C:\\1.jpg");
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

        }
        else
        {
            this.DialogResult = DialogResult.None;

        }
    }

我必须先从我的图片框预览中停止使用然后重新上传。但是我该怎么做呢?我试过了

 pictureBox1.Image=null

,但它没有用。

3 个答案:

答案 0 :(得分:2)

使用时

pictureBox2.Image = Image.FromFile("C:\\1.jpg");

静态调用使文件保持打开(并锁定)。因此,您无法覆盖该文件。

解决方案:

if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
bitmap.Save("C:\\1.jpg");
bitmap.Dispose();
pictureBox2.Image = new Bitmap(Image.FromFile("C:\\1.jpg"));

这样文件就会被释放,以后可以被覆盖。

替代方案:

if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
bitmap.Save("C:\\1.jpg");
bitmap.Dispose();
using (Bitmap bm = new Bitmap(C:\\1.jpg"))
{
   pictureBox2.Image = new Bitmap(bm);
};

答案 1 :(得分:0)

我使用FileStream解决了这个问题。

  Dim fs1 As System.IO.FileStream
            fs1 = New System.IO.FileStream(Bitmap, IO.FileMode.Open, IO.FileAccess.Read)
            PictureBox1.Image = System.Drawing.Image.FromStream(fs1)
            fs1.Close()

看看你应该fs1.close()来完成流。

FileStream类表示计算机中的文件。 FileStream允许将数据作为字节数组移入和移出流。这意味着,它不能直接在文件中工作,就像你可以操作的Stream一样。

代码在vb.net中(因为它适用于我)。 因此将它转换为C#

是没有问题的

答案 2 :(得分:0)

这里是VB.NET的示例,用于在不打开文件锁定状态下进行其他操作的情况下在图片框中打开图像。 VB.NET代码。

Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg",
     IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()

C#代码。

System.IO.FileStream fs;
// Specify a valid picture file path on your computer.
fs = new System.IO.FileStream(@"C:\WINNT\Web\Wallpaper\Fly Away.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
PictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();

参考- https://www.codeproject.com/Questions/492654/5bc-23-5dplusdeleteplusimagepluswhichplusisplusope