我试图通过替换现有文件来调整上传图像的大小并将其保存在特定位置。但这给了我一个错误,说“该进程无法访问文件'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,它会给我同样的问题。任何帮助,将不胜感激。谢谢