Silverlight: Images

Posted Fri, Nov 13 2009 17:09 by Deborah Kurata

I just needed to add a simple image to my Silverlight application to include a company logo. Three hours later …

This post provides information on the image formats supported by Silverlight so that you don't run into the same issues and waste as much time as I did today.

[NOTE: To anyone on the Silverlight team that may read this: Why not have Visual Studio (or even Expression Blend) tell me I was making this mistake instead of letting me waste so much time!!]

First, I followed the *very* simple instructions I found everywhere:

1) Add the image to the Silverlight project.

I put mine in an Images folder in the project.

2) Set the Build Action for the image to "Resource".

3) Add the image tag to the XAML.

Easy peasy. Then why didn't it work? No image appeared. I read/tried everything I could think of but no luck. Three hours later my partner suggested I try another file… AND IT WORKED.

The problem was the image format!

  • bmp: NOT SUPPORTED IN SILVERLIGHT
    No warning, no error, it just does not display the image
  • jpg: It works, but does not support a transparent color
  • gif: It supports a transparent color, but NOT SUPPORTED IN SILVERLIGHT
  • png: It works AND it supports a transparent color

As soon as I replaced my logo.bmp with a logo.png, it worked!

[For help setting up a transparent color, see this Silverlight Tip of the Day.]

So here again are the steps for adding an image to a Silverlight application:

1) Add the image to the Silverlight project.

BE SURE that the file is either a .jpg or .png format image.

image

NOTE: The above screen shot shows the image under my Silverlight VB project, but the same is true for a C# Silverlight project.

2) Set the Build Action for the image to "Resource".

image

3) Add the image tag to the XAML.

<StackPanel Background="BlanchedAlmond">
    <Image Source="Images/Logo.png" Width="100"
           HorizontalAlignment="Left"
           Margin="2"/>
    <data:DataGrid x:Name="CustomerList"
      ItemsSource="{Binding Data, ElementName=CustomerSource}">
</data:DataGrid>           
</StackPanel>

NOTE: Since I set up the file as a resource, I set the source property of the Image tag directly to the location of the image in the Silverlight project.

And the result:

image

Enjoy!

Filed under: , , , ,

Comments

# re: Silverlight: Images

Saturday, November 14, 2009 9:09 PM by BEM

...and, in order for PNG transparency to work, the PNG must be 24 bits/pixel.  Like you, I found out the hard way.

# Silverlight: Images - Deborah Kurata

Sunday, November 15, 2009 1:15 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: Silverlight: Images

Monday, November 16, 2009 10:24 AM by Deborah Kurata

Thanks for the tip, BEM!

# Check the images first

Monday, November 16, 2009 11:19 AM by Ted

- Assume that Microsoft tools do not support anything other than the most plain image format (open it in paint and also preview it via file explorer just to check).  MS largely lags behind in supporting image, audio and video formats.

- Use a decent image editing program (photoshop, gimp, etc.) so that your images match the image format standard

- Use more widely supported sub-format like 4:2:2 in Jpeg instead of 4:1:1

- Consider optimizing the image using optipng or jpegtrans

- Assume that Silverlight/MS apps will very poorly resize or dither your image.

# re: Silverlight: Images

Friday, November 20, 2009 12:27 AM by Deborah Kurata

Thanks for the tips, Ted.

# re: Silverlight: Images

Monday, November 23, 2009 12:33 AM by Ganesh Ranganathan

Hi Deborah,

We can also bind an image tag's Image Source proprety to and ImageSource tag in the codebehind. That works too :)

# ico-files in WPF don't work, to :-(

Thursday, November 26, 2009 12:07 PM by Volker

Hi,

let me add another curiosity (and example of sleazy app-development) with images, but this time WPF:

Struggling around with XAML and WPF one'll find out that using Blend and VS side by side will be highly recommended. OK that far. Now, let you (or even worse your stylish designer) open your .net-project in Blend. First you arange some window-properties. Among others the Icon-Prop. Blend gives you the file-open dialog and let you choose (as you did that thousends of times with winforms before) an ico-file. And now you hit F5 to run the project, but the only thing you'll get is the well known "app encountered a problem; report this to MS or not". To me as a winforms developer the near to most feared and hated message. But no further explanation. Hmmm.

So let's try it in VS. There you'll get an error, to, but this time a FileFormatException, giving you the hint, that you should probably try png instead of ico and e`voila, it works!

1) Why is the ico-format supported by the file-open dialog, when it's usage creates an error? Bad, because a good app should let the user make mistakes...

2) Why not giving a qualified error message?

Both aspects make THIS implementation really is lousy and not appropriate to designers (you know, the guys wearing stylish glasses and love to use their fruity-comps and hate to use that boring business stuff instead ;-)).

Seems as if MS has to do much more homework to launch new technologies in a way that doesn' make it's critics laugh...

Greetings from rainy & stormy germany

Volker

# Silverlight: Wish List

Friday, December 11, 2009 10:30 AM by Deborah's Developer MindScape

Yes, it is that time of the year when we put together our holiday wish list and hope Santa has us on

Leave a Comment

(required) 
(required) 
(optional)
(required)