IOException未处理 [英] IOException was unhandled

查看:114
本文介绍了IOException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以裁剪图像并保存图像的应用程序
该过程是加载图像,裁剪图像,删除原始图像(以便我可以替换它)并保存.
这是我的代码:

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

错误显示在行System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg"

该进程无法访问文件'C:\ Users .... \ t2.jpg',因为该文件正在被另一个进程使用.

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.

推荐答案

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

Clone()方法无法实现您希望的效果.它仍然对文件保持锁定,内存映射文件对象在两个图像对象之间共享.放置第一个只是关闭对象上的一个句柄,pictureBox.Image对象仍然打开了另一个句柄.改为这样写:

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); 

这篇关于IOException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆