Wednesday, September 26, 2012

Image Loading Problem

Image Loading Problem

I have created a simple child window as below

<controls:ChildWindow x:Class="LeanFleet.SL.ChildControls.ChildWindow1"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"             Width="400" Height="300"              Title="ChildWindow1">      <Grid x:Name="LayoutRoot" Margin="2">          <Image Source="/Resources/refresh.png" Width="24" Height="24" />          <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />          <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />        </Grid>  </controls:ChildWindow>

 

When I open the chilld window, the image was displayed. Next I have created a class to create same child window as below

 

public partial class childwnd : ChildWindow      {          public childwnd()              : base()          {              Grid grd = new Grid() { Height = 200, Width = 200 };              Image img = new Image() { Source = new BitmapImage(new Uri("/Resources/refresh.png", UriKind.Relative)), Width = 24, Height = 24 };              grd.Children.Add(img);              this.Content = grd;          }        }

 

But , now if I open this, the image not displayed. Did I miss annything? 

 


 

Answers & Comments...

Answer: 1

I'm guessing the buttons are on top of the image in the first example and that would be undesirable.  Maybe you are wanting the image INSIDE one of the buttons?

Not sure why it wouldn't work in code.



Answer: 2

You can remove the buttons and the issue will be still there. This is a scale down test case of my orginal 2 problems which are

1. If I create a menuitem dynamically (from code behind) the menuitem icons are not displayed.

2. If I crate a combo box with images on a child window (from code behind), only the images loaded in the parent page are displayed in the combo and all others are blank.

After posting this issue here, I did some more investigations and found that, in the code behind if I specify

CreateOptions = BitmapCreateOptions.None

as Image property, the image is displayed. Though, I do not understand the logic but it solved the Issue#1

But the Issue#2 is still not resolved even if I add the above property.



Answer: 3

Where are the images you are trying to use?  Are they in the server folder?  Or in the client xap?  Or in a resource?  I'm assuming not in a resource since you are giving a path.  But if they are in a resource, how do the properties look?



Answer: 4

Images are kept in server in a folder (named 'Resources') under clientbin folder.



Answer: 5

Seems like it should work.  I wonder if the problem is that it is in a ChildWindow?  I've seen ChildWindow break stuff (like drag and drop, for instance).  Sorry, no other suggestions I can think of.



Answer: 6

I'm not sure ... but worth investigating I think

In the code version you are running on the client and so you will need to have a folder called Resources with refresh.png in it in your Silerlight project (eg not in the web hosting project).

The xaml version is referencing a folder withing the hosting project.



Answer: 7

I have solved the problem.

Two things discovered.

1. This problem happens from VS2010 server not from IIS

2. If I place the images directly in the clientbin folder (instead of any other folder in clientbin), everything works fine. I don't even need CreateOptions="none"

Thanks





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog