C# 溪流;文件在另一个程序中打开;
我在这段代码中找不到错误 每次检查都会出现“文件在另一个程序中打开”的错误 我想有些小溪我还没有处理好C# 溪流;文件在另一个程序中打开;,c#,stream,C#,Stream,我在这段代码中找不到错误 每次检查都会出现“文件在另一个程序中打开”的错误 我想有些小溪我还没有处理好 public static void CheckResolution(string imagePath) { var image = LoadSigleImageFromFile(imagePath); var baseArea = 2600 * 1000;//dimenzioni in risoluzione FileStream s
public static void CheckResolution(string imagePath)
{
var image = LoadSigleImageFromFile(imagePath);
var baseArea = 2600 * 1000;//dimenzioni in risoluzione
FileStream stream = new FileStream(image.FileInfo.FullName, FileMode.Open, FileAccess.ReadWrite);
try
{
Image img = Image.FromStream(stream);
var imageArea = img.Height * img.Width;
if (imageArea >= baseArea)
{
var scaleFactor = (imageArea / baseArea);
var newVerticalRes = (int)(img.Height / scaleFactor);
var newHorizontalRes = (int)(img.Width / scaleFactor);
var newImage = ResizeImage(img, new Size(newHorizontalRes, newVerticalRes));
if (File.Exists(imagePath))
File.Delete(imagePath);
newImage.Save(imagePath, ImageFormat.Jpeg);
}
}
catch (Exception ex)
{
logger.Error("errore scala foto : " + ex.Message);
//if (Boolean.Parse(ConfigurationManager.AppSettings["StopOnException"]))
throw new Exception("CheckResolution errore scala foto : " + ex.Message);
}
finally
{
stream.Dispose();
}
}
这里是单曲。。。作用
public static ImageFromFile LoadSigleImageFromFile(string file)
{
var ris = new ImageFromFile();
FileInfo fileInfo = new FileInfo(file);
if (fileInfo.Name != "Thumbs.db")
ris = (new ImageFromFile() { FileInfo = fileInfo });
return ris;
}
更新ResizeImage函数
private static Image ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = (int)imgToResize.Width;
int sourceHeight = (int)imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
b.SetResolution(imgToResize.HorizontalResolution, imgToResize.VerticalResolution);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
private static Image ResizeImage(图像imgToResize,大小)
{
int sourceWidth=(int)imgToResize.Width;
int sourceHeight=(int)imgToResize.Height;
浮动百分比=0;
浮动nPercentW=0;
浮点数nPercentH=0;
nPercentW=((float)size.Width/(float)sourceWidth);
nPercentH=((浮点)size.Height/(浮点)sourceHeight);
如果(nPercentH
很明显,在您的代码中,这段行
if (File.Exists(imagePath))
File.Delete(imagePath);
尝试删除上面的流打开的相同文件。您应该仅在关闭之前打开的流之后才尝试删除文件(以及以下保存)
我可以建议这些改变
public static void CheckResolution(string imagePath)
{
// Flag becomes true if the resize operation completes
bool resizeCompleted = false;
Image newImage = null;
// If file doesn't exist the code below returns null.
var image = LoadSigleImageFromFile(imagePath);
if(image == null) return;
var baseArea = 2600 * 1000;//dimenzioni in risoluzione
using(FileStream stream = new FileStream(image.FileInfo.FullName, FileMode.Open, FileAccess.ReadWrite))
{
try
{
Image img = Image.FromStream(stream);
var imageArea = img.Height * img.Width;
if (imageArea >= baseArea)
{
... resize ops ....
// if (File.Exists(imagePath))
//File.Delete(imagePath);
// newImage.Save(imagePath, ImageFormat.Jpeg);
// Set the flag to true if resize completes
resizeCompleted = true;
}
}
catch (Exception ex)
{
logger.Error("errore scala foto : " + ex.Message);
throw new Exception("CheckResolution errore scala foto : " + ex.Message);
}
}
// Now you can delete and save....
if(resizeCompleted)
{
// No need to check for existance. File.Delete doesn't throw if
// the file doesn't exist
File.Delete(imagePath);
newImage.Save(imagePath, ImageFormat.Jpeg);
}
}
public static ImageFromFile LoadSigleImageFromFile(string file)
{
// Check if the file exists otherwise return null....
var ris = null;
if(File.Exists(file))
{
FileInfo fileInfo = new FileInfo(file);
if (fileInfo.Name != "Thumbs.db")
ris = (new ImageFromFile() { FileInfo = fileInfo });
}
return ris;
}
在using块的右括号关闭后移动删除和保存操作,即表示文件不再被您自己的程序锁定,您可以继续执行删除和保存操作
还请注意,在输入此代码之前,您应该检查输入文件是否存在,否则将出现异常。错误在哪一行?另外,为什么要缩进括号中的方法?它们应该与方法声明的开头对齐。请继续创建。我打赌您正在尝试删除为您的
流打开的完全相同的文件,并且,您应该使用using
语句来获取可使用的资源。我还没有确定您尝试打开两次的文件是什么,但问题几乎肯定是由于GDIImage
对象(由Image.FromFile()
等创建)保持其底层流的打开状态。也就是说,Image.FromFile(path)
将在path
处打开指向该文件的流,该流在您处理该图像之前不会关闭。in Image img=Image.fromfstream(stream);我有另一个流,我也必须处理这个流?您有任何错误吗?流在using块的出口处被处理,因此,不,您不需要处理流实例