Silverlight:获取图像(来自OpenFileDialog)的宽度/高度

时间:2023-01-08 21:20:32

I've been using the BitmapImage.DownloadProgress event to wait until the image is loaded when creating a bitmap from a URI. That was I can use an event handler to check the Image.ActualWidth/ActualHeight after the download is complete. This works fine.

我一直在使用BitmapImage.DownloadProgress事件等到从URI创建位图时加载图像。那就是我可以使用事件处理程序在下载完成后检查Image.ActualWidth / ActualHeight。这很好用。

But when I am loading an image that the user has selected from their computer, the DownloadProgress is useless.

但是当我加载用户从他们的计算机中选择的图像时,DownloadProgress是没用的。

This works for URI bitmaps:

这适用于URI位图:

private void LoadImage(Uri uri)
{
    this.ResetProgressImage();
    BitmapImage image = new BitmapImage();            
    image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(LoadImageDownloadProgress);
    image.UriSource = uri;
    this.ImageFull.Source = image;
}

private void LoadImageDownloadProgress(object sender, DownloadProgressEventArgs e)
{
    this.Dispatcher.BeginInvoke(delegate
    {
        this.ProgressImage.Value = e.Progress;
        if (e.Progress == 100)
        {
            ImageHelper.Current.Width = Math.Round(this.ImageFull.ActualWidth);
            ImageHelper.Current.Height = Math.Round(this.ImageFull.ActualHeight);
        }
    });
}

But this doesn't work when getting a BitmapImage from a stream:

但是从流中获取BitmapImage时这不起作用:

private void LoadImage(Stream stream)
{
    this.ResetProgressImage();
    BitmapImage image = new BitmapImage();            
    image.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(LoadImageDownloadProgress);            
    image.SetSource(stream);
    this.ImageFull.Source = image;
}

Does anyone know an alternative to DownloadProgress, when using SetSource(stream)?

使用SetSource(流)时,有没有人知道DownloadProgress的替代方案?

1 个解决方案

#1


There is no event in BitmapImage to monitor the loading progress using a stream.

BitmapImage中没有事件使用流监视加载进度。

However, if you just need the image dimensions, you can add the image to the hierarchy and wait for the "Loaded" or "LayoutUpdated" event.

但是,如果只需要图像尺寸,则可以将图像添加到层次结构中并等待“已加载”或“布局更新”事件。

#1


There is no event in BitmapImage to monitor the loading progress using a stream.

BitmapImage中没有事件使用流监视加载进度。

However, if you just need the image dimensions, you can add the image to the hierarchy and wait for the "Loaded" or "LayoutUpdated" event.

但是,如果只需要图像尺寸,则可以将图像添加到层次结构中并等待“已加载”或“布局更新”事件。