Yeah, i looked at it some more and the code is not correct. Two things are
incorrect:
First of all, you don't need 'hdcOriginal', because you release 'hdcBitmap'
anyway. So the line becomes:
SelectObject(hdcBitmap, v_BackImage.GetHbitmap)
and you can remove:
DeleteObject(hdcOriginal)
but you already did that.
Secondly, and more importantly, you need to delete the handle to the bitmap:
Dim hdcBitmapHandle As IntPtr = v_BackImage.GetHbitmap
....
SelectObject(hdcBitmap, hdcBitmapHandle )
....
DeleteObject(hdcBitmapHandle)
If you don't do this, you're application will run for a while, but then you
will get an error somthing like "Error: object still in use.".
Off-topic remark:
If speed is important for you application, notice that the line
"SelectObject(hdcBitmap, hdcBitmapHandle )" takes up about 85% of the total
time to perform this. You might consider creating a class that holds and
releases the handle to the bitmap only 1 time. First I thought keeping a
handle to a bmp would cause problems, but it seems to work ok. Now BitBlt is
40% faster then DrawImage method.
"Dennis" <(E-Mail Removed)> schreef in bericht
news:2209D59F-A3D9-4212-BBCF-(E-Mail Removed)...
> Thanks a lot for the reply...I know it took a lot of you time. I included
> it
> into my program and it works fine except for one problem. The line
> "DeleteObject(hdcOriginal)" somehow causes the form containing my control
> to
> lose it's identity such that it throw an exception "Object not set to
> reference" when I try to access it's properties such as me.Enabled. If I
> delete the line "DeleteObject(hdcOriginal)" then it works fine.
>
> "Qwert" wrote:
>
>> Private Declare Function SelectObject Lib "gdi32" Alias "SelectObject"
>> (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
>> Private Declare Function DeleteObject Lib "gdi32" Alias "DeleteObject"
>> (ByVal hObject As IntPtr) As Integer
>> ....
>> Dim hdcBitmap, hdcControl, hdcOriginal As IntPtr
>> ....
>> hdcBitmap = gph.GetHdc
>> hdcControl = e.Graphics.GetHdc
>> hdcOriginal = SelectObject(hdcBitmap, v_BackImage.GetHbitmap)
>> BitBlt(hdcControl, 0, 0, v_BackImage.Width, v_BackImage.Height,
>> hdcBitmap,
>> 0, 0, &HCC0020)
>> SelectObject(hdcBitmap, hdcOriginal)
>> DeleteObject(hdcOriginal)
>> e.Graphics.ReleaseHdc(hdcControl)
>> gph.ReleaseHdc(hdcBitmap)
>> ....
>> gph.Dispose() ' When you're done.
>>
>>
>> "Dennis" <(E-Mail Removed)> schreef in bericht
>> news:017F3DEA-8243-4CBA-BD15-(E-Mail Removed)...
>> >I am trying to implement drawing on a bitmap and using bitblt to
>> >transfer
>> > it to the control graphics object in the paint event. It seems to draw
>> > on
>> > the bitmap ok but doesn't get transferred to the control graphics
>> > object
>> > in
>> > the paint event. Any help would be appreciated. Here is my code:
>> >
>> > public class as mycontrol
>> >
>> > Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As
>> > IntPtr, ByVal nxDest As Integer, ByVal nyDest As Integer, ByVal nWidth
>> > As
>> > Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc
>> > As
>> > Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean
>> >
>> > Private v_BackImage As Bitmap = New Bitmap(Me.Width, Me.Height,
>> > Me.CreateGraphics)
>> > Private gph As Graphics = Graphics.FromImage(v_BackImage)
>> >
>> > 'do some drawing on gph
>> >
>> > Private Sub Panel_Paint(ByVal sender As Object, ByVal e As
>> > System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
>> > 'Get Device contexts for Source and Memory Graphics Objects
>> > Dim hdcSrc As IntPtr = gph.GetHdc
>> > Dim hdcDest As IntPtr = e.Graphics.GetHdc
>> > BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width,
>> > Me.Height,
>> > 13369376)
>> > 'Clean Up
>> > gph.ReleaseHdc(hdcSrc)
>> > e.Graphics.ReleaseHdc(hdcDest)
>> > end sub
>> >
>> > end class
>> >
>> > --
>> > Dennis in Houston
>>
>>
>>