# .NET and More > WPF, WCF, WF >  How to Update and Remove the .xaml Image.Source Property using Visual Basic code

## VMcElwee

Microsoft Visual Studio Community 2019 Version 16.8.4
VB.NET WPF
How to Update and Remove the .xaml Image.Source Property using
Visual Basic code?


```
                            <Image x:Name="ImgPicture"
                                   Grid.Column="0"
                                   Margin="30,47,0,0"
                                   Source="C:\CPG\Picture\cat.jpg"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Top"
                                   Height="43"
                                   Width="50"
                                   Visibility="Visible"
                                   RenderTransformOrigin="0.78,1.38">
                            </Image>
```

The Image Source Property must be included in the above .xaml
I can not leave it blank or remove it without a 
"An attribute name is missing" error.  I could change the
Visibility="Hidden" to accomplish "Removal."
Picture Box is not available, since I am using WPF .xaml

Behind VB code...


```
  ImgPicture.Source = "C:\CPG\Picture\cat.jpg"
gives a "Value of type 'String' cannot be converted to
'ImageSource' message.  It seems it has be a BitmapImage...
            Dim myImage As New Image
            Dim myBitmap As New BitmapImage

            myBitmap.BeginInit()
            myBitmap.UriSource = New Uri("C:\CPG\Picture\cat.jpg")
            myBitmap.EndInit()

            myImage.Source = myBitmap
         *  ImgPicture.Source = myImage.Source
```

* The above VB code gives a "Value of type 'String' cannot be converted
    to 'ImageSource' message.

If you have any suggestions on how I can Update/Remove the 
ImgPicture.Source Property using behind VB code , please let me know.

Thanks in advance.

----------


## si_the_geek

Welcome to VBForums  :wave: 

_Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'WPF, WCF, WF' forum_

----------


## techgnome

The problem isn't ImgPicture but rather myImage... myImage is sending a String for the .Source property ... while ImgPicture is expecting an ImageSource ... in the XAML for an Image, the Source property is a string, so that's fine. What isn't fine is the VB Code. 
According to the documentation, you'll see that the .Source property needs to be an ImageSource ... which can be gotten from a BitmapSource. 
To create that, you simply pass in a URI in the constructor... 


```
            Dim myBitmap As New BitmapImage(New Uri("C:\CPG\Picture\cat.jpg"))
            ImgPicture.Source = myBitmap
```

You can probably forgo all that and just assign the BitmapImage directly:


```
ImgPicture.Source = New BitmapImage(New Uri("C:\CPG\Picture\cat.jpg"))
```

-tg
\

----------


## VMcElwee

Appreciate your rapid response to my dilemma.

I tryied both VB Code examples without success.
No compile or runtime errors.  Since the XAML Image 
Source Property is required, it displays that image.

I was able to control the Image Visibility using VB Code...
      ImgPicture.Visibility = Visibility.Visible
      ImgPicture.Visibility = Visibility.Hidden

Could you revisit this issue and provide me with test
results?

Your reference links to Microsoft Documentation
uses C# examples, not VB Code examples.  VB Code
does things strangely, as shown above with Visibility 
and Brush...

       TxtBox1.Background = Brushes.Yellow

I think Microsoft support for Visual Basic is fading
and focusing more on C#.

----------

