4

I am drag and drop a heavy image (95729 kb) in my RichTextBox. But memory usage is so incomprehensible: memory usage Why it stores more than 700 mb?

My Drag and Drop code:

private void RtbEditor_PreviewDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files != null && files.Length > 0)
        {
            foreach (var file in files)
            {
                // Filter out non-image files.
                if (IsValidImageFile(file))
                {
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();            
                    bitmap.UriSource = new Uri(file, UriKind.Absolute);            
                    bitmap.EndInit();
                    Image image = new Image();            
                    image.Source = bitmap;            
                    var container = new InlineUIContainer(image, rtbEditor.CaretPosition);
                    rtbEditor.CaretPosition = container.ElementEnd;
                }
            }
        }
    }
}

For image check, I took only image header: Check only header What am I doing wrong?

Il Vic
  • 5,576
  • 4
  • 26
  • 37
Roman
  • 127
  • 9

2 Answers2

4

This heavy image probably has an enormous resolution and uses a format like PNG or JPEG. These formats compress the pixel data to make the final file size smaller. However, in order to render an image, the renderer needs to decompress the image into actual pixels with (A)RGB values.

An uncompressed 3000x3000 image with 32 bits per pixel weighs in at ~ 36 MB. So your image must be even larger.

Why do you want to render such a large image in a textbox anyway?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • To be honest, it is not necessary in most of cases. But I don’t understand why it occupies so much memory while I load just ~100 mb jpeg image. I wanted to make RichTextBox works like Microsoft Word. Maybe I am idiot:) sorry for my English – Roman Apr 10 '19 at 09:21
  • An 100 MB JPEG is HUGE. If you want your users to be able to drag images into your text editor, you'll have to resize the image to something more handleable. – CodeCaster Apr 12 '19 at 07:16
1

You definitely don't need that much of pixels. Just set BitmapImage.DecodePixelHeight or BitmapImage.DecodePixelHeight to appropriate value between BitmapImage.BeginInit() and BitmapImage.BeginInit(). And don't set both unless you don't care about original aspect ratio.

Alex.Wei
  • 1,798
  • 6
  • 12
  • I understand that I don’t need so much pixels. But I want to implement image resize feature. After BitmapImage.DecorePixehHeight image quality will decrease, won’t It? And do you know why it occupies so much memory? – Roman Apr 10 '19 at 12:02
  • @Roman As you said, you load a 100MB jpeg, and it will be decode to RGBA image in memory. So, what else can you expect? – Alex.Wei Apr 10 '19 at 12:52
  • That means I didn't understand what is going on. So, It turns out when I load 100 MB jpeg, it will be decode to RGBA, which in turn larger than jpeg? Sorry, but I am not good at image formatting – Roman Apr 10 '19 at 13:20
  • @Roman Ok... Actually, after `EndInit()`, you can use `Height` * `Width` * `Format.BitsPerPixel` / 8 to calculate how many bytes that decoded image takes. Or you can also go through `BitmapImage._finalSource.DUCECompatiblePtr._gcPressure._gcPressure` to check how many memory it keep from `GC`. – Alex.Wei Apr 10 '19 at 14:45