我正在加载一个图像,内存增加了大约文件大小的10倍。
这是下方代码的输出
用513kb 加载文件img_6.jpg
图像img_6.jpg的内存为63704kb(124.12x(
谢谢!
这是我用来加载的代码
string filename = "img_6.jpg";
Directory.SetCurrentDirectory(@"C:UsersAdminDesktopisolated images");
Helpers.membefore(filename);
BitmapImage bitmap = new BitmapImage();
//using (Stream stream = new FileStream(filepath, FileMode.Open))
{
bitmap.BeginInit();
//bitmap.StreamSource = stream;
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(filename, UriKind.Relative);
bitmap.EndInit();
}
ImageBrush ib = new ImageBrush() { ImageSource = bitmap };
Helpers.memafter(ib);
这些是内存日志助手
public static void memafter(object result)
{
//debugging
if (result.GetType() == typeof(ImageBrush) && (result as ImageBrush).ImageSource != null)
{
var after = Process.GetCurrentProcess().VirtualMemorySize64;
string factor = ((double)(after - before) / (double)len).ToString("N") ;
string source = (result as ImageBrush).ImageSource.ToString();
Console.WriteLine(String.Format("Memory for image {0} is {1}kb ({2}x)",
source,
(after - before) / 1024,
factor.ToString()));
string s = result.ToString();
}
}
public static void membefore(string xaml)
{
FileInfo fi = new FileInfo(xaml);
len = fi.Length;
Console.WriteLine(string.Format("Loading file {0} with originally {1}kb", xaml, fi.Length / 1024));
if (xaml.Contains("ico") || xaml.Contains("jpg") || xaml.Contains("bmp") || xaml.Contains("png"))
{
before = Process.GetCurrentProcess().VirtualMemorySize64;
}
}
private static long len = 0;
static long before = 0;
好吧,背后的原因是存在不同的文件格式。Irfanview对按下I打开图像信息显示文件和加载的内存大小有很大帮助。