Advertisement
3DotDev

Classe d'extraction de ressource Bitmap (P/Invoke)

Aug 16th, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.79 KB | None | 0 0
  1. '########################################################
  2. ' Credits 3DotDev from http://3dotdevcoder.blogspot.fr/
  3. '########################################################
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class Cls_ExtractBmp
  7.  
  8.                 Private bmp As Bitmap
  9.                 Private hRes As IntPtr = IntPtr.Zero
  10.                 Private hLib As IntPtr = IntPtr.Zero
  11.                 Private m_filename As String = String.Empty
  12.                 Private m_ResName As Integer = 0
  13.  
  14.                 Private Const LOAD_LIBRARY_AS_IMAGE_RESOURCE As Integer = 32
  15.                 Private Const LOAD_LIBRARY_AS_DATAFILE As Integer = 2
  16.  
  17.                 Private Declare Function LoadLibraryEx Lib "kernel32" (ByVal lpLibFileName As String, ByVal hModule As IntPtr, ByVal dwFlags As Integer) As IntPtr
  18.  
  19.                 Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As IntPtr
  20.  
  21.                 Private Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As IntPtr, ByVal lpBitmapName As String) As IntPtr
  22.  
  23.                 Public Sub New(ByVal Filename As String, ByVal ResName As Integer)
  24.                         m_filename = Filename
  25.                         m_ResName = ResName
  26.                 End Sub
  27.  
  28.                 Public Property Filename() As String
  29.                         Get
  30.                                 Return m_filename
  31.                         End Get
  32.                         Set(ByVal value As String)
  33.                                 m_filename = value
  34.                         End Set
  35.                 End Property
  36.  
  37.                 Public Property ResName() As String
  38.                         Get
  39.                                 Return m_ResName
  40.                         End Get
  41.                         Set(ByVal value As String)
  42.                                 m_ResName = value
  43.                         End Set
  44.                 End Property
  45.  
  46.                 Private ReadOnly Property Handle() As IntPtr
  47.                         Get
  48.                                 Return hLib
  49.                         End Get
  50.                 End Property
  51.  
  52.                 Public Function GetImg() As Bitmap
  53.                         ClearUp()
  54.                         hLib = LoadLibraryEx(m_filename, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE + LOAD_LIBRARY_AS_IMAGE_RESOURCE)
  55.                         If hLib <> Nothing Then
  56.                                 hRes = LoadBitmap(hLib, ResName)
  57.                                 If hRes <> 0 Then
  58.                                         bmp = Bitmap.FromHbitmap(hRes)
  59.                                         FreeLibrary(hLib)
  60.                                 End If
  61.                         End If
  62.                         Return bmp
  63.                 End Function
  64.  
  65.                 Private Sub ClearUp()
  66.                         If Not hLib.Equals(IntPtr.Zero) Then
  67.                                 FreeLibrary(hLib)
  68.                                 hLib = IntPtr.Zero
  69.                         End If
  70.                 End Sub
  71.  
  72.         End Class
  73.  
  74. '##############################
  75. ' Comment utiliser cette classe
  76. '##############################
  77.  
  78.         'Instanciation d'un objet de type PictureBox
  79.         Dim Pbx as new PictureBox
  80.  
  81.         'Si PictureBox déjà chargée alors suppression de l'image
  82.         If Not (Pbx.Image Is Nothing) Then
  83.             Pbx.Image.Dispose()
  84.             Pbx.Image = Nothing
  85.         End If
  86.  
  87.         'Affichage de la ressource Bitmap 6801 dans la PictureBox en utilisant la classe "Cls_ExtractBmp"
  88.         If File.Exists(My.Application.GetEnvironmentVariable("SYSTEMDRIVE") & "Windows\explorer.exe") Then
  89.             Dim ext1 As New Cls_ExtractBmp(My.Application.GetEnvironmentVariable("SYSTEMDRIVE") & "Windows\explorer.exe", 6801)
  90.             Pbx.Image = ext1.GetImg
  91.         End If
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement