Retrieve the size of an image in Silverlight
Let me explain, for different reason it is sometimes necessary to have access to the dimensions of an image in the XAML document informed of your application.
<Image x: Name = "imPreview" Width = "200" Height = "120" Source = "image.jpg" />
XAML: <Image x: Name = "imPreview" />
Code (C #): BitmapImage bi = new BitmapImage (new Uri ("image.jpg", UriKind.Relative)); imPreview.Source = bi;
To manipulate the image at will and get the image size you will have to use a "workaround" via the event image "SizeChanged".
What gives, on page load:
BitmapImage bi = new BitmapImage (new Uri ("image.jpg", UriKind.Relative)); imPreview.Source = bi; imPreview.SizeChanged + = new SizeChangedEventHandler (imPreview_SizeChanged); imPreview_SizeChanged void (object sender, SizeChangedEventArgs e) ( // E.PreviousSize.Width; Similarly Height // E.NewSize.Width; Similarly Height )
Ex1: <Image x: Name = "imPreview" Stretch = "None" />Note: If your parent is a container <canvas> you will not be able to retrieve the event SizeChanged.
While the image does not download, we can not get the size assumed. The idea is to listen to the SizeChanged image to intercept the load of the image in the XAML document. There are other possibilities such as using an interval but it is much less clean
Re: Import Font (font) in Silverlight
Here is a small novelty Silverlight 2, which I think will be important to designers: Silverlight 2 carries import Font natively. Let me explain, in Silverlight 1.0 when you need to use a specific font in your application, you had to use the object Downloader to download the font on the client. Small recall with Silverlight 1.0.
1. We download the form via the Downloader object:
function onLoad (sender, EventArgs)
(2. In the event of the object onCompleted Downloader can use the method setFontSource recovering the sender ie the TTF file:
onCompleted function (sender, EventArgs)Now with Silverlight 2, it is possible to replace these 10 lines of code with "1 line" and this directly in your XAML file:
<TextBlock Text = "TextBlock" FontFamily = "# angelica.ttf Angelica" FontSize = "72" />
|Tags: c code, font, image, import, microsoft, silverlight, xaml|
|Thread Tools||Search this Thread|
|Similar Threads for: "Retrieve the size of an image in Silverlight"|
|Thread||Thread Starter||Forum||Replies||Last Post|
|Convert 2D image into 3D image using Silverlight 4||Thenral||Windows Software||5||18-04-2011 11:45 AM|
|Unable to download custom image in SilverLight||fAROK||Technology & Internet||5||18-04-2011 11:41 AM|
|How to add image inside the canvas using SilverLight||Hunter-Man||Windows Software||3||17-04-2011 07:51 PM|
|How to refresh an image control in Silverlight||NAKKIRAN||Windows Software||4||13-12-2010 07:11 PM|
|Monitor image size error||Arval||Monitor & Video Cards||3||16-03-2009 03:29 PM|