Go Back   TechArena Community > Software > Tips & Tweaks
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Retrieve the size of an image in Silverlight

Tips & Tweaks


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-08-2010
Member
 
Join Date: Dec 2008
Posts: 128
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.
XAML:

Code:
<Image x: Name = "imPreview" Width = "200" Height = "120" Source = "image.jpg" />
Code (C #):

Code:
imPreview.GetValue (Image.WidthProperty);
But what happens when I do not wish to impose a size and a source for my image in the XAML? Stupidly ... I do not describe the properties Width and Height in my XAML document and I will then load the image source via code.

Code:
XAML: 

<Image x: Name = "imPreview" />
Code:
Code (C #): 

BitmapImage bi = new BitmapImage (new Uri ("image.jpg", UriKind.Relative)); 
imPreview.Source = bi;
And in this case the C # code (imPreview.GetValue (Image.WidthProperty)) returns NaN and it is the same for the value ActualWidth ... Thus we find ourselves stuck.

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:

Code:
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 
)
So you can find the size of your image using NewSize. By altering the stretch of the image you get well real size of the image in your XAML page.
Ex1: <Image x: Name = "imPreview" Stretch = "None" />
Returns the physical size of the image since it is not distorted.
Ex2: <Image x: Name = "imPreview" Stretch = "Fill" />
Returns the size of the image stretch to the parent element.
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

Reply With Quote
  #2  
Old 07-08-2010
Member
 
Join Date: Dec 2008
Posts: 128
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)
(
var plugin = sender.getHost ();
var downloader = plugin.createObject ("downloader");
downloader.addEventListener ("Completed", onCompleted);
downloader. open ("GET", "angelica.ttf");
downloader.send ();
)
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)
(
var myTextBlock sender.findName = ("myTextBlock");
myTextBlock.setFontSource (sender);
myTextBlock.fontFamily = "Angelica";
myTextBlock.text = "TextBlock";
)
Now with Silverlight 2, it is possible to replace these 10 lines of code with "1 line" and this directly in your XAML file:
HTML Code:
<TextBlock Text = "TextBlock" FontFamily = "# angelica.ttf Angelica" FontSize = "72" />
Of course, with either Blend or Visual Studio do not forget to include the policy in question in your project.
Reply With Quote
Reply

  TechArena Community > Software > Tips & Tweaks
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 10:43 AM.