10 Replies - 13493 Views - Last Post: 26 January 2012 - 09:56 AM Rate Topic: -----

#1 Jersey928  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 22-March 11

Setting Image Source via Code VB.Net

Posted 25 January 2012 - 09:53 AM

I have searched the forum and have seen some solutions but they are in C#. What i am trying to do is set an image source via code. the tricky part is that i want to make it so that i only have to pass i the image file. I have that all down i just dont know the syntax for setting the string to the source. I do know that i hsve to use a URI i just dont understand them.

the code that i have tried is

 Dim ImageIcon As New BitmapImage
        ImageIcon.BeginInit()
        Dim ImageSource As String
        ImageSource = "/ProjectName;component./Resources/" & Source
        ImageIcon.UriSource = New Uri(ImageSource, UriKind.Relative)
        ImageBlock.Source = ImageIcon



Is This A Good Question/Topic? 0
  • +

Replies To: Setting Image Source via Code VB.Net

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:00 AM

Here is how I normally do it. You have already added the image to the project. Click on the image file in the Solution Explorer, then hit F4(to show the Properties Window). Set the Build Action to Content. From there, you can access the image just from the relative path.

For example, if your image named "team.png" is in an "images" folder in the solution, you would access it like..

"/images/team.png"



No need for the "projectName;component:::" blah blah crap.
Was This Post Helpful? 1
  • +
  • -

#3 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:02 AM

here is what i have used and it works for me

                        Dim bit As New BitmapImage()
                        bit.BeginInit()
                        bit.CacheOption = BitmapCacheOption.onload
                        bit.UriSource = New Uri(ImageLocationString)
                        MyImage.Source = bit
                        bit.EndInit()



EDIT:

And yes, i agree with eclipse. also if your image is in a folder back a level, you would need something like this

../Resources/MyImage.png


This post has been edited by bflosabre91: 25 January 2012 - 10:04 AM

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:03 AM

"/ProjectName;component./Resources/"

You do realize that you can't have a semicolon in a path, right? It's not a valid path character... Along with * ? etc.

You really should start your trial and error with valid paths and go from there.


Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.
Was This Post Helpful? 0
  • +
  • -

#5 Jersey928  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 22-March 11

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:04 AM

Ok so tried this after changing the
Build Action


    ImageIcon.UriSource = New Uri("/Resources/" & Source, UriKind.Relative)
        ImageBlock.Source = ImageIcon




And the image still does not show up
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:13 AM

Can you put a breakpoint on line 2 of that last snippet and show us a screen shot of the actual value of ImageIcon.UriSource?
Was This Post Helpful? 0
  • +
  • -

#7 Jersey928  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 22-March 11

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 10:36 AM

Ok i got it to work its not pretty but what i had to do was add a Resources folder in the same folder that the usercontrol is located. then put the image inside that folder. then i used the code that bflosabre91 posted and it worked.

Here is what the code looks like
            Dim ImageIcon As New BitmapImage
            ImageIcon.BeginInit()
            ImageIcon.CacheOption = BitmapCacheOption.onload
            ImageIcon.UriSource = New Uri("./Resources/" & Source, UriKind.Relative)
            ImageBlock.Source = ImageIcon
            ImageIcon.EndInit()




Thanks for your helps guys
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Setting Image Source via Code VB.Net

Posted 25 January 2012 - 01:03 PM

View PosttlhIn`toq, on 25 January 2012 - 01:03 PM, said:

"/ProjectName;component./Resources/"

You do realize that you can't have a semicolon in a path, right? It's not a valid path character... Along with * ? etc.

You really should start your trial and error with valid paths and go from there.


It's not an actual path. It's a pack URI. Once you get a little more familiar with WPF, you will see how you are allowed to include resources from a reference.
Was This Post Helpful? 1
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Setting Image Source via Code VB.Net

Posted 26 January 2012 - 08:46 AM

I don't understand why you needed to add a folder if all you wanted to do was display an image...

I tried it to make sure it wasn't harder than I remember.

Here's my XAML:
<StackPanel>
    <Image x:Name="myImageControl" Height="150"/>
    <Button x:Name="showPic" Click="showPic_Click" Content="Show Picture"/>
</StackPanel>


Here's my button click code:
Private Sub showPic_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim image As New BitmapImage
        Dim imagePath As String = "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"
        image.BeginInit()
        image.UriSource = New Uri(imagePath, UriKind.Absolute)
        image.EndInit()

        myImageControl.Source = image
End Sub


I mean I could have made the button click code a little more user friendly using the FileOpenDialog...Like this:
 Private Sub showPic_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim image As New BitmapImage
        Dim imagePath As String = "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"

        Dim dlg As New Microsoft.Win32.OpenFileDialog()
        dlg.FileName = "Photo"
        dlg.DefaultExt = ".jpg"
        dlg.Filter = "All images|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.ico;*.png;*.tif;*.tiff;*.hpd;*.jxr;*.wdp|" + _
            "JPEG image|*.jpg;*.jpeg;*.jpe|Windows BMP image|*.bmp|GIF image|*.gif|Microsoft Windows icon|*.ico|" + _
            "PNG image|*.png|TIFF image|*.tif;*.tiff|JPEG XR|*.hpd;*.jxr;*.wdp"
        dlg.FilterIndex = 2
        dlg.Multiselect = False
        If dlg.ShowDialog() Then
            For Each filename As String In dlg.FileNames
                imagePath = filename
            Next
        End If

        image.BeginInit()
        image.UriSource = New Uri(imagePath, UriKind.Absolute)
        image.EndInit()

        myImageControl.Source = image
    End Sub


It's pretty straightforward using a URI though...

Even if I wanted to directly set the source I'd just open a file stream (using the path) to read in the bytes and then use a MemoryStream to set the source of the image:
    Private Sub showPic_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim image As New BitmapImage
        Dim imagePath As String = "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"

        Dim dlg As New Microsoft.Win32.OpenFileDialog()
        dlg.FileName = "Photo"
        dlg.DefaultExt = ".jpg"
        dlg.Filter = "All images|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.ico;*.png;*.tif;*.tiff;*.hpd;*.jxr;*.wdp|" + _
            "JPEG image|*.jpg;*.jpeg;*.jpe|Windows BMP image|*.bmp|GIF image|*.gif|Microsoft Windows icon|*.ico|" + _
            "PNG image|*.png|TIFF image|*.tif;*.tiff|JPEG XR|*.hpd;*.jxr;*.wdp"
        dlg.FilterIndex = 2
        dlg.Multiselect = False

        If dlg.ShowDialog() Then
            For Each filename As String In dlg.FileNames
                Using photoStream As System.IO.FileStream = New System.IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

                    Dim photoArray(photoStream.Length - 1) As Byte
                    photoStream.Read(photoArray, 0, photoStream.Length)

                    Using bitmapStream As System.IO.MemoryStream = New System.IO.MemoryStream(photoArray)
                        image.BeginInit()
                        image.StreamSource = bitmapStream
                        image.CacheOption = BitmapCacheOption.onload
                        image.EndInit()
                    End Using
                End Using

            Next
        End If

       

        myImageControl.Source = image
    End Sub


-Frinny

This post has been edited by Frinavale: 26 January 2012 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Setting Image Source via Code VB.Net

Posted 26 January 2012 - 09:07 AM

View PostFrinavale, on 26 January 2012 - 09:46 AM, said:

I don't understand why you needed to add a folder if all you wanted to do was display an image...

I tried it to make sure it wasn't harder than I remember.


Unless I'm mistaken its two different needs. The OP is trying to load an image they are providing with the application; part of the resources that ship with the program. Probably a background image, face of a playing card... etc.

Your example is how to display an image from the HDD after giving the user a chance to browse to it. A photo browse/display type application.

While loading is basically the same, the OP was really just having trouble figuring out how to structure his folders and how to dynamically reference the placement of the resources he was including.

This post has been edited by tlhIn`toq: 26 January 2012 - 09:08 AM

Was This Post Helpful? 2
  • +
  • -

#11 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Setting Image Source via Code VB.Net

Posted 26 January 2012 - 09:56 AM

Ahh...where is the resource and what type is it?
Is it in a resource file?

If it's in a folder within the application then it then answer is already in the thread:

  Private Sub showPic_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim image As New BitmapImage
        Dim fileName As String = "myImage.png" 'could be passed in as a parameter'
        Dim imagePath As String = "/myNamespace;component/ImageFolderInApp/" + fileName
        image.BeginInit()
        image.UriSource = New Uri(imagePath, UriKind.RelativeOrAbsolute)
        image.EndInit()
        myImageControl.Source = image
  End Sub


If it has been added to the Resource file that is part of the application (embedded into the resource file for the application) then you can get to it like so:
    Private Sub showPic_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim image As New BitmapImage

        'If you do not have to pass in the "key" of the resource by name, '
        'You could access it directly using My.Resources.Key '
        'Like the following: '
        ' Dim pic As System.Drawing.Bitmap = My.Resources.ImageKey'

        Dim pictureReourceKey As String = "ImageKey"
        Dim rm As New System.Resources.ResourceManager("MyNamespace.Resources", Reflection.Assembly.GetExecutingAssembly)
        Dim pic As System.Drawing.Bitmap = rm.GetObject(pictureReourceKey)

        Using bitmapStream As System.IO.MemoryStream = New System.IO.MemoryStream()
            pic.Save(bitmapStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Using imageSourceStream As New MemoryStream(bitmapStream.ToArray)
                image.BeginInit()
                image.StreamSource = imageSourceStream
                image.CacheOption = BitmapCacheOption.onload
                image.EndInit()
            End Using
        End Using

        myImageControl.Source = image
    End Sub

Apparently there are a lot of ways to load an image :)


-Frinny

This post has been edited by Frinavale: 26 January 2012 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1