无法覆盖包含在using语句中的文件

时间:2015-07-21 14:24:11

标签: c# wpf xaml

我正在构建一个应该执行以下操作的应用程序:

  • 用户选择列表框中的元素(示例:青蛙
  • 在画布上打开与元素对应的图片作为背景,用户可以在其上绘制
  • 当用户选择列表框的另一个元素时,画布将保存为以未选中元素命名的图片,其中" New"添加(示例:frogNew
  • 此新元素已添加到列表框中,如果用户再次对其进行编辑,则会将其保存在同名下(示例:frogNew

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

page_screen_output(0);

page_output_immediately(1);

知道为什么这行&#34; 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; } 总是给我这个文件已经打开的错误?我怎么能关闭呢?

2 个答案:

答案 0 :(得分:4)

这是因为您的文件被您用作图像源的 <div class="container-fluid"> <div class="row"> <div class="col-md-2 col-sm-4 col-xs-4 col-xxs-4"> <div class=""> <a href="#"><img id="logo" class="img-responsive" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRnfIk3IX1dY-okcqPH5BX_L0XF2z31qwP-MD0nijTdAGc2alNc4Q" style="vertical-align: middle;" alt="logo here"/></a> </div> </div> <div class="col-md-10 col-sm-8 col-xs-8 col-xxs-8"> <div class="row"> <div class="col-md-12"> <div class=""> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="#" style="color: #1B75BB;">Search</a> </li> <li><a href="#" style="color: #1B75BB;">Archives</a> </li> <li><a href="#" style="color: #1B75BB;">About</a> </li> </ul> </div> </nav> </div> </div> </div> </div> </div> </div> 锁定

初始化位图图像时需要指定BitmapImage选项:

BitmapCacheOption.OnLoad

答案 1 :(得分:2)

我不认为这是保存文件的保存操作。它是您的代码示例末尾附近的读取操作。你打开FooNew.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()创建新文件或覆盖现有文件。