Reputation: 137
I am drag and drop a heavy image (95729 kb) in my RichTextBox. But memory usage is so incomprehensible: 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?
Upvotes: 4
Views: 356
Reputation: 1883
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.
Upvotes: 1
Reputation: 151730
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?
Upvotes: 4