Setting image source in only in runtime (Without define it in the XAML)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Setting image source in only in runtime (Without define it in the XAML)

Hybrid View

  1. #1
    Join Date
    May 2011
    Posts
    5

    Setting image source in only in runtime (Without define it in the XAML)

    Hi,
    I'm trying to create XAML page with several images that will only be set in the code during runtime. Here is a sample of image tag from my XAML -
    <Image Name="CloseAppWindow" Width="454" Height="256" Canvas.Top="45" Canvas.Left="24"></Image>

    As you can see I did not specify the source for it since I want to do it through code. However the resources of the images are kept in a different project and no matter what I do to send them as parameters the XAML window is drawn without them.

    Any ideas will be great,
    Shak

  2. #2
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Re: Setting image source in only in runtime (Without define it in the XAML)

    You can fetch the image from other project using reflection.
    Note that this will work only when the images are embedded in the project dll.
    Regards,
    MMH
    Rate my post if you find it usefull.

  3. #3
    Join Date
    May 2011
    Posts
    5

    Re: Setting image source in only in runtime (Without define it in the XAML)

    Hi,
    Thanks for your replay.
    Can you please explain further how to do the reflection?

  4. #4
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Re: Setting image source in only in runtime (Without define it in the XAML)

    Quote Originally Posted by shak_pg View Post
    Hi,
    Thanks for your replay.
    Can you please explain further how to do the reflection?
    Look into the attached sample POC.
    It is developed in VS2010; so if you cant opn the solution, try looking into the files using the VS version that you have.

    I hope this will help.
    Attached Files Attached Files
    Last edited by MMH; May 4th, 2011 at 06:33 AM.
    Regards,
    MMH
    Rate my post if you find it usefull.

  5. #5
    Join Date
    May 2011
    Posts
    5

    Re: Setting image source in only in runtime (Without define it in the XAML)

    Thanks for the example but I did not understand how your example can help me.
    I was talking about XAML(WPF) that will define images in the XAML but the source to that image will be decided upon runtime.
    :-(

  6. #6
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Re: Setting image source in only in runtime (Without define it in the XAML)

    Here have a look into the below code:

    Xaml contains the image tag without source.

    Code:
    <Window x:Class="FetchResource_SampleApp_WPF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Image x:Name="closeAppWindow" Width="454" Height="256"></Image>    </Grid>
    </Window>
    In the code behind, do the following:
    Code:
    System.Reflection.Assembly otherProject;
                otherProject = System.Reflection.Assembly.GetAssembly(typeof(ResourceLib.Class1));
                System.IO.Stream streamFile = otherProject.GetManifestResourceStream("ResourceLib.Images.ExpressionBlend.JPG");
                
                BitmapImage bitmap = new BitmapImage(); 
                bitmap.BeginInit(); 
                bitmap.StreamSource = streamFile; 
                bitmap.EndInit();
    
                closeAppWindow.Source = bitmap;
    Do not forget to check the attachment
    Attached Files Attached Files
    Last edited by MMH; May 5th, 2011 at 11:34 AM.
    Regards,
    MMH
    Rate my post if you find it usefull.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center