下图显示了我的项目。
您可以在下面找到我的项目代码,以满足您的测试需求。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="MainWindow"
Height="300"
Width="400">
<Grid>
<Image Name="Image1" Height="100" Width="125" HorizontalAlignment="Left"/>
<Button Name="Button1" Content="Button1" Height="30" Width="125" HorizontalAlignment="Center"/>
<Button Name="Button2" Content="Button2" Height="30" Width="125" HorizontalAlignment="Right"/>
</Grid>
</Window>
...
Class MainWindow
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim myOFD As New Microsoft.Win32.OpenFileDialog
myOFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
myOFD.Filter = "Image File (*.png)|*.png"
If myOFD.ShowDialog = True Then
Dim myBitmap As New BitmapImage
myBitmap.BeginInit()
myBitmap.UriSource = New Uri(uriString:=myOFD.FileName, UriKind:=UriKind.RelativeOrAbsolute)
myBitmap.EndInit()
myBitmap.Freeze()
Image1.Source = myBitmap
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim myBitmapEncoder As New PngBitmapEncoder()
myBitmapEncoder.Frames.Add(BitmapFrame.Create(Image1.Source))
End Sub
End Class
Option Strict on时,我遇到以下错误。
'公共共享函数Create(source As System.Windows.Media.Imaging.BitmapSource)As System.Windows.Media.Imaging.BitmapFrame':Option Strict On禁止从System.Windows.Media.ImageSource'到'的隐式转换System.Windows.Media.Imaging.BitmapSource”。
...
'公共共享函数Create(bitmapStream As System.IO.Stream)As System.Windows.Media.Imaging.BitmapFrame':'System.Windows.Media.ImageSource'类型的值无法转换为'System.IO。流”。
...
'公共共享函数Create(bitmapUri As System.Uri)As System.Windows.Media.Imaging.BitmapFrame':'System.Windows.Media.ImageSource'类型的值无法转换为'System.Uri
答案 0 :(得分:0)
使用Option Strict
选项,您必须将Uri
传递给BitmapFrame.Create
方法,以便您的代码进行编译:
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim source = DirectCast(Image1.Source, BitmapImage)
Dim myBitmapEncoder As New PngBitmapEncoder()
myBitmapEncoder.Frames.Add(BitmapFrame.Create(source.UriSource))
End Sub