Skip to content

Instantly share code, notes, and snippets.

@meklarian
Created May 16, 2010 22:44
Show Gist options
  • Save meklarian/403231 to your computer and use it in GitHub Desktop.
Save meklarian/403231 to your computer and use it in GitHub Desktop.
source code demonstrating correct image size lookup for an asynchronous image in WPF
Source code for a project that demonstrates how to get the correct dimensions for an asynchronous image download in WPF.
The key is to wait for the download has completed, before reading the size.
Also note, that WPF does some scaling and DPI checks, so you may have to recalculate pixel size depending on your output medium.
<Window x:Class="ImageLookup.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Grid.Row>0</Grid.Row>
<Label>URL:</Label>
<TextBox Name="txtUrl" Width="400" />
<Button Name="btnLoad" Click="btnLoad_Click">Load</Button>
</StackPanel>
<Grid Name="gridView">
<Grid.Row>1</Grid.Row>
</Grid>
<Label Padding="0" Margin="0 2 0 0" Name="lblInfo">
<Grid.Row>2</Grid.Row>
</Label>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace ImageLookup
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Image img = null;
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
this.gridView.Children.Clear();
string mapURL = this.txtUrl.Text;
if (string.IsNullOrEmpty(mapURL)) { return; }
this.Visibility = Visibility.Visible;
img = new Image();
this.gridView.Children.Add(img);
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(string.Format(mapURL), UriKind.Absolute);
src.CacheOption = BitmapCacheOption.Default;
src.EndInit();
// NOTE: At this point, the image will be downloading, and PixelHeight/PixelWidth reports the wrong size.
Trace.WriteLine(string.Format("Init: {0} x {1} image, {2}", src.PixelWidth, src.PixelHeight, src.IsDownloading ? "Downloading" : "Ready"));
src.DownloadCompleted += new EventHandler(
(object xsender, EventArgs xe) =>
{
// NOTE: After the download has completed, PixelHeight/PixelWidth will report the correct size.
BitmapImage readySrc = (BitmapImage)xsender;
img.Height = readySrc.PixelHeight;
img.Width = readySrc.PixelWidth;
img.Stretch = Stretch.UniformToFill;
img.Source = readySrc;
this.lblInfo.Content = string.Format("{0} x {1} image", readySrc.PixelWidth, readySrc.PixelHeight);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment