代码之家  ›  专栏  ›  技术社区  ›  ChrisUK

为什么我的位图帧每次都不下载?

  •  0
  • ChrisUK  · 技术社区  · 6 年前

    我正在尝试创建一个允许我的应用程序分配 image.Source BitmapFrame 在下载图像之前获取图像的大小细节。

    问题是 bFrame.DownloadCompleted 我的部分代码不会触发某些图像,所以我需要知道为什么下载事件不会一直触发,以及如何绕过它,以便它可以处理每个图像。

    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;
                };
            }
    
    
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Clemens    6 年前

    不知道BitmapFrame为什么会这样。

    image.Source 强制立即下载图像。

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