开发者

How to load a very large sourceimage in WPF image?

开发者 https://www.devze.com 2023-04-03 19:43 出处:网络
I have a very large image (600mb) 30000x30000 and want to load it into a wpf image control. I can watch this image with the Windows Photo Viewer!

I have a very large image (600mb) 30000x30000 and want to load it into a wpf image control.

I can watch this image with the Windows Photo Viewer!

I set my testapp to 64bit and used the following code.

var image = new BitmapIm开发者_StackOverflow社区age();
image.BeginInit();

// load into memory and unlock file
image.CacheOption = BitmapCacheOption.OnLoad;

image.UriSource = uri;

image.EndInit();

imagecontrol.source = image;

The test app just shows a white screen with this large image.

Smaller ones like 100mb and 7000x7000 are working.

What am I doing wrong? Sry for my bad english and thanks in advance.


64-Bit Applications.

As with 32-bit Windows operating systems, there is a 2GB limit on the size of an object you can create while running a 64-bit managed application on a 64-bit Windows operating system.


Picture can be fetched directly from the hard drive to reduce a memory usage.

You can use BitmapCacheOption

Code bellow will read the image directly from the HDD and small thumbnail will be generated without the original image cache:

public BitmapImage memoryOptimizedBitmapRead(string url,FrameworkElement imageContainer)
        {
            if (string.IsNullOrEmpty(url) || imageContainer.ActualHeight<= 0)
            {
                return null;
            }

            var bi = new BitmapImage();
            bi.BeginInit();

            bi.CacheOption = BitmapCacheOption.None;
            bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
            bi.DecodePixelHeight = (int)imageContainer.ActualHeight;
            bi.UriSource = new Uri(url, UriKind.Absolute);

            // End initialization.
            bi.EndInit();
            bi.Freeze();
            return bi;

        }

Max thumbnail size is determined by the image container size.

Method usage example:

var image= new Image();
image.Source = memoryOptimizedBitmapRead(...);


I'd divide it into 10 (3000x3000) segments and put them into 10 files.

Also check what format you're using it. It may be filling up the threshold for file size or for that particular format. Try TIF format, then try JPG, then try BMP, etc.. Also see if you can compress it with the JPG format to 40-50% and see if that changes anything.

Let me know what you find out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号