Tipp 0189 GIF- & JPG-Grafiken in DD-Surface laden
Autor/Einsender:
Datum:
  Alexander Csadek
19.01.2001
Entwicklungsumgebung:
DirectX-Version:
  VB 6
DirectX 7
Die Funktion CreateSurfaceFromFile vom DirectDraw7-Objekt unterstützt nur Bitmaps. Über die Intel-JPG-DLL (siehe Tipp JPG-Grafiken in DirectDraw verwenden) können auch JPG's geladen werden. Aber was ist mit den GIF-Grafiken?
Dieser Tipp zeigt eine Lösung, wie mittels API-Funktionen und ohne zusätzliche DLL oder Steuerelement, JPG- und GIF-Grafiken in ein DirectDraw-Surface geladen werden können.
Zunächst braucht man ein StdPicture-Objekt, welches zum Laden und Konvertieren der Bilder dient. Danach wird eine leere Surface erstellt, ein Device zum Bild und zu der Surface. Mit der Funktion StretchBlt wird dann das Bild im Speicher vom StdPicture-Objekt in die Surface kopiert.
Der abgebildete Code bezieht sich hauptsächlich auf die neue CreateSurfaceFromFile-Funktion. Da der Code für die Initialisierung von DirectX meist immer gleich bleibt, wird dieser hier nicht mehr gesondert abgebildet.
Code im Codebereich des Moduls
 
Option Explicit

Public Const SRCCOPY = &HCC0020

Public Declare Function CreateCompatibleDC Lib "gdi32" ( _
      ByVal hdc As Long) As Long

Public Declare Function SelectObject Lib "gdi32" ( _
      ByVal hdc As Long, ByVal hObject As Long) As Long

Public Declare Function StretchBlt Lib "gdi32" ( _
      ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
      ByVal nWidth As Long, ByVal nHeight As Long, _
      ByVal hSrcDC As Long, ByVal xSrc As Long, _
      ByVal ySrc As Long, ByVal nSrcWidth As Long, _
      ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Public Declare Function DeleteDC Lib "gdi32" ( _
      ByVal hdc As Long) As Long
 
Code im Codebereich der Form frmMain
 
Function CreateSurfaceFromFile(DirectDraw As DirectDraw7, ByVal _
        FileName As String, SurfaceDesc As DDSURFACEDESC2) As _
        DirectDrawSurface7
  Dim Picture As StdPicture
  Dim Width As Long
  Dim Height As Long
  Dim Surface As DirectDrawSurface7
  Dim hdcPicture As Long
  Dim hdcSurface As Long

  Set Picture = LoadPicture(FileName)

  Width = CLng((Picture.Width * 0.001) * 567 / _
          Screen.TwipsPerPixelX)
  Height = CLng((Picture.Height * 0.001) * 567 / _
          Screen.TwipsPerPixelY)

  With SurfaceDesc
    If .lFlags = 0 Then .lFlags = DDSD_CAPS
    .lFlags = .lFlags Or DDSD_WIDTH Or DDSD_HEIGHT
    If .ddsCaps.lCaps = 0 Then _
          .ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
    If .lWidth = 0 Then .lWidth = Width
    If .lHeight = 0 Then .lHeight = Height
  End With
  
  Set Surface = DirectDraw.CreateSurface(SurfaceDesc)

  hdcPicture = CreateCompatibleDC(0)

  SelectObject hdcPicture, Picture.Handle

  hdcSurface = Surface.GetDC

  StretchBlt hdcSurface, 0, 0, SurfaceDesc.lWidth, _
        SurfaceDesc.lHeight, hdcPicture, 0, 0, _
        Width, Height, SRCCOPY

  Surface.ReleaseDC hdcSurface

  DeleteDC hdcPicture

  Set Picture = Nothing
  Set CreateSurfaceFromFile = Surface
  Set Surface = Nothing
End Function
 
Weitere Links zum Thema
JPG-Grafiken in DirectDraw verwenden
Hinweis
Um dieses Beispiel ausführen zu können, wird die DirectX 7 for Visual Basic Type Library benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (30,7 kB) Downloads bisher: [ 1639 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Dienstag, 20. September 2011