Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Loading GIFs, JPGs to a Surface
By: W-Buffer

It's relatively easy, we don't need some decoder to get a JPG Bytes, or anything like that, instead we're gonna use a PictureBox (StdPicture) to load the image and the pass it to a Surface, but first we need this API calls:

Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC 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 dwRop As Long) As Long

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

Public Declare Function DeleteDC 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

Now we need to have something to load on the image:
Dim Pict1 As StdPicture
Set Pict1 = LoadPicture("MyPict.jpg")

Now lets create the surface:
Dim TDesc As DDSurfaceDesc2

TDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
TDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
TDesc.lHeight = CLng((Pict1.Height * 0.001) * 567 / Screen.TwipsPerPixelY)
TDesc.lWidth = CLng((TPict.Width * 0.001) * 567 / Screen.TwipsPerPixelX)

Set Surf = DDraw.CreateSurface(TDesc)

Dim SurfDC As Long, PictDC As Long
SurfDC = Surf.GetDC
PictDC = CreateCompatibleDC(0)
SelectObject Pict1.Handle, PictDC

Now we're gonna copy from Pict1 to Surf using DCs and BitBlt or StretchBlt:

To only Copy at the same Size:
BitBlt SurfDC, 0, 0, TDesc.lWidth, TDesc.lHeight, PictDC, 0, 0, vbSrcCopy

Now to Copy Streching the Image:
StretchBlt SurfDC, 0, 0, StretchWidth, StretchHeight, PictDC, 0, 0, TDesc.lWidth, TDesc.lWidth, vbSrcCopy

And now Release DCs:
Surf.ReleaseDC SurfDC
DelecteDC PictDC
Set Pict1 = Nothing

What's Next?, Download the SurfUtil.Bas that I created to Load Images with this Technique!

Notes: Some times StretchBlt dosen't correctly display GIFs, thats why i added the BitBlt option.