【问题标题】:Delete or have Control of an Image while your Application is Using it C#在应用程序使用图像时删除或控制图像 C#
【发布时间】:2015-11-04 18:12:21
【问题描述】:

我正在使用 WinForms。在我的表格中,我有一个图片框。在表单加载时,我的程序从我的 C:/image 目录打开一个图像文档到我的图片框中。问题是当我的程序打开该图像时,我无法进入我的 C:/image 目录并删除该图片,因为我的应用程序正在使用它。当我转到 C:/image 目录并尝试删除图片时,出现此错误。

我的目标是控制图像文档,这意味着我能够删除特定文档,即使它正在被我的应用程序使用。

测试:我测试了您是否可以在我的计算机中安装“Windows 照片查看器”的同时查看图像时删除图像,并且该应用程序允许您。 Windows 照片查看器不会锁定图像。当您从目录中删除图像时,图像也会在 Windows 照片查看器中消失。我想完成类似的事情。

建议的代码:我尝试实现它,但我认为我实现它不正确。

Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
    img = new Bitmap(bmpTemp);
}

下面我提供了我编写的将图片加载到图片框中的代码。

    private void Form1_Load(object sender, EventArgs e) //When form load you do this:
    { 
        try // Get the tif file from C:\image\ folder
        {
            string path = @"C:\image\";
            string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.

            pictureBox1.Load(filename[0]);


            lblFile.Text = filename[0];
            RefreshImage(); // refreshing and showing the new file
            opened = true; // the files was opened.


                Image img1 = Image.FromFile(lblFile.Text);
                pictureBox1.Image = img1;
                pictureBox1.Width = img1.Width;
                pictureBox1.Height = img1.Height;
                picWidth = pictureBox1.Width;
                picHeight = pictureBox1.Height;
                getRatio();  
        }
        catch (Exception ex)
        {
            MessageBox.Show("No files or " + ex.Message);
        }          
    }

【问题讨论】:

  • FromFile 锁定文件。尝试从流中打开它:Open Image from file, then release lock?
  • 您需要在打开图像之前对其进行复制。您可以将其加载到内存中(例如使用MemoryStream)或在文件系统上制作文件的副本(例如临时文件)。
  • 我将尝试这两种方法。我不知道哪个应该是更好的方法。我还是编程新手。如果你们能举例说明如何在我的代码中实现它,我将非常感激@LarsTech

标签: c# .net winforms picturebox delete-file


【解决方案1】:

在创建图像之前复制图像文件位:

private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{ 
    try // Get the tif file from C:\image\ folder
    {
        string path = @"C:\image\";
        string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.

        FileInfo fi = new FileInfo(filename[0]);
        byte [] buff = new byte[fi.Length];
        using ( FileStream fs = File.OpenRead(fileToDisplay) )
        {
            fs.Read(buff, 0, (int)fi.Length);      
        }
        MemoryStream ms = new MemoryStream(buff);
        Bitmap img1 = new Bitmap(ms);
        opened = true; // the files was opened.
        pictureBox1.Image = img1;

        pictureBox1.Width = img1.Width;
        pictureBox1.Height = img1.Height;
        picWidth = pictureBox1.Width;
        picHeight = pictureBox1.Height;
        getRatio();  
    }
    catch (Exception ex)
    {
        MessageBox.Show("No files or " + ex.Message);
    }          
}

【讨论】:

  • 我尝试制作副本,但无法弄清楚。你能帮帮我吗?
猜你喜欢
  • 2016-02-05
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 2021-10-08
  • 2014-07-05
相关资源
最近更新 更多