關閉正在使用的文件,這是我使用這個代碼爲Excel轉換成圖像和一個圖片預覽。代碼正在爲第一次工作。但是當我試圖上傳第二次時,我得到一個錯誤,說圖像文件正在使用,特別是在保存點。從一個PictureBox
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "bak files (*.xls)|*.xls|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Img1 = openFileDialog1.FileName;
//Create a new Workbook object and
//Open a template Excel file.
Workbook book = new Workbook(Img1);
//Get the first worksheet.
Worksheet sheet = book.Worksheets[0];
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
//Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = true;
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
Bitmap bitmap = sr.ToImage(0);
//Save the image file specifying its image format.
bitmap.Save("C:\\1.jpg");\\in this point i get my error that it says general error GDI+.
pictureBox2.Image = Image.FromFile("C:\\1.jpg");
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
}
else
{
this.DialogResult = DialogResult.None;
}
}
我的事情,我必須停止使用首先從我的PictureBox的預覽,然後重新上傳。但我將如何做到這一點?我試過
pictureBox1.Image=null
,但它沒有奏效。
您需要檢查是否存在1.JPG如果是刪除它,然後將其保存沒有問題。 – Sorceri
如果我將嘗試刪除它表示該文件已被其他進程使用。它只適用於我將重新運行我的項目。我認爲問題在這裏pictureBox2.Image = Image.FromFile(「C:\\ 1.jpg」); pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; – DIMITRIS
您需要像Johan建議的那樣處理picturebox圖像:if(pictureBox2.Image!= null)pictureBox2.Image.Dispose(); – Sorceri