Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting a bitmap from local directory
    primarykey
    data
    text
    <p>Alright it has come to this. I searched this website among many others and no one can seem to give me a straight answer so I'm going to try just asking outright. Been on this issue for about a solid 3 days and I can't afford to waste any more time on it. </p> <p><strong>Goal:</strong> The app I am building is in WPF and is going to be used as a bug tracker for a project my design team and I will be undertaking soon. Since we are going to be building a game in C++ most of the errors that occur will have a visual element to them so I inlcuded functionality to provide an image of the error in question when the user adds a bug to the list. I then take that image and save it to a local directory (for testing). Now the image path in the Error object points to a path that leads to the local directory. This functionality has been tested and works fine. My problem showes up when I want to delete a bug from the list. I am getting that very infamous "IO Exception" saying that the image I want to delete is being used by another process.</p> <p><strong>So Far:</strong> At first I tried very elegant solutions, but as with all things you get to a point where you just want to see if you can get the thing to even work at all. So I am at the point where most of the code I am using is experimental and radical. So please when looking at it note that the code being used is out of desperation, so any "simple" solutions have probably already been tried (I did research this a lot becuase I hate having to do this). Things i can think of off the top of my head are the obsurd amount of disposes and forced garbage collections being called so please to not comment on the negative nature of this practice, I am well aware :).</p> <p><strong>The Code</strong></p> <p><em>Saving image to local directory</em></p> <pre><code>public void OnBrowseClick() { Microsoft.Win32.OpenFileDialog openBox = new Microsoft.Win32.OpenFileDialog(); // Show dialog box to user and grab output Nullable&lt;bool&gt; result = openBox.ShowDialog(); if (result == true) { // Create temp variable to hold local path string string localPath = Directory.GetCurrentDirectory(); // Grab the extension of the specified file path string extension = openBox.FileName.Substring(openBox.FileName.LastIndexOf("\\")); // Add extension to local path localPath += extension; // Create local copy of image at given file path (being ridiculous at this point) using (Stream stream = new FileStream(openBox.FileName, FileMode.Open, FileAccess.ReadWrite)) { using (Bitmap bmp = LoadImage(stream)) { using (Bitmap temp = (Bitmap)bmp.Clone()) { temp.Save(localPath); temp.Dispose(); } bmp.Dispose(); } stream.Dispose(); } // Set the URL in the image text box (UI stuff) LocalError.ImagePath = localPath; } } </code></pre> <p><em>The following is the <strong>LoadImage</strong> function that is used in the function above</em></p> <pre><code>private Bitmap LoadImage(Stream stream) { Bitmap retval = null; using (Bitmap bitmap = new Bitmap(stream)) { retval = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat); using (Graphics gdi = Graphics.FromImage(retval)) { gdi.DrawImageUnscaled(bitmap, 0, 0); gdi.Flush(); gdi.Dispose(); bitmap.Dispose(); } } // Garbage collection here to be safe GC.WaitForPendingFinalizers(); GC.Collect(); return retval; } </code></pre> <p><em>And finally we come to where I try to <strong>delete</strong> the image</em></p> <pre><code>public void OnDeleteClick() { // Ask user to make sure they want to delete selected item(s) MessageBoxResult result = MessageBox.Show("Are you sure you want to delete selected item(s) from the list?", "Delete", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { for( int i = 0; i &lt; Parent.ErrorListControl.ErrorDataGrid.SelectedItems.Count; ++i) { // Get path to image string path = (Parent.ErrorListControl.ErrorDataGrid.SelectedItems[i] as Error).ImagePath; // Even tried calling garbage collection here!!!!! System.GC.WaitForPendingFinalizers(); System.GC.Collect(); File.Delete(path); // Remove the error from the error list Parent.ErrorListVM.ErrorList.Remove((Error)Parent.ErrorListControl.ErrorDataGrid.SelectedItems[i]); // Decrement counter because we altered the list while in a loop i--; } } } </code></pre> <p><strong>Notes:</strong> If anyone would like me to explain anything further or if you need to know something I left out please just ask I will get back to you ASAP! Any suggestions are helpful at this point I have absolutley no idea what I am doing wrong. I generally only program in a C++ environment so I tend to manage my own memory this whole "garbage collection" thing is really throwing a wrench in our project! (Off topic note: I do not know why I am not getting any color highlighting so I apologize to anyone who takes the time to read this).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload