Home > database >  How to display image in WinForms and then delete?
How to display image in WinForms and then delete?

Time:07-03

When my form opens it uses the file path that is provided from another form and displays the images, and when I close the form I want it to delete all the images.

But when I close it keeps stating the file is being used, is there a better way of doing it please?

public frmMark(string strImagePath)
{
    InitializeComponent();
    pbImage1.Image = new Bitmap(strImagePath   "1.jpg");
    pbImage2.Image = new Bitmap(strImagePath   "2.jpg");
    pbImage3.Image = new Bitmap(strImagePath   "3.jpg");
}

private void frmMark_FormClosing(object sender, FormClosingEventArgs e)
{
    File.Delete(deletepath   "1.jpg");
}

CodePudding user response:

Change this:

pbImage1.Image = new Bitmap(strImagePath   "1.jpg");

to this:

pbImage1.ImageLocation = Path.Combine(strImagePath, "1.jpg");

and for the others and your issue should go away because the files will not be locked, so you don't have to worry about unlocking them.

CodePudding user response:

Perhaps create a language extension which clones the original image, load the image then deletes the physical file.

public static class PictureBoxExtensions
{
    public static void LoadClone(this PictureBox pictureBox, string fileName)
    {
        var imageOriginal = Image.FromFile(fileName);

        var imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);

        Graphics gr = Graphics.FromImage(imageClone);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
        gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
        gr.Dispose();
        imageOriginal.Dispose();

        pictureBox.Image = imageClone; // assign the clone to picture box
        File.Delete(fileName); // remove original
    }
}

Or if not always needing to remove an image add another parameter,in this case remove where the default is to remove which of course you can set the default to false.

public static class PictureBoxExtensions
{
    public static void LoadClone(this PictureBox pictureBox, string fileName, bool remove = true)
    {
        var imageOriginal = Image.FromFile(fileName);

        var imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);

        Graphics gr = Graphics.FromImage(imageClone);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
        gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
        gr.Dispose();
        imageOriginal.Dispose();

        pictureBox.Image = imageClone; // assign the clone to picture box

        if (remove)
        {
            File.Delete(fileName); // remove original
        }
        
    }
}

Usage

public frmMark(string strImagePath)
{
    InitializeComponent();
    pbImage1.LoadClone(strImagePath   "1.jpg");
    pbImage2.LoadClone(strImagePath   "2.jpg");
    pbImage2.LoadClone(strImagePath   "3.jpg");
}   
  • Related