-
Jul 20th, 2021, 10:19 PM
#1
Thread Starter
New Member
How to Update and Remove the .xaml Image.Source Property using Visual Basic code
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?
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...
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.
Last edited by si_the_geek; Jul 21st, 2021 at 06:29 AM.
Reason: added Code tags
-
Jul 21st, 2021, 06:30 AM
#2
Re: How to Update and Remove the .xaml Image.Source Property using Visual Basic code
Welcome to VBForums
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
-
Jul 21st, 2021, 06:58 AM
#3
Re: How to Update and Remove the .xaml Image.Source Property using Visual Basic code
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...
Code:
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:
Code:
ImgPicture.Source = New BitmapImage(New Uri("C:\CPG\Picture\cat.jpg"))
-tg
\
-
Jul 21st, 2021, 08:18 PM
#4
Thread Starter
New Member
Re: How to Update and Remove the .xaml Image.Source Property using Visual Basic code
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#.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|