Wiki


Wiki Table of Contents

Page Details

Published by:
2 people found this article useful.

100% of people found this useful
Silverlight Tip of the Day #13 - How to Get an Images Dimensions in Silverlight.

Filed under: [Edit Tags]

Vì Silverlight dựa trên mô hình bất đồng bộ, nên việc lấy kích thước thật của ảnh sau khi load lên web là không thể. Tuy nhiên, bạn có thể giám sát quá trình download của nó và  kích thước thu được của hình ảnh mỗi khi tiến trình đạt 100%.

Để làm được điều đó, ta có vị dụ minh họa sau. Và những chú ý quan trọng:

  • BitmapImage yêu cầu phải có  namespace System.Windows.Media.Imaging;
  • ActualWidth và  ActualHeight  phải được tính giá trị. Sự tính toán được thực hiện phía sau một layout pass.  Vì vậy, hình ảnh phải được thêm vào  trong 1 tree để kích thước thật của nó được tính toán. Tôi gọi nó là GameCanvas.Children.Add(grass);
  • Hình ảnh mà bạn sẽ thêm vào  canvas đầu tiên phải được add vào ứng dụng  Silverlight project  trong Visual Studio. Hãy chắc là đường dẫn đến hình ảnh đó phải đúng.
  •  Dispatcher.BeginInvoke(delegate() { … } ); là yêu cầu phải có trước khi bạn đặt  Width/Height  hoặc chúng có thể là 0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
 
namespace SilverlightApplication12
{
    
    public partial class Page : UserControl
    {
        private Image grass;
 
        public Page()
        {
            InitializeComponent();
            LoadImage("grass.png");
        }
 
        private void LoadImage(string path)
        {
            Uri uri = new Uri(path, UriKind.Relative);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.UriSource = uri;
            bitmapImage.DownloadProgress += 
                new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
            
            grass = new Image();
            grass.Source = bitmapImage;
            GameCanvas.Children.Add(grass);
        }
 
        void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            if (e.Progress == 100)
            {
                Dispatcher.BeginInvoke(delegate()
                {
                   double height = grass.ActualHeight;
                   double width = grass.ActualWidth;
                });
            }
        }
    }
}

 

Page.xaml:

<UserControl x:Class="SilverlightApplication12.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <ContentControl x:Name="MyContent">
            <Canvas x:Name="GameCanvas" ></Canvas>
        </ContentControl>                              
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 

Recent Comments

By: colteiv Posted on 01-14-2011 11:53 AM

Đúng cái mình cần tìm. Cảm ơn nhiều.