我有一個應用程序,它會裁剪圖像並保存它
該過程是加載圖像,裁剪它,刪除原始圖像(所以我可以替換它),並保存它。
這是我的代碼:IOException未處理
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); }
}
}
這是用於定位將用於裁剪圖像的邊緣。
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");
}
,這是圖像
錯誤的裁剪和保存顯示在行System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg"
它說
該進程無法訪問該文件「C:\用戶。 ... \ t2.jpg',因爲它正在被另一個進程使用。
我想看看我在哪裏錯了幾天,仍然沒有。
請幫幫我。
可能重複(http://stackoverflow.com/questions/3661799/file-delete-failing-when-image-fromfile-was-called-before-it-though-making-copy) – BrokenGlass 2012-02-12 17:13:57
請僅發佈代碼的相關位。 – svick 2012-02-12 17:14:25
@svick - 我想問題是在那裏,我不能指出它。所以我張貼了所有的代碼,以便你們可以告訴我哪一個會導致問題:) – 2012-02-12 17:37:10