Im using GDI32 to TransparentBlt graphics to the screen about 10 times a second. But after about 10 seconds I start to suffer severe slowdown. This is the sprite.draw function I have created for my sprite class.
I think the problem resides in this part of the code.
Because when I changed the arguments of the Bitmap to 100,100 (width and height) it created a small rectangle instead, but didnt suffer from the slowdown. I used offscreen.dispose() but that doesnt seem to make any difference.
Any just before anyone suggests it - no, its not the dreaded TransparentBlt memory leak as Im using XP and BitBlt also has this problem (its commented out in there somewhere).
Any help would be greatly appreciated.
Jamie.
Code:
Public Sub Draw(ByVal targetGraphics As Graphics, ByVal ImageType As TileImage, ByVal Tile As Integer, _
ByVal DestX As Integer, ByVal DestY As Integer)
Dim X As Integer
Dim Y As Integer
Y = Math.Floor(Tile / ImageType.RowCount) * ImageType.TileHeight
Math.DivRem((Tile * ImageType.TileWidth), (ImageType.TileWidth * ImageType.RowCount), X)
Row and Column (X & Y position) of the tile to draw
Dim offscreen As Bitmap = New Bitmap(Application.StartupPath & ImageType.Location)
Get the hDC from targetGraphics
Dim targethDC As IntPtr = targetGraphics.GetHdc()
Create an offscreen dc and select our bitmap in to it
Dim offscreenhDC As IntPtr = CreateCompatibleDC(targethDC)
Dim oldObject As IntPtr
oldObject = SelectObject(offscreenhDC, offscreen.GetHbitmap())
Blt the image to the target
TransparentBlt(targethDC, DestX, DestY, ImageType.TileWidth, ImageType.TileHeight, offscreenhDC, X, Y, ImageType.TileWidth, ImageType.TileHeight, ImageType.KeyColor)
Equivalent BitBlt (minus transparency) commented out
BitBlt(targethDC, DestX, DestY, ImageType.TileWidth, ImageType.TileHeight, offscreenhDC, X, Y, SRCCOPY)
Select our bitmap out of the dc and delete it
DeleteDC(SelectObject(offscreenhDC, oldObject))
Release the target hDC
targetGraphics.ReleaseHdc(targethDC)
offscreen.Dispose()
End Sub
I think the problem resides in this part of the code.
Code:
Dim offscreen As Bitmap = New Bitmap(Application.StartupPath & ImageType.Location)
Because when I changed the arguments of the Bitmap to 100,100 (width and height) it created a small rectangle instead, but didnt suffer from the slowdown. I used offscreen.dispose() but that doesnt seem to make any difference.
Any just before anyone suggests it - no, its not the dreaded TransparentBlt memory leak as Im using XP and BitBlt also has this problem (its commented out in there somewhere).
Any help would be greatly appreciated.
Jamie.