0 Replies - 2219 Views - Last Post: 27 March 2014 - 01:35 AM

#1 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

A couple of useful extension methods for dealing with Images in WPF

Posted 27 March 2014 - 01:35 AM

Imports System.IO
Imports System.Windows.Media.Imaging
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.CompilerServices 

Namespace Global.Exts

  Namespace Imagery

    Public Module Exts
      Public Function ToWinFormsBitmap(bitmapsource As BitmapSource) As Bitmap
        If bitmapsource Is Nothing Then Return Nothing 
        Using mem As New MemoryStream
          Dim enc As New BmpBitmapEncoder
          enc.Frames.Add(CType(bitmapsource, BitmapFrame))
          Using temp As New Bitmap(mem)
            Return New Bitmap(temp)
          End Using
        End Using
      End Function

      Public Function ToWPF_Bitmap(bitmap As Bitmap) As BitmapSource
        If bitmap Is Nothing Then Return Nothing
        Using mem As New MemoryStream
          bitmap.Save(mem, ImageFormat.Bmp)
          mem.Position = 0
          Dim result As New BitmapImage
          With result
            .CacheOption = BitmapCacheOption.onload
            .StreamSource = mem
          End With
          Return result
        End Using
      End Function
    End Module

  End Namespace

End Namespace

Is This A Good Question/Topic? 0
  • +

Page 1 of 1