Public Enum eImageType
PNG = 0
JPG = 1
BMP = 2
GIF = 3
Icon = 4
WMF = 5
TIFF = 6
End Enum
'''
''' Given a valid resource image name that includes [namespace].[imagename].[extension]
''' and the proper image type selection this function returns an image source that can be
''' prgramatically used as a WPF ImageSource object.
'''
'''
'''
'''
''' Given a valid resource image name that includes [namespace].[imagename].[extension]
''' and the proper image type selection this function returns an image source that can be
''' prgramatically used as a WPF ImageSource object.
Public Shared Function GetResourceImage(ByVal ImageNameIn As String, ByVal TypeIn As eImageType) As ImageSource
Dim MyStream As System.IO.Stream = Nothing
Dim MyDecoder As BitmapDecoder = Nothing 'PngBitmapDecoder = Nothing
Dim MyImage As ImageSource = Nothing
Dim MyAssembly As System.Reflection.Assembly = Nothing
Try
'get a reference to the containing assembly
MyAssembly = System.Reflection.Assembly.GetExecutingAssembly
'get a memory stream of the embedded image
MyStream = MyAssembly.GetManifestResourceStream(ImageNameIn)
'make sure we have a valid stream instance
If MyStream IsNot Nothing Then
'get the appropriate decoder
Select Case TypeIn
Case eImageType.BMP
MyDecoder = New BmpBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.GIF
MyDecoder = New GifBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.Icon
MyDecoder = New IconBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.JPG
MyDecoder = New JpegBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.PNG
MyDecoder = New PngBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.TIFF
MyDecoder = New TiffBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
Case eImageType.WMF
MyDecoder = New WmpBitmapDecoder(MyStream, _
BitmapCreateOptions.PreservePixelFormat, _
BitmapCacheOption.Default)
End Select
MyImage = MyDecoder.Frames(0)
End If
Catch ex As Exception
ProcessException(ex, eExceptionsOptions.ShowMessageAndLog)
Finally
MyStream.Close()
End Try
Return MyImage
End Function