使用WPF显示和保存图像

时间:2015-04-14 16:41:12

标签: .net wpf vb.net winforms

我决定将我的WinForm项目移植到WPF,但是我遇到了一个问题: 我的软件有一个用户空间。通常,通过OpenFileDialog选择图像,该图像由控件“Image”(代码中的roundPB)存储和使用。但是,我的WinForm代码在WPF中不起作用,因为PictureBox的属性与“Image”的属性不同。 这是我目前的非功能性代码:

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

绝对没有任何作用,既不是roundPB(没有显示图片)也不是保存,但没有返回错误消息。 你能开导我吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

表达式

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

应该给你一个绝对的路径,例如C:\Program Files\MTS\profpic.png,那么为什么要尝试创建一个亲戚Uri?

根本不要设置UriKind

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

为了保存文件,您期望下面一行的结果是什么?

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

您应该删除ImageRep前缀。

答案 1 :(得分:0)

我使用BitmapImage解决了我的问题,就像Clemens说的那样。这是我的功能代码:

    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