0

I've searched and read a lot of the different ways to do this... and tried many of them.

When the program loads it loads printmark.png into the picture box.
Of course every time I try to delete the PNG file it says it's in use. As you can see I've tried both the Image.FileFrom and the picturebox.Load method.

This is the code I have.

private void GetCurrentLogos()
    {
        Image CurrentWM = Image.FromFile(@"C:\pics\logo.png");
        Image CurrentPM = Image.FromFile(@"C:\pics\printmark.png");

        pbWatermark.Image = CurrentWM;
        pbPrintmark.Image = CurrentPM;

        //pbWatermark.Load(@"C:\pics\logo.png");
        //pbPrintmark.Load(@"C:\pics\printmark.png");
    }

    private void btnSetPM_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {
            ListViewItem item = listView1.SelectedItems[0];
            txtNewPM.Text = item.Tag.ToString();
            pbPrintmark.Image.Dispose();
            pbPrintmark.Image = null;
            pbPrintmark.Refresh();
            Application.DoEvents();
            renameMark("printmark.png", txtNewPM.Text);
        }
    }

    private void renameMark(string MarkType, string FileName)
    {
        string path = txtPath.Text;
        string FullSource = path + FileName;
        string FullDest = path + MarkType;

        if(File.Exists(FullDest))
        {
            File.Delete(FullDest);
        }

        System.IO.File.Copy(FullSource, FullDest);
    }
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Matt Winer
  • 495
  • 9
  • 26

3 Answers3

0

See Image.FromFile():

The file remains locked until the Image is disposed.

To get around this, pass the returned image to a new Bitmap so that the original lock gets released:

        Image tmp = Image.FromFile(@"C:\pics\logo.png");
        Image CurrentWM = new Bitmap(tmp);
        tmp.Dispose();
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thank you... that is probably one way I didn't try. Any reason you can think of that after I delete the file. Then I re-run the GetCurrentLogos() function. If I try to run the delete again it doesn't work unless I close the program and start over. Could the IO.Copy.File be locking the file? – Matt Winer Jan 09 '15 at 03:46
0

As noted in some of the answers to this question, an image created by Image.FromFile keeps the underlying file open. This is by design, as the MSDN documentation says ("The file remains locked until the Image is disposed."). You can get around this by loading the file into a MemoryStream, then creating the image from that stream.

Community
  • 1
  • 1
adv12
  • 8,443
  • 2
  • 24
  • 48
0

tmp.Dispose(); didnt worked for me so maybe u need also a different solution.

I used it for my dataGridView_SelectionChanged:

private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
    var imageAsByteArray = File.ReadAllBytes(path);
    pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}
Deniz
  • 429
  • 1
  • 4
  • 19