简体   繁体   中英

Why doesn't my BitmapFrame download every time?

I'm trying to create a function that allows my app to assign an image.Source to an image at an internet URL. I also need to download a BitmapFrame to get the image's size details before I download the image itself.

The problem is that the bFrame.DownloadCompleted part of my code doesn't trigger with certain images, so I need to know why the download event isn't triggering all the time and how to get around it so that it works with every image.

Here is my code:

private static void GetWebImage(Image image, string path)
    {
        // Adjust image size to the source image's dimensions.
        var bFrame = BitmapFrame.Create(new Uri(path), BitmapCreateOptions.None, BitmapCacheOption.None);
        if (bFrame.IsDownloading)
        {
            bFrame.DownloadCompleted += (e, arg) =>
            {
                image.Width = bFrame.PixelWidth;
                image.Height = bFrame.PixelHeight;

                // Check whether the bitmap is in portrait orientation.
                bool portrait = false;
                if (image.Height > image.Width) { portrait = true; }

                // Resize large images to the correct proportions.
                double shrinkRatio = 0;
                if (portrait && image.Height > 300)
                {
                    shrinkRatio = 300 / image.Height;
                    image.Height = image.Height * shrinkRatio;
                    image.Width = image.Width * shrinkRatio;
                }
                else if (!portrait && image.Width > 400)
                {
                    shrinkRatio = 400 / image.Width;
                    image.Height = image.Height * shrinkRatio;
                    image.Width = image.Width * shrinkRatio;
                }

                // Round the edges of the image.
                image.Clip = new RectangleGeometry(new Rect(0, 0, image.Width, image.Height), 6, 6);

                // Download the image from the URL.
                BitmapImage bImage = new BitmapImage();
                bImage.BeginInit();
                bImage.UriSource = new Uri(path, UriKind.Absolute);
                bImage.EndInit();
                image.Source = bImage;
            };
        }


    }

Not sure why exactly BitmapFrame behaves like that.

However, you could directly assign a BitmapImage to image.Source to enforce that the image is downloaded immediately.

private static void GetWebImage(Image image, string path)
{
    var bitmap = new BitmapImage(new Uri(path));

    if (bitmap.IsDownloading)
    {
        bitmap.DownloadCompleted += (s, e) => AdjustSize(image, bitmap);
    }
    else
    {
        AdjustSize(image, bitmap);
    }

    image.Source = bitmap;
}

private static void AdjustSize(Image image, BitmapSource bitmap)
{
    image.Width = bitmap.PixelWidth;
    image.Height = bitmap.PixelHeight;

    if (image.Height > image.Width)
    {
        if (image.Height > 300)
        {
            image.Width *= 300 / image.Height;
            image.Height = 300;
        }
    }
    else if (image.Width > 400)
    {
        image.Height *= 400 / image.Width;
        image.Width = 400;
    }

    image.Clip = new RectangleGeometry(
        new Rect(0, 0, image.Width, image.Height), 6, 6);
}

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