質問

I was trying to delete an Image from the view Model after some modification but I'm getting this error message IOException was unhandled

I disposed the current bitmap currImgHandler.CurrentBitmap.Dispose();and also made the Image Source to be null ViewedPhoto.Source = null;

if i click on the button which has the Rotate function (90°) once and clikcing on the delete function which produces the error IOException was unhandled

If i clcik on the button Roate function twicethen click on Delete function; I don't get any error and it deletes image without any problem.

What is the mistake am I doing here? Thank you

Selecting an Image from ListView:

private string saveFilname;
private void showImage(object sender, SelectionChangedEventArgs args)
 {

     ListBox list = ((ListBox)sender);
     if (list != null)
     {
         int index = list.SelectedIndex;
         if (index >= 0)
         {
             ImageFileViewModel image = imageListBox.SelectedItem as ImageFileViewModel;

             if ((image != null))
             {
                 saveFilname = image.FileName.ToString();
                 currImgHandler.CurrentFileHandler.Load(image.FileName);
                 PaintImage();

             }
         }
     }
 }

Delete Function:

private void bDeletePhoto_Click(object sender, RoutedEventArgs e)
{

   ImageFileCollectionViewModel viewModel = imageListBox.DataContext as ImageFileCollectionViewModel;
    if (viewModel != null)
    {
        ImageFileViewModel image = imageListBox.SelectedItem as ImageFileViewModel;
        if (image != null)
        {
            //remove physical file from disk:
            currImgHandler.CurrentBitmap.Dispose();
            ViewedPhoto.Source = null; 

            File.Delete(image.FileName);
            //remove item from ObservableCollection:
            viewModel.AllImages.Remove(image);
        }
    }

}

//Rotate Function:

private void Button_Click(object sender, RoutedEventArgs e)//Rotate
{
    currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);
    PaintImage();
}
private void PaintImage()
{
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    currImgHandler.CurrentBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
    stream.Position = 0;
    byte[] data = new byte[stream.Length];
    stream.Read(data, 0, Convert.ToInt32(stream.Length));
    BitmapImage bmapImage = new BitmapImage();
    bmapImage.BeginInit();
    bmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bmapImage.StreamSource = stream;
    bmapImage.EndInit();
    ViewedPhoto.Source = bmapImage; //ImageBox
    ViewedPhoto.Stretch = Stretch.Uniform;
}

Error message: enter image description here

役に立ちましたか?

解決 3

After Drew Rsuggestion i have added a method in the MVVM itself:

public void RemoveOldPhotoItem(ImageFileViewModel imageFile)
        {
            this._allImages.Remove(imageFile);
            this.DataItemsCount++;
            File.Delete(imageFile.FileName);
        }

I need to dispose the image which was used by a 3rd party .dll....which i disposed properly. Sorry guys it was my mistake. Thank you for your support!

他のヒント

Your application is throwing an IOException. Likely culprit: File.Delete(image.FileName);

IMO You should avoid making calls like this directly in event handlers for the reason demonstrated here - it is hard to catch exceptions thrown directly from an event handler.

If implementing MVVM this doesn't tend to occur.

Perhaps the account privileges under which your application is running are not sufficient to let you delete the file. Please check the credentials of this account against operations on the file you want to delete.

It's only a supposition though, it's hard to be sure without the IOEXception content, and it could also be because the path you're using doesn't exist, etc...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top