0

I decided to port my WinForm project to WPF but I encounter a problem: My software has a user space. Normally, an image is selected via OpenFileDialog, this image is both stored and used by the control "Image" (roundPB in the code). But, my WinForm code does not work in WPF because the properties are not the same for a PictureBox for a "Image". Here is my current nonfunctional code :

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim OpenFileDialog2 As New Microsoft.Win32.OpenFileDialog()
    Dim ImagePath As String = Nothing
    If OpenFileDialog2.ShowDialog() = vbOK Then
        ImagePath = OpenFileDialog2.FileName
        My.Computer.FileSystem.CopyFile(ImagePath, ImageRep & System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png", overwrite:=True)
    Else : Exit Sub : End If
    If roundPB IsNot Nothing Then roundPB.Source.Freeze()
    roundPB.Source = New BitmapImage(New Uri(ImagePath, UriKind.Relative))
End Sub
Private Sub MetroWindow_Loaded(sender As Object, e As RoutedEventArgs)
    If My.Computer.FileSystem.FileExists(System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png") Then
        Dim ImagePath As String = System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png"
        roundPB.Source = New BitmapImage(New Uri(ImagePath, UriKind.Relative))
        roundPB.Source.Freeze()
    End If
End Sub

Absolutely nothing works, neither roundPB (which is not showing the picture) nor the save, but no error message is returned. Can you enlighten me? Thank you!

OneFineDay
  • 9,004
  • 3
  • 26
  • 37

2 Answers2

0

The expression

System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png"

should give you an absolute path, e.g. C:\Program Files\MTS\profpic.png, so why do you try to create a relative Uri?

Do not set the UriKind at all:

roundPB.Source = New BitmapImage(New Uri(ImagePath))

For saving the file, what do you expect to be the result of the line below?

ImageRep & System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png", 

You should probably remove the ImageRep prefix.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thank you, I removed UriKind, now the image is well loaded at startup but the save didn't work, nothing happens : the file didn't change and the picture don't change too in the form :/ ImageRep is a residual code from my code on WinForm, It was already deleted in my project :) – Clément POIRET Apr 14 '15 at 19:32
  • WPF may keep the image file open (so that it can't be overwritten) and it also caches the image with the file name as key. So writing a file with the same name won't result in an updated image in the UI. You may either use a different file name or load the BitmapImage from a FileStream, as e.g. shown in [this answer](http://stackoverflow.com/a/29625691/1136211). – Clemens Apr 14 '15 at 19:54
  • Have you a vb.NET exemple ? I'm not able to read C# :/ Thank you – Clément POIRET Apr 15 '15 at 06:39
  • No I don't have one. But there are VB examples in the online documentation on MSDN. Anyway, it may be worth the effort to learn C#. – Clemens Apr 15 '15 at 07:07
  • Thank you so much Clemens, it worked ! I'll post the code below :) – Clément POIRET Apr 15 '15 at 15:47
  • Don't post more code. Just accept the answer to indicate that it was helpful. – Clemens Apr 15 '15 at 15:57
  • Yes but it could be helpful for the guy who is searching a code that works :) – Clément POIRET Apr 15 '15 at 16:56
  • I seriously doubt that. It's too specialized. You may however leave your answer and still accept mine. You know how accepting works? – Clemens Apr 15 '15 at 16:57
0

I solved my problem using BitmapImage like Clemens said. Here is my functional code :

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim OpenFileDialog2 As New Microsoft.Win32.OpenFileDialog()
    Dim ImagePath As String = Nothing
    Dim bitmapimage As New BitmapImage
    If OpenFileDialog2.ShowDialog() = True Then
        ImagePath = OpenFileDialog2.FileName
        bitmapimage.BeginInit()
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad
        bitmapimage.UriSource = New Uri(ImagePath)
        bitmapimage.EndInit()
        My.Computer.FileSystem.CopyFile(ImagePath, My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\MTS\profpic.png", overwrite:=True)
        roundPB.Source = bitmapimage
    Else
    End If
End Sub
Private Sub MetroWindow_Loaded(sender As Object, e As RoutedEventArgs)
    If My.Computer.FileSystem.FileExists(System.Environment.GetEnvironmentVariable("ProgramFiles") & "\MTS\profpic.png") Then
        Dim ImagePath As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\MTS\profpic.png"
        Dim bitmapimage As New BitmapImage
        bitmapimage.BeginInit()
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad
        bitmapimage.UriSource = New Uri(ImagePath)
        bitmapimage.EndInit()
        roundPB.Source = bitmapimage
    End If
End Sub