[英]Is Image guaranteed to be disposed in this method?
鑒於resizedImage
的類型為System.Drawing.Image
的代碼:
using (var resizedImage = Resizer.ResizeImage(bytes, requestedWidth))
{
return PNGCompressor.LosslesslyCompressPNG(resizedImage);
}
該方法定義為:
public static byte[] LosslesslyCompressPNG(Image image)
{
var fileName = Guid.NewGuid();
var inputFilePath = TempCompressionFolder + fileName + ".png";
image.Save(inputFilePath, ImageFormat.Png);
var outputFilePath = TempCompressionFolder + fileName + "_comp.png";
var startInfo = new ProcessStartInfo
{
FileName = PathToOptiPNGExe,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
Arguments = "\"" + inputFilePath + "\" -out \"" + outputFilePath + "\" -o2 -strip all"
};
using (var process = Process.Start(startInfo))
{
if (process == null)
{
throw new Exception("Could not start " + PathToOptiPNGExe);
}
process.WaitForExit(Settings.Executables.WaitForExitMS);
if (!process.HasExited)
{
process.Kill();
}
}
var bytes = File.ReadAllBytes(outputFilePath);
File.Delete(outputFilePath);
File.Delete(inputFilePath);
return bytes;
}
resizedImage
總是會被處理掉嗎? 我想知道這是否是由於新進程正在啟動而導致內存泄漏的原因。
using
語句保證即使在發生異常的情況下也會調用Dispose
。
您開始新流程的事實不會對此實例做任何事情,因為您沒有使用帶有此新流程僅string
參數的實例。 即使您正在使用此實例(通過流參數或其他方式), using
語句也將負責處理和釋放該實例。
順便說一句,我的理解是您使用 optipng 工具僅用於“剝離”元數據。 有許多其他方法可以做到這一點,而無需在磁盤上創建文件並啟動新進程。 例如,查看從圖像中清除元數據的簡單方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.