none
How to Add Images Dynamically using VB.net to the Window

    Question

  • Hi all :D,
                 How can I add an image to the window at a specific point without using a control to contain it (using VB.Net)?

    Thanks

    Q

    Q
    Tuesday, October 14, 2008 1:39 PM

Answers

  • Hi QuddusAli,

    I'm more of a C# guy so I actually had to test out the C# implementation before converting it to VB.NET :)

    What I had in my Window was only two elements, a button and a Canvas with x:Name = Canvas1, and I also added a an image into my project, which is "Sunset.jpg". I placed the code to add an image to the Window when a button is clicked. The code is as below:

        Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim src As Uri = New Uri("Sunset.jpg", UriKind.Relative)
            Dim bitmap As BitmapImage = New BitmapImage(src)
            Dim img As Image = New Image()
            img.Source = bitmap
            Canvas1.Children.Add(img)
        End Sub


    The great thing about WPF & Silverlight is that everything that can be done in XAML can be done also in code behind, its just based on your preference and the scenario you are facing, in this situation, dynamically generating UIElements might be the best way to go.

    If this answered your question please mark it as answered, hopefully this will also help others facing similar issues :)


    -
    • Proposed as answer by Kok ChiannMVP Monday, October 20, 2008 9:17 AM
    • Marked as answer by QuddusAli Sunday, October 26, 2008 9:48 PM
    Monday, October 20, 2008 9:17 AM

All replies

  • Hi QuddusAli,

    I'm more of a C# guy so I actually had to test out the C# implementation before converting it to VB.NET :)

    What I had in my Window was only two elements, a button and a Canvas with x:Name = Canvas1, and I also added a an image into my project, which is "Sunset.jpg". I placed the code to add an image to the Window when a button is clicked. The code is as below:

        Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim src As Uri = New Uri("Sunset.jpg", UriKind.Relative)
            Dim bitmap As BitmapImage = New BitmapImage(src)
            Dim img As Image = New Image()
            img.Source = bitmap
            Canvas1.Children.Add(img)
        End Sub


    The great thing about WPF & Silverlight is that everything that can be done in XAML can be done also in code behind, its just based on your preference and the scenario you are facing, in this situation, dynamically generating UIElements might be the best way to go.

    If this answered your question please mark it as answered, hopefully this will also help others facing similar issues :)


    -
    • Proposed as answer by Kok ChiannMVP Monday, October 20, 2008 9:17 AM
    • Marked as answer by QuddusAli Sunday, October 26, 2008 9:48 PM
    Monday, October 20, 2008 9:17 AM
  • thanks
    Sunday, October 26, 2008 11:06 AM