locked
How to set source of image as user control using C# Windows 8

    Question

  • I want to set the source of image which is user control using C#, how can I do that? Here is my user control.

    <UserControl
        x:Class="App4.MyImage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App4"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
    
    
        <Image x:Name="MyImageControl" Width="100" Height="100"  
               HorizontalAlignment="Left" VerticalAlignment="Top" 
               ManipulationDelta="MyImage_ManipulationDelta" 
               ManipulationMode="TranslateY,TranslateX,Rotate"
               Tapped="MyImage_Tapped" RightTapped="MyImage_RightTapped">
            <Image.RenderTransform>
                <CompositeTransform></CompositeTransform>
            </Image.RenderTransform>
        </Image>
    
    </UserControl>

    I tried this but this does not work

    string url = "ms-appx:///Assets/21.png";
    MyImage image = new MyImage();
    image.SetValue(Image.SourceProperty, new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));

    Wednesday, June 4, 2014 10:06 PM

Answers

  • If you find it difficult to use custom dependency properties, the you can make some slight changes as below:

    1. Provide public field modifier to the image control :

    <Image x:FieldModifier="public" x:Name="MyImageControl" Width="100" Height="100"  
               HorizontalAlignment="Left" VerticalAlignment="Top" 
               ManipulationDelta="MyImage_ManipulationDelta" 
               ManipulationMode="TranslateY,TranslateX,Rotate"
               Tapped="MyImage_Tapped" RightTapped="MyImage_RightTapped">
            <Image.RenderTransform>
                <CompositeTransform></CompositeTransform>
            </Image.RenderTransform>
        </Image>

    2. Now you can access the image control, so access the image and apply source like this:

    string url = "ms-appx:///Assets/21.png";
    MyImage image = new MyImage();
    image.MyImageControl.SetValue(Image.SourceProperty, new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 7:58 AM

All replies

  • Hi Ghazanfar,

    I would suggest you to register a dependency property for your user control, the image inside your user control can binding its Source with this property. For more information, ref here: Custom dependency properties. This way is much more easier than directly access the UserControl and set the image source.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, June 5, 2014 6:03 AM
    Moderator
  • If you find it difficult to use custom dependency properties, the you can make some slight changes as below:

    1. Provide public field modifier to the image control :

    <Image x:FieldModifier="public" x:Name="MyImageControl" Width="100" Height="100"  
               HorizontalAlignment="Left" VerticalAlignment="Top" 
               ManipulationDelta="MyImage_ManipulationDelta" 
               ManipulationMode="TranslateY,TranslateX,Rotate"
               Tapped="MyImage_Tapped" RightTapped="MyImage_RightTapped">
            <Image.RenderTransform>
                <CompositeTransform></CompositeTransform>
            </Image.RenderTransform>
        </Image>

    2. Now you can access the image control, so access the image and apply source like this:

    string url = "ms-appx:///Assets/21.png";
    MyImage image = new MyImage();
    image.MyImageControl.SetValue(Image.SourceProperty, new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute)));

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 5, 2014 7:58 AM
  • Try 

    MyImageControl.Source = new BitmapImage(new Uri("ms-appx:///Assets/21.png"));


    • Edited by St4mos Friday, June 6, 2014 9:20 PM
    Friday, June 6, 2014 9:18 PM
  • No such property
    Tuesday, June 10, 2014 6:09 PM