我一直在嘗試解決這個問題,但找不到解決方案。
當我復制時,記憶體使用量超過 2GB 或更多,而且我的筆記本電腦死機了。我可以做什么來釋放 RAM?
這個程式對水平和垂直照片進行排序,所以沒什么大不了的,我知道代碼不是最好的。
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
string Name = Path.GetFileName(srcPath);
//Gets the file's format (like png or jpeg)
string ext = Path.GetExtension(srcPath);
bool allowFile = false;
//This line examines the correct file's format, if it's not correct it won't copy it.
if (ext == ".png" || ext == ".jpeg" || ext == ".jpg" || ext == ".mp4" || ext == ".PNG" || ext == ".JPEG" || ext == ".JPG" || ext == ".MP4")
allowFile = true;
Image img = Image.FromFile(srcPath);
int width = img.Width;
int height = img.Height;
if (allowFile)
{
if (height > width)
{
File.Copy(srcPath, pathString "\\" Name , true);
}
else
{
File.Copy(srcPath, pathString2 "\\" Name, true);
}
//Copy the file from sourcepath and place into mentioned target path,
}
}
uj5u.com熱心網友回復:
您需要在使用后處理您的影像,以便從Image.FromFile呼叫中釋放使用的記憶體。
嘗試將您的影像呼叫包裝在一個using:
using (Image img = Image.FromFile(srcPath))
{
int width = img.Width;
int height = img.Height;
if (allowFile)
{
if (height > width)
{
File.Copy(srcPath, pathString "\\" Name , true);
}
else
{
File.Copy(srcPath, pathString2 "\\" Name, true);
}
//Copy the file from sourcepath and place into mentioned target path,
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/344023.html
標籤:C# 视窗 桌面应用程序 .net-framework-4.8
