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

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • 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

    <HideModuleName>
    Public Module Exts
      <Extension>
      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))
          enc.Save(mem)
          Using temp As New Bitmap(mem)
            Return New Bitmap(temp)
          End Using
        End Using
      End Function

      <Extension>
      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
            .BeginInit
            .CacheOption = BitmapCacheOption.onload
            .StreamSource = mem
            .EndInit
            .Freeze
          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