locked
Why is this code not working? RRS feed

  • Question

  • I have some code to set save an image:

    Process.Start(@"C:\Users\MicroNugget II\Documents\Visual Studio 2015\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.exe");
                              newImage.Save(@"C:\Users\MicroNugget II\Desktop\Otto\DesktopImage.png", ImageFormat.Png);

    I also have some code in VB.NET that was called in the Process.Start above (for some reason vb.net's System.IO is better)

    Imports System.IO
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If (File.Exists("C:\Users\MicroNugget II\Desktop\Otto\DesktopImage.png")) Then
                File.Delete("C:\Users\MicroNugget II\Desktop\Otto\DesktopImage.png")
    
            End If
    
    
            If (File.Exists("C:\Users\MicroNugget II\Desktop\Otto\newtiled.png")) Then
                File.Delete("C:\Users\MicroNugget II\Desktop\Otto\newtiled.png")
    
            End If
            If (File.Exists("C:\Users\MicroNugget II\Desktop\Otto\newtiled1.png")) Then
                File.Delete("C:\Users\MicroNugget II\Desktop\Otto\newtiled1.png")
    
            End If
            If (File.Exists("C:\Users\MicroNugget II\Desktop\Otto\newtiled2.png")) Then
                File.Delete("C:\Users\MicroNugget II\Desktop\Otto\newtiled2.png")
    
            End If
            End
        End Sub
    End Class

    The purpose of this code is to delete the image that is being saved in multiple lines of code,

    however, I always get some kind of error from the VB.NET application and the c# ones above.

    Errors for VB.NET:

    File is being accessed by another process.

    C#:

    A generic error occurred in GDI+.

    Tuesday, December 6, 2016 9:56 PM

Answers

  • Thanks for the help, everyone, but I have fixed the problem myself. I fixed it by restarting the form that sets the image, every time an error occurs in the form. I have no Idea how this worked, but it did.

    • Marked as answer by FakeTuxedo1 Sunday, December 18, 2016 10:59 PM
    Sunday, December 18, 2016 10:59 PM

All replies

  • I would say the code that "saves" the file is not disposed correctly

    thus still has the png files locked

    see link for more reading

    http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being

    Tuesday, December 6, 2016 10:28 PM
  • I would say the code that "saves" the file is not disposed correctly

    thus still has the png files locked

    see link for more reading

    http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being

    I have tried the link on both my VB.NET code (using a vb.net code converter) and my C# code but it does not work.


    Wednesday, December 7, 2016 9:14 PM
  • Hi,

    Any update? Is your issue resolved?


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, December 12, 2016 12:23 PM
  • The common reason for this is that a developer created say a image, saved then not call Dispose afterwards

    See example here, in the example the format is different but that does not matter.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, December 12, 2016 1:27 PM
  • No, I was waiting for another response.
    Monday, December 12, 2016 2:20 PM
  • The common reason for this is that a developer created say a image, saved then not call Dispose afterwards

    See example here, in the example the format is different but that does not matter.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    When I dispose the image, I get the same error.
    Monday, December 12, 2016 3:04 PM
  • If File.Delete does not work even in your first program, just before Process.Start, then the problem is not necessarily in your second program.

    Show some details about the usage of the images in your first program and make sure that the data are not held by some unclosed streams, Picture controls, Visual Studio, graphical editors, etc.

    Monday, December 12, 2016 6:43 PM
  • Use a file stream to get the image. 

    Dim fs As New System.IO.FileStream("C:\bliss.jpg", IO.FileMode.Open) Dim bm As New Bitmap(fs)
    fs.close

    System.IO.FileStream fs = new System.IO.FileStream("C:\bliss.jpg", IO.FileMode.Open);
    Bitmap bm = new Bitmap(fs);
    fs.close();



    Success
    Cor



    Monday, December 12, 2016 7:06 PM
  • Thanks for the help, everyone, but I have fixed the problem myself. I fixed it by restarting the form that sets the image, every time an error occurs in the form. I have no Idea how this worked, but it did.

    • Marked as answer by FakeTuxedo1 Sunday, December 18, 2016 10:59 PM
    Sunday, December 18, 2016 10:59 PM