-1

Following picture shows what my project is.

My project

You can find my project codes in the following for your testing needs.

<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

I am having following errors when Option Strict on.

'Public Shared Function Create(source As System.Windows.Media.Imaging.BitmapSource) As System.Windows.Media.Imaging.BitmapFrame': Option Strict On disallows implicit conversions from System.Windows.Media.ImageSource' to 'System.Windows.Media.Imaging.BitmapSource'.

...

'Public Shared Function Create(bitmapStream As System.IO.Stream) As System.Windows.Media.Imaging.BitmapFrame': Value of type 'System.Windows.Media.ImageSource' cannot be converted to 'System.IO.Stream'.

...

'Public Shared Function Create(bitmapUri As System.Uri) As System.Windows.Media.Imaging.BitmapFrame': Value of type 'System.Windows.Media.ImageSource' cannot be converted to 'System.Uri

1 Answers1

0

With the Option Strict option you must pass an Uri to the BitmapFrame.Create method for your code to even compile:

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
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes, probably: https://stackoverflow.com/questions/3056514/difference-between-directcast-and-ctype-in-vb-net – mm8 Sep 14 '17 at 10:26