Rubrik: Grafik und Font · Bilder & Icons | VB-Versionen: VB6 | 07.12.11 |
Größe einer Grafik mit GDI+ ermitteln Mit dieser Funktion lässt sich schnell die Größe (Breite x Höhe) einer Grafik mittels GdiPlus ermitteln. | ||
Autor: Dieter Otter | Bewertung: | Views: 9.039 |
www.tools4vb.de | System: WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Vor langer Zeit haben wir Ihnen schon einmal einen Tipp vorgestellt, mit dem sich neben BMP, GIF und JPG-Bildern auch TIFF oder PNG-Bilder in VB laden lassen. Zum Einsatz kamen hier die Funktionen aus dem GDI+ API von Windows:
Ersatz für die VB6-LoadPicture Funktion
Wir möchten die Reihe der GDI+ Funktionen nun fortsetzen und zeigen Ihnen mit nachfolgendem Code, wie sich die Maße (Breite und Höhe) einer Grafik in Pixel ermitteln lässt.
Packen Sie nachfolgenden Code in ein Modul.
Option Explicit ' benötigte API-Deklarationen Private Type GdiplusStartupInput GdiplusVersion As Long DebugEventCallback As Long SuppressBackgroundThread As Long SuppressExternalCodecs As Long End Type Private Declare Function GdiplusStartup Lib "gdiplus" ( _ Token As Long, _ InputBuf As GdiplusStartupInput, _ Optional ByVal OutputBuf As Long = 0) As Long Private Declare Function GdiplusShutdown Lib "gdiplus" ( _ ByVal Token As Long) As Long Private Declare Function GdipCreateBitmapFromFile Lib "gdiplus" ( _ ByVal Filename As Long, _ hBitmap As Long) As Long Private Declare Function GdipGetImageDimension Lib "gdiplus" ( _ ByVal hImage As Long, _ Width As Single, _ Height As Single) As Long Private Declare Function GdipDisposeImage Lib "gdiplus" ( _ ByVal hImage As Long) As Long
' Gibt die Größe (Breite x Höhe) einer Grafik in Pixel zurück Public Function GetImageDim(ByVal File As String, _ ByRef Width As Long, ByRef Height As Long) As Boolean Dim SI As GdiplusStartupInput Dim hBitmap As Long Dim Token As Long Dim bResult As Boolean Dim nWidth As Single Dim nHeight As Single ' GDI+ initialisieren SI.GdiplusVersion = 1 If GdiplusStartup(Token, SI) = 0 Then ' Bilddatei erstellen If GdipCreateBitmapFromFile(StrPtr(File), hBitmap) = 0 Then If GdipGetImageDimension(hBitmap, nWidth, nHeight) = 0 Then Width = CLng(nWidth) Height = CLng(nHeight) bResult = True End If ' Freigeben GdipDisposeImage hBitmap End If ' GDI+ beenden GdiplusShutdown Token End If GetImageDim = bResult End Function
Aufrufbeispiel:
Dim nHeight As Long Dim nWidth As Long Dim sFile As String sFile = "D:\temp\MyPicture.png" If GetImageDim(sFile, nWidth, nHeight) Then MsgBox "Größe: " & CStr(nWidth) & " x " & CStr(nHeight) & " Pixel" Else MsgBox "Grafik-Informationen können nicht ermittelt werden." End If