GDI vs GDI+

Darc

Well-known member
Joined
Apr 18, 2003
Messages
89
hey, this kinda falls under interop too, but I wanted to know if GDI (BitBlt) would be faster than .NETs GDI+. I found a way to use old GDI with C++ .DLLs so would it be faster under C++?
 
You dont need to create any C++ DLL to use GDI32 in your .NET app. You can simply declare the function and use it.
As far as comparison goes. A lot of people will say that GDI32 is faster than GDI+. But using GDI+ from .NET is MUCH easier.
 
Yes, GDI is much faster.

I made a class library a while back to make GDI easier to use. Feel free to expand on it to meet your own needs. If you want to see a sample of it being used, do a search for Sharp Invader. Its a game I made using this GDI library.
 

Attachments

I dont have C#, I have VB and C++.

btw, for using bitblt, do I just use GetWindowsDC(Me.Handle) or something like that?
 
ok, I tried the following but it gave me:
An unhandled exception of type System.InvalidOperationException occurred in system.windows.forms.dll

Additional information: The object is currently in use elsewhere.

heres the code:
Code:
    Public Sub Blt(ByVal img As Image, ByVal e As Graphics, ByVal rct As Rectangle)
        Dim graph As Graphics = Graphics.FromImage(img)
        Dim imgHDC As Long = graph.GetHdc.ToInt64
        Dim hdc As Long = e.GetHdc.ToInt64
        Dim retval As Long

        retval = BitBlt(hdc, CRD_AREA.X, CRD_AREA.Y, CRD_AREA.Width, CRD_AREA.Height, imgHDC, 0, 0, &HCC0020)

        graph.Dispose()
    End Sub
 
BitBlt uses Int32 not Int64 (Integer not Long)
Code:
Public Sub Blt(ByVal img As Image, ByVal e As Graphics, ByVal rct As Rectangle)
        Dim graph As Graphics = Graphics.FromImage(img)
        Dim imgHDC As Integer = graph.GetHdc.ToInt32
        Dim hdc As Integer = e.GetHdc.ToInt32
        Dim retval As Integer

        retval = BitBlt(hdc, CRD_AREA.X, CRD_AREA.Y, CRD_AREA.Width, CRD_AREA.Height, imgHDC, 0, 0, &HCC0020)

        graph.ReleaseDC(imgHDC)
        graph.Dispose()

        e.ReleaseDC(hdc)
    End Sub
GDI+ has more features then GDI, If you use GDI+ effeciently the slower speed will make minimal difference
 
Im just using it in a couple places for boosts of speed (simple square images)

just a question, how can I convert the integers to IntPtr, ReleaseHDC doesnt have an overload for Integer
 
Last edited by a moderator:
I recommend using this BitBlt declaration:
Code:
Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As IntPtr, ByVal x As Int32, _
 ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, _
  ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32
The Bitblt function can now use IntPtr objects, it works fine.

Or if you dont want to do that you can just alter the function:
Code:
    Public Sub Blt(ByVal img As Image, ByVal e As Graphics, ByVal rct As Rectangle)
        Dim graph As Graphics = Graphics.FromImage(img)
        Dim imgHDC As IntPtr = graph.GetHdc
        Dim hdc As IntPtr = e.GetHdc
        Dim retval As Integer

        retval = BitBlt(hdc.ToInt32, CRD_AREA.X, CRD_AREA.Y, CRD_AREA.Width, CRD_AREA.Height, imgHDC.ToInt32, 0, 0, &HCC0020)

        graph.ReleaseDC(imgHDC)
        graph.Dispose()

        e.ReleaseDC(hdc)
    End Sub
 
Back
Top