Hier ist die Klasse komplett mit Aufruf
Imports System.Runtime.InteropServices
Imports System.IO
Public Class ImageLoader
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function FindResource(ByVal hhMod As IntPtr, ByVal lpID As _
Integer, ByVal lpType As String) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function LoadResource(ByVal hhMod As IntPtr, ByVal hResInfo _
As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function LockResource(ByVal hResData As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function SizeofResource(ByVal hhMod As IntPtr, ByVal _
hResInfo As IntPtr) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function FreeLibrary(ByVal hModule As IntPtr) As Boolean
End Function
Public Shared Function GetImageFromResource(ByVal resourceId As Integer, _
ByVal moduleName As String) As Image
Dim hModule As IntPtr = LoadLibrary(moduleName)
If hModule = IntPtr.Zero Then Return Nothing
Dim resource As IntPtr = FindResource(hModule, resourceId, "IMAGE")
If resource = IntPtr.Zero Then Return Nothing
Dim rsrcid As IntPtr = LoadResource(hModule, resource)
If rsrcid = IntPtr.Zero Then Return Nothing
Dim memory As IntPtr = LockResource(rsrcid)
If memory = IntPtr.Zero Then Return Nothing
Dim size As Integer = SizeofResource(hModule, resource)
Dim bytes(size - 1) As Byte
Marshal.Copy(memory, bytes, 0, size)
Dim memStream As New MemoryStream(bytes)
FreeLibrary(hModule)
Return Image.FromStream(memStream)
End Function
End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
PictureBox1.Image = ImageLoader.GetImageFromResource(5031, _
"imageres.dll")
End Sub ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |