简体   繁体   中英

bitmap loaded memory increase 10x the size of the file

I am loading an image and i get an increase of memory around 10x the size of the file.

this is the output for the code below

Loading file img_6.jpg with originally 513kb

Memory for image img_6.jpg is 63704kb (124.12x)

Thanks!

this is the code i use to load

      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);

these are the memory log helpers

      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;

Ok right, the reason there are different file formats was behind it. Irfanview helped a lot with pressing I opening image information shows file and loaded memory size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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