fork download
  1. Class MainWindow
  2.  
  3. Private Sub SaveToBmp(visual As FrameworkElement, fileName As String)
  4. Dim encoder = New BmpBitmapEncoder()
  5. SaveUsingEncoder(visual, fileName, encoder)
  6. End Sub
  7.  
  8. Private Sub SaveToPng(visual As FrameworkElement, fileName As String)
  9. Dim encoder = New PngBitmapEncoder()
  10. SaveUsingEncoder(visual, fileName, encoder)
  11. End Sub
  12.  
  13. Private Sub SaveUsingEncoder(visual As FrameworkElement, fileName As String, encoder As BitmapEncoder)
  14. Dim bitmap As New RenderTargetBitmap(CInt(visual.ActualWidth), CInt(visual.ActualHeight), 96, 96, PixelFormats.Pbgra32)
  15.  
  16. bitmap.Render(visual)
  17. Dim frame As BitmapFrame = BitmapFrame.Create(bitmap)
  18. encoder.Frames.Add(frame)
  19.  
  20. Using stream = File.Create(fileName)
  21. encoder.Save(stream)
  22. End Using
  23. End Sub
  24.  
  25. Private Sub load(sender As Object, e As RoutedEventArgs)
  26. Me.SaveToPng(MyGrid, "image.png")
  27. Me.SaveToBmp(MyImage, "image.bmp")
  28. End Sub
  29.  
  30.  
  31. Public Sub New()
  32.  
  33. InitializeComponent()
  34. AddHandler Me.Loaded, AddressOf load
  35.  
  36. 'Here i set MyImage source
  37. 'MyImage.Source = MyImageSource
  38.  
  39. End Sub
  40.  
  41.  
  42. End Class
  43.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty