-2

Im using WPF and VB.net and in order to use a picture (source) for an image you need the file to be in the folder of the application, so the source of stack.png would be stack.png.

But I want to use C:/APictureLocation/stack.png. I think Releative file name maybe? I honestly do not know how to ask a question without explaining. Sorry. So I can do:

image.source = "C:\APictureLocation\stack.png"

instead of

image.source = "stack.png"

Thanks for you help!

Do not vote down without giving me pointers. Doesn't help the community.

My answer!

Private Sub BrowseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
Dim dialog As New OpenFileDialog()
dialog.InitialDirectory = "C:\"

dialog.ShowDialog()

picbox.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri(dialog.FileName, UriKind.Absolute)) 
End Sub
D0LB33
  • 1
  • 3
  • That's one of the worst things you can ever do while programming.... Why on earth would you want to do that?? – walther Apr 10 '14 at 10:51
  • Redirect a picture to a direct location! Perfect question let me change it! – D0LB33 Apr 10 '14 at 10:52
  • ANd walther I need to use a browse button. And when the user clicks it I want the image to set the source to the file location. But If I stick with the regular method I would have to copy the image to the application folder and that will get messy. :) @walther – D0LB33 Apr 10 '14 at 10:54
  • Are you looking for a [FileOpenDialog](http://stackoverflow.com/q/10315188/205233)? – Filburt Apr 10 '14 at 10:56
  • Why you didn't explain it like this? Rewrite the question, because as it stands, it doesn't make much sense. Use FileOpenDialog as Filburt has suggested. – walther Apr 10 '14 at 10:57
  • @filbert I have that worked out, its kinda related to my problem, if I use image.source = dialog.filename it will not work. The image has to be in the application folder. – D0LB33 Apr 10 '14 at 10:58
  • @walther Please read my comment to filbert. I know about OpenFileDialog but the filename gives you a direct location C:\your\location.png and WPF wants the location to be in the application folder. And I don't want to use application folder. I want to make it direct. – D0LB33 Apr 10 '14 at 10:59
  • Your application needs access to the specified folder and file. If it doesn't have sufficient permissions, you won't ever bypass the limitation. The image doesn't have to be in the application folder. – walther Apr 10 '14 at 11:04
  • By the way, Windows uses '\' in paths... – rodit Apr 10 '14 at 11:06
  • If I use an absolute location it doesn't show image. Im about to try this Image1.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/images/imagenamehere", UriKind.Absolute)) @walther – D0LB33 Apr 10 '14 at 11:08
  • Don't post the answer as part of the question, instead post an answer yourself and accept it. – walther Apr 10 '14 at 12:30

1 Answers1

0

I believe I have the solution to your problem (or what I think your asking)...

Let's say your image source folder is 'C:\Images\ProgramImages'

(notice how I have used a '\' not a '/' in my path...)

Firstly, create a new form called 'ImageBrowser'. Then, add a ListBox control and name it 'ImageBrowserLB'. After this, double click on the ImageBrowser form so it takes you to the code editor. In the auto-generated sub, type the following:

For Each F As File In System.IO.Directory.GetFiles("C:\Images\ProgramImages\")
    If Path.Extension(F) = ".png" Then
        ImageBrowserLB.Items.Add(Path.GetFileName(F))
    End If
Next

When that form loads, it will add all the files with a PNG extension in the directory I specified. I'm sure you can take this code and manipulate it to do what you would like (which isn't very clear in your question!).

Hope this helped,

Rodit

rodit
  • 1,746
  • 2
  • 19
  • 33
  • Thanks! Not exactly what I needed, but now I know something new! :) The real answer is Image1.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/images/imagenamehere", UriKind.Absolute)) – D0LB33 Apr 10 '14 at 11:11
  • Notice the Urikind.Absolute is what I needed. Ive never been good at explaining problems but thanks so much! :) – D0LB33 Apr 10 '14 at 11:12
  • You could have used Image.FromFile('file') for that :) – rodit Apr 10 '14 at 12:45