0

我正在加载图像,并且内存增加了大约 10 倍于文件大小。

这是下面代码的输出

加载最初为 513kb 的文件 img_6.jpg

图像 img_6.jpg 的内存为 63704kb (124.12x)

谢谢!

这是我用来加载的代码

      string filename = "img_6.jpg";

        Directory.SetCurrentDirectory(@"C:\Users\Admin\Desktop\isolated 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;
4

1 回答 1

0

好吧,有不同文件格式的原因是它的背后。Irfanview 对按 I 打开图像信息显示文件和加载的内存大小有很大帮助。

于 2020-09-27T19:56:18.687 回答