0

i have a application which crops an image and save it
the process was to load an image, crop it, delete the original image(so i can replace it) and, save it.
this is my code:

private void DetectSize(object sender, EventArgs e)  
{  
int x = 1;  
Bitmap TempImage = new Bitmap(@cwd + "\\t" + (x + 1) + ".jpg", true);  
        pictureBox.Image = (Image)TempImage.Clone();  
        TempImage.Dispose();  
        Bitmap imgPart = new Bitmap(pictureBox.Image);  
        int imgHeight = imgPart.Height;  
        int imgWidth = imgPart.Width;  
        HalfWidth = imgWidth / 2;  
        MaxWidth = imgWidth;  
        try  
        {  
            Bitmap imgPart1 = new Bitmap(pictureBox.Image);  
            Color c;  
            for (int i = 0; i < imgPart1.Width; i++)  
            {  
                for (int j = 0; j < imgPart1.Height; j++)  
                {  
                    c = imgPart1.GetPixel(i, j);  
                    string cn = c.Name;  
                    for (int z = 0; z <= 9; z++)  
                    {  
                        if (z < 10)  
                        {  
                            if (cn == "ff00000" + z)  
                            {  
                                if (i < HalfWidth)  
                                {  
                                    MinWidth = i;  
                                }  
                                else  
                                {  
                                    if (i < MaxWidth)  
                                    {  
                                        MaxWidth = i;  
                                    }  
                                }  
                            }  
                        }  
                        else  
                        {  
                            if (cn == "ff0000" + z)   
                            {  
                                if (i < HalfWidth)  
                                {  
                                    MinWidth = i;  
                                }  
                                else  
                                {    
                                    if (i < MaxWidth)  
                                    {  
                                        MaxWidth = i;  
                                    }  
                                }  
                            }  
                        } 
                    }  
                }  
            }  
            MinWidth += 1;  
            MaxWidth -= 1;  
            MaxWidth = imgWidth - MaxWidth;  
            imgPart1.Dispose();  
            imgPart.Dispose();  
            lblLeftMargin.Text = Convert.ToString(MinWidth);  
            lblRightMargin.Text = Convert.ToString(MaxWidth);  

        }  
        catch (Exception ex) { MessageBox.Show(ex.Message); }  
        }  
    }  

This is for locating the margins that will be used to crop the image.

private void CropSave(object sender, EventArgs e)
    {
        int x = 1;
        Bitmap croppedBitmap = new Bitmap(pictureBox.Image);

        croppedBitmap = croppedBitmap.Clone(
        new Rectangle(
            MinWidth, 0,
            (int)croppedBitmap.Width - MinWidth - MaxWidth,
            1323),
        System.Drawing.Imaging.PixelFormat.DontCare);

        if (System.IO.File.Exists(@cwd + "\\t" + (x + 1) + ".jpg"))
            System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg");

        croppedBitmap.Save(@cwd + "\\t" + (x + 1) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        croppedBitmap.Dispose();
        MessageBox.Show("File " + (x + 1) + "Done Cropping");
    }  

and this is for cropping and saving of image

The error shows at line System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg"

it says

The process cannot access the file 'C:\Users....\t2.jpg' because it is being used by another process.

I'm trying to look where i have been wrong for days, and still nothing.
Please help me.

svick
  • 236,525
  • 50
  • 385
  • 514
Ozarraga_AB
  • 939
  • 5
  • 15
  • 24
  • possible duplicate of [File.Delete failing when Image.FromFile was called prior it, despite making copy of loaded image and destroying original one](http://stackoverflow.com/questions/3661799/file-delete-failing-when-image-fromfile-was-called-prior-it-despite-making-copy) – BrokenGlass Feb 12 '12 at 17:13
  • 1
    Please post only relevant bits of your code. – svick Feb 12 '12 at 17:14
  • @svick - i guess that the problem was in there and i can't pin point it. so i posted all the codes so you guys can tell me which one in there causes the problem :) – Ozarraga_AB Feb 12 '12 at 17:37
  • @kazu.zushifukato, in such cases, you can remove code bit by bit, until you find which part causes the problem. If you still can't figure it out, post it here. – svick Feb 12 '12 at 17:41

1 Answers1

4
    Bitmap TempImage = new Bitmap(@cwd + "\\t" + (x + 1) + ".jpg", true);  
    pictureBox.Image = (Image)TempImage.Clone();  
    TempImage.Dispose();  

The Clone() method doesn't do what you hope it does. It still keeps a lock on the file, the memory mapped file object is shared between the two image objects. Disposing the first one just closes one handle on the object, the pictureBox.Image object still has the other handle opened. Write it like this instead:

    pictureBox.Image = new Bitmap(TempImage); 
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536