Created
September 8, 2020 01:46
-
-
Save karenpayneoregon/9e041c7fc2a6afed6843ab3a42a3b581 to your computer and use it in GitHub Desktop.
Get resource images for VB.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class ResourceImages | |
Private Shared _instance As ResourceImages | |
Private _dtBitMapImages As DataTable | |
Private _dtIconImages As DataTable | |
Public Function BitMaps() As DataTable | |
If _dtBitMapImages Is Nothing Then | |
GetBitMapImages() | |
End If | |
Return _dtBitMapImages | |
End Function | |
Public Function Icons() As DataTable | |
If _dtIconImages Is Nothing Then | |
GetIconImages() | |
End If | |
Return _dtIconImages | |
End Function | |
Public Shared Function GetInstance() As ResourceImages | |
If _instance Is Nothing Then | |
_instance = New ResourceImages | |
End If | |
Return _instance | |
End Function | |
''' <summary> | |
''' Save image by Identifier field value | |
''' </summary> | |
''' <param name="fileName"></param> | |
''' <param name="identifier"></param> | |
''' <remarks></remarks> | |
Public Sub SaveImage(fileName As String, identifier As Integer) | |
_dtBitMapImages. | |
Select("Identifier =" & identifier)(0). | |
Field(Of Image)("Image").Save(fileName) | |
End Sub | |
''' <summary> | |
''' Save image by tweaking the name shown (for this demo) the ComboBox, | |
''' replacing spaces with underscore char, default to bin\debug folder | |
''' and yes we add the image extension. | |
''' </summary> | |
''' <param name="fileName"></param> | |
''' <param name="displayName"></param> | |
''' <remarks></remarks> | |
Public Sub SaveBitMapImage(fileName As String, displayName As String) | |
_dtBitMapImages.Select("Name ='" & displayName & "'")(0). | |
Field(Of Image)("Image").Save(fileName) | |
End Sub | |
Public Sub SaveIconImage(fileName As String, displayName As String) | |
_dtIconImages.Select("Name ='" & displayName & "'")(0). | |
Field(Of Image)("Image").Save(fileName) | |
End Sub | |
''' <summary> | |
''' Retrieve images from project resources | |
''' </summary> | |
''' <remarks></remarks> | |
Private Sub GetBitMapImages() | |
_dtBitMapImages = New DataTable | |
_dtBitMapImages.Columns.AddRange(New DataColumn() _ | |
{ | |
New DataColumn("Identifier", GetType(Int32)), | |
New DataColumn("Name", GetType(System.String)), | |
New DataColumn("Image", GetType(Image)) | |
} | |
) | |
_dtBitMapImages.Columns("Identifier").AutoIncrement = True | |
_dtBitMapImages.Columns("Identifier").AutoIncrementSeed = 1 | |
Dim myProperties As Reflection.PropertyInfo() = | |
GetType(My.Resources.Resources).GetProperties(Reflection.BindingFlags.NonPublic Or | |
Reflection.BindingFlags.Instance Or | |
Reflection.BindingFlags.Static) | |
Dim bitMaps = (From T In myProperties Where T.PropertyType Is GetType(Bitmap)).ToList | |
If bitMaps.Count > 0 Then | |
For Each pInfo As Reflection.PropertyInfo In bitMaps | |
_dtBitMapImages.Rows.Add(New Object() _ | |
{ | |
Nothing, | |
pInfo.Name.Replace("_", " "), | |
My.Resources.ResourceManager.GetObject(pInfo.Name) | |
} | |
) | |
Next | |
End If | |
End Sub | |
Public Function GetSingleBitMap(name As String) As Image | |
Return BitMaps().AsEnumerable(). | |
FirstOrDefault(Function(row) row.Field(Of String)("Name") = name). | |
Field(Of Image)("image") | |
End Function | |
Private Sub GetIconImages() | |
_dtIconImages = New DataTable | |
_dtIconImages.Columns.AddRange(New DataColumn() _ | |
{ | |
New DataColumn("Identifier", GetType(Int32)), | |
New DataColumn("Name", GetType(System.String)), | |
New DataColumn("Image", GetType(Icon)) | |
} | |
) | |
_dtIconImages.Columns("Identifier").AutoIncrement = True | |
_dtIconImages.Columns("Identifier").AutoIncrementSeed = 1 | |
Dim myProperties As Reflection.PropertyInfo() = | |
GetType(My.Resources.Resources).GetProperties(Reflection.BindingFlags.NonPublic Or | |
Reflection.BindingFlags.Instance Or | |
Reflection.BindingFlags.Static) | |
Dim bitMaps = (From T In myProperties Where T.PropertyType Is GetType(Icon)).ToList | |
If bitMaps.Count > 0 Then | |
Try | |
For Each pInfo As Reflection.PropertyInfo In bitMaps | |
_dtIconImages.Rows.Add(New Object() _ | |
{ | |
Nothing, | |
pInfo.Name.Replace("_", " "), | |
My.Resources.ResourceManager.GetObject(pInfo.Name) | |
} | |
) | |
Next | |
Catch ex As Exception | |
' | |
' Decide how you want to handle this | |
' | |
Console.WriteLine(ex.Message) | |
End Try | |
End If | |
End Sub | |
Protected Sub New() | |
End Sub | |
End Class | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class Form1 | |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load | |
ListBox1.DisplayMember = "Name" | |
ListBox1.DataSource = ResourceImages.GetInstance().BitMaps() | |
End Sub | |
Private Sub GetImageButton_Click(sender As Object, e As EventArgs) Handles GetImageButton.Click | |
PictureBox1.Image = ResourceImages.GetInstance().GetSingleBitMap(ListBox1.Text) | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment