不能覆盖用using语句包装的文件

本文关键字:包装 文件 语句 using 覆盖 不能 | 更新日期: 2023-09-27 18:03:43

我正在构建一个应用程序,应该做以下事情:

  • 用户在列表框中选择一个元素(示例:frog)
  • 在画布上打开与元素相对应的图片作为背景,用户可以在其上绘制
  • 当用户选择列表框的另一个元素时,画布被保存为以未选中元素命名的图片,并添加"New"(示例:frogNew)
  • 这个新元素被添加到列表框中,如果用户再次编辑它,它将被保存在下,以相同的名称 (示例:frogNew)

事情正在解决,除了我试图以相同的名称保存画布(frogNew)的部分。我得到一个错误,说我不能保存文件,因为它已经打开。你能告诉我在我的代码中没有正确关闭文件的地方吗?

private void save_picture(string name)
{
    //This part takes a screenshot of the canvas, named "paintSurface"
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)paintSurface.RenderSize.Width, (int)paintSurface.RenderSize.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default);
    rtb.Render(paintSurface);
    BitmapEncoder pngEncoder = new PngBitmapEncoder();
    pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
    //If the file already exists, we add "New" to its name
    var regex1 = new Regex(@"New$");
    if (regex1.Match(nom).ToString() == "")
    {
        using (var fs = System.IO.File.OpenWrite(@"D:'Test" + name + "New.png"))
        {
            pngEncoder.Save(fs);
        }
    }
    else
    {
        using (var fs = System.IO.File.OpenWrite(@"D:'Test" + name + ".png"))
        {
            pngEncoder.Save(fs);
        }
    }
}
private void listBox1_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
{
    //When the index of listbox changes, I save the canvas in a file named after the former index
    List<string> oldItemNames = new List<string>();
    if (e.RemovedItems.Count != 0)
    {
        var oldPhoto = e.RemovedItems[0].ToString();
        save_picture(oldPicture);
    }
    //I start a new canvas with the picture corresponding to the new index as a background
    paintSurface.Children.Clear();
    ImageBrush newBrush = new ImageBrush();
    newBrush.ImageSource = new BitmapImage(new Uri(@"D:'Test" + listBox1.SelectedItem.ToString() + ".png", UriKind.Relative));
    paintSurface.Background = newBrush;                        
}

知道为什么这行" using (var fs = System.IO.File.OpenWrite(@"D:'Test" + name + ".png"))"总是给我这个文件已经打开的错误吗?我怎样才能关闭它?

不能覆盖用using语句包装的文件

这是因为您的文件被您用作图像源的BitmapImage锁定了

您需要在初始化位图图像时指定BitmapCacheOption.OnLoad选项:

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(@"D:'Test" + listBox1.SelectedItem.ToString() + ".png", UriKind.Relative);
bitmapImage.EndInit();
newBrush.ImageSource = bitmapImage;

我不认为这是你的保存操作保持文件打开。它是代码示例末尾附近的读取操作。您打开fooonew .png,编辑它,并尝试写入相同的文件。

试试这个。当你有:

newBrush.ImageSource = new BitmapImage(new Uri(@"D:'Test" + listBox1.SelectedItem.ToString() + ".png", UriKind.Relative));

替换为:

using (FileStream fileStream = File.OpenRead(@"D:'Test" + listBox1.SelectedItem.ToString() + ".png"))
{
    var bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = fileStream;
    bitmapImage.EndInit();
    newBrush.ImageSource = bitmapImage;
}

一旦你得到这个工作,你会意识到File.OpenWrite()不是你想要的save_picture。它将把新的PNG附加到任何现有的文件内容。你想要File.Create()创建一个新文件或覆盖一个现有的文件。