繁体   English   中英

位图加载内存使文件大小增加 10 倍

[英]bitmap loaded memory increase 10x the size of the file

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

这是下面代码的输出

加载文件 img_6.jpg 原来是 513kb

图像 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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM