无法删除图像(正在使用)C#
Cannot delete image (in use) C#
我在这里要做的是:
- 下载图片
- 将其保存到磁盘
- 调整大小等
- 用新名称保存
- 删除旧图片
我已经实现了一切,但还没有完成最后一步。我收到 "file in use" 错误。
这是代码:
filenameb = "img-1b.jpg";//old img
fullpathb = Path.Combine(dir, filenameb);//old img path
//downloading using imglink
client.DownloadFile(imglink, fullpathb);//save old img as %dir%\img-1b.jpg
//until here I downloaded the "old" img
filename = "img-1.jpg";//new img
fullpath = Path.Combine(dir, filename);//new img path
//name and fullpath for the "new img" are set
Image imgresize = Image.FromFile(fullpathb);//give old img path
imgresize = FixedSize(imgresize);//resize
imgresize.Save(fullpath, ImageFormat.Jpeg);//save new img as %dir%\img-1.jpg
//EVERYTHING WORKS PERFECTLY UP TO HERE
imgresize.Dispose();//dispose -has old img path
System.IO.File.Delete(fullpathb);//delete old img
我也把图片配置成FixedSize。
如果需要,这是 "FixedSize" 的代码:
//kind of messed up to save some space
static Image FixedSize(Image imgPhoto)
{
int Width = 300;int Height = 250;
int sourceWidth = imgPhoto.Width;int sourceHeight = imgPhoto.Height;
int sourceX = 0;int sourceY = 0;int destX = 0;int destY = 0;
float nPercent = 0;float nPercentW = 0;float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW){ nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);}
else { nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2); }
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
我对此真的很陌生,我无法意识到我做错了什么(或者我根本没有在做什么)。感谢您的帮助!
Image imgresize = Image.FromFile(fullpathb)
这将锁定文件。相反,从文件中读取的字节的 MemoryStream 创建一个图像
byte[] imageBytes = FileReadAllBytes(fullpathb);
using (var ms = new MemoryStream(imageBytes)){
var image = Image.FromStream(ms);
}
未测试
我在这里要做的是:
- 下载图片
- 将其保存到磁盘
- 调整大小等
- 用新名称保存
- 删除旧图片
我已经实现了一切,但还没有完成最后一步。我收到 "file in use" 错误。
这是代码:
filenameb = "img-1b.jpg";//old img
fullpathb = Path.Combine(dir, filenameb);//old img path
//downloading using imglink
client.DownloadFile(imglink, fullpathb);//save old img as %dir%\img-1b.jpg
//until here I downloaded the "old" img
filename = "img-1.jpg";//new img
fullpath = Path.Combine(dir, filename);//new img path
//name and fullpath for the "new img" are set
Image imgresize = Image.FromFile(fullpathb);//give old img path
imgresize = FixedSize(imgresize);//resize
imgresize.Save(fullpath, ImageFormat.Jpeg);//save new img as %dir%\img-1.jpg
//EVERYTHING WORKS PERFECTLY UP TO HERE
imgresize.Dispose();//dispose -has old img path
System.IO.File.Delete(fullpathb);//delete old img
我也把图片配置成FixedSize。 如果需要,这是 "FixedSize" 的代码:
//kind of messed up to save some space
static Image FixedSize(Image imgPhoto)
{
int Width = 300;int Height = 250;
int sourceWidth = imgPhoto.Width;int sourceHeight = imgPhoto.Height;
int sourceX = 0;int sourceY = 0;int destX = 0;int destY = 0;
float nPercent = 0;float nPercentW = 0;float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW){ nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);}
else { nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2); }
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
我对此真的很陌生,我无法意识到我做错了什么(或者我根本没有在做什么)。感谢您的帮助!
Image imgresize = Image.FromFile(fullpathb)
这将锁定文件。相反,从文件中读取的字节的 MemoryStream 创建一个图像
byte[] imageBytes = FileReadAllBytes(fullpathb);
using (var ms = new MemoryStream(imageBytes)){
var image = Image.FromStream(ms);
}
未测试