1
我嘗試調整上傳的圖像的大小並通過替換現有文件將其保存在特定位置。但是這給了我一個錯誤:「進程無法訪問文件'C:\ Photo.jpg',因爲它正在被另一個進程使用。」該進程無法訪問文件'C: Photo.jpg',因爲它正在被另一個進程使用
這是我的代碼。
private void llblChangePlayerPicture_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ofdPlayerPicture.Reset();
ofdPlayerPicture.Filter = "JPEG(*.jpeg,*.jpg)|*.jpeg;*.jpg|Bitmap (*.bmp)| *.bmp |PNGs(*.png)|*.png";
ofdPlayerPicture.ShowDialog();
Image newImage;
if (ofdPlayerPicture.FileName != "")
{
pboPlayerPicture.Image = null;
pboPlayerPicture.ImageLocation = null;
newImage = Resize(Image.FromFile(ofdPlayerPicture.FileName), new Size(250, 100));
SaveJpeg(Program.playerImagePath + regNo + "\\Photo1.jpg", newImage, 100);
}
這裏是我保存圖像
public static void SaveJpeg(string path, Image img, int quality)
{
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
System.IO.MemoryStream mss = new System.IO.MemoryStream();
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
img.Save(mss, jpegCodec, encoderParams);
byte[] matriz = mss.ToArray();
fs.Write(matriz, 0, matriz.Length);
mss.Close();
fs.Close();
mss.Dispose();
fs.Dispose();
}
櫃面我想用File.Delete功能刪除Photo.jpg,它給了我同樣的問題。任何幫助,將不勝感激。 Thanx