Results 1 to 4 of 4

Thread: How to Update and Remove the .xaml Image.Source Property using Visual Basic code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    3

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    \
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width