473,785 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BitMap to Control in Paint Event

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.CreateGraphi cs)
Private gph As Graphics = Graphics.FromIm age(v_BackImage )

'do some drawing on gph

Private Sub Panel_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles MyBase.Paint
'Get Device contexts for Source and Memory Graphics Objects
Dim hdcSrc As IntPtr = gph.GetHdc
Dim hdcDest As IntPtr = e.Graphics.GetH dc
BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width, Me.Height,
13369376)
'Clean Up
gph.ReleaseHdc( hdcSrc)
e.Graphics.Rele aseHdc(hdcDest)
end sub

end class

--
Dennis in Houston
Nov 21 '05 #1
7 2024
Private Declare Function SelectObject Lib "gdi32" Alias "SelectObje ct"
(ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" Alias "DeleteObje ct"
(ByVal hObject As IntPtr) As Integer
....
Dim hdcBitmap, hdcControl, hdcOriginal As IntPtr
....
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
hdcOriginal = SelectObject(hd cBitmap, v_BackImage.Get Hbitmap)
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmap,
0, 0, &HCC0020)
SelectObject(hd cBitmap, hdcOriginal)
DeleteObject(hd cOriginal)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)
....
gph.Dispose() ' When you're done.
"Dennis" <De****@discuss ions.microsoft. com> schreef in bericht
news:01******** *************** ***********@mic rosoft.com...
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.CreateGraphi cs)
Private gph As Graphics = Graphics.FromIm age(v_BackImage )

'do some drawing on gph

Private Sub Panel_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles MyBase.Paint
'Get Device contexts for Source and Memory Graphics Objects
Dim hdcSrc As IntPtr = gph.GetHdc
Dim hdcDest As IntPtr = e.Graphics.GetH dc
BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width, Me.Height,
13369376)
'Clean Up
gph.ReleaseHdc( hdcSrc)
e.Graphics.Rele aseHdc(hdcDest)
end sub

end class

--
Dennis in Houston

Nov 21 '05 #2
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(h dcOriginal)" 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(h dcOriginal)" then it works fine.

"Qwert" wrote:
Private Declare Function SelectObject Lib "gdi32" Alias "SelectObje ct"
(ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" Alias "DeleteObje ct"
(ByVal hObject As IntPtr) As Integer
....
Dim hdcBitmap, hdcControl, hdcOriginal As IntPtr
....
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
hdcOriginal = SelectObject(hd cBitmap, v_BackImage.Get Hbitmap)
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmap,
0, 0, &HCC0020)
SelectObject(hd cBitmap, hdcOriginal)
DeleteObject(hd cOriginal)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)
....
gph.Dispose() ' When you're done.
"Dennis" <De****@discuss ions.microsoft. com> schreef in bericht
news:01******** *************** ***********@mic rosoft.com...
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.CreateGraphi cs)
Private gph As Graphics = Graphics.FromIm age(v_BackImage )

'do some drawing on gph

Private Sub Panel_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles MyBase.Paint
'Get Device contexts for Source and Memory Graphics Objects
Dim hdcSrc As IntPtr = gph.GetHdc
Dim hdcDest As IntPtr = e.Graphics.GetH dc
BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width, Me.Height,
13369376)
'Clean Up
gph.ReleaseHdc( hdcSrc)
e.Graphics.Rele aseHdc(hdcDest)
end sub

end class

--
Dennis in Houston


Nov 21 '05 #3
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(hd cBitmap, v_BackImage.Get Hbitmap)

and you can remove:

DeleteObject(hd cOriginal)

but you already did that.

Secondly, and more importantly, you need to delete the handle to the bitmap:

Dim hdcBitmapHandle As IntPtr = v_BackImage.Get Hbitmap
....
SelectObject(hd cBitmap, hdcBitmapHandle )
....
DeleteObject(hd cBitmapHandle)

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(h dcBitmap, 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" <De****@discuss ions.microsoft. com> schreef in bericht
news:22******** *************** ***********@mic rosoft.com...
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(h dcOriginal)" 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(h dcOriginal)" then it works fine.

"Qwert" wrote:
Private Declare Function SelectObject Lib "gdi32" Alias "SelectObje ct"
(ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" Alias "DeleteObje ct"
(ByVal hObject As IntPtr) As Integer
....
Dim hdcBitmap, hdcControl, hdcOriginal As IntPtr
....
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
hdcOriginal = SelectObject(hd cBitmap, v_BackImage.Get Hbitmap)
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght,
hdcBitmap,
0, 0, &HCC0020)
SelectObject(hd cBitmap, hdcOriginal)
DeleteObject(hd cOriginal)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)
....
gph.Dispose() ' When you're done.
"Dennis" <De****@discuss ions.microsoft. com> schreef in bericht
news:01******** *************** ***********@mic rosoft.com...
>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.CreateGraphi cs)
> Private gph As Graphics = Graphics.FromIm age(v_BackImage )
>
> 'do some drawing on gph
>
> Private Sub Panel_Paint(ByV al sender As Object, ByVal e As
> System.Windows. Forms.PaintEven tArgs) Handles MyBase.Paint
> 'Get Device contexts for Source and Memory Graphics Objects
> Dim hdcSrc As IntPtr = gph.GetHdc
> Dim hdcDest As IntPtr = e.Graphics.GetH dc
> BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width,
> Me.Height,
> 13369376)
> 'Clean Up
> gph.ReleaseHdc( hdcSrc)
> e.Graphics.Rele aseHdc(hdcDest)
> end sub
>
> end class
>
> --
> Dennis in Houston


Nov 21 '05 #4
> "SelectObject(h dcBitmap, hdcBitmapHandle )" takes up about 85% of the
total...


....I mean 'v_BackImage.Ge tHbitmap' method...
Nov 21 '05 #5
This is what I ended up with..is this correct? Just curious, but what does
the SelctObject statement do? I would have thought the BitBlt statement
would copy the pixels from the bitmap graphics object to the screen graphics
object with the need for further manipulation but I guess I'm wrong.

Dim hdcBitmap, hdcControl As IntPtr
Dim hdcBitmapHandle As IntPtr = v_BackImage.Get Hbitmap 'Takes 85% of time
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmap,
0, 0,
&HCC0020)
SelectObject(hd cBitmap, hdcBitmapHandle )
DeleteObject(hd cBitmapHandle)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)

"Qwert" wrote:
"SelectObject(h dcBitmap, hdcBitmapHandle )" takes up about 85% of the
total...


....I mean 'v_BackImage.Ge tHbitmap' method...

Nov 21 '05 #6
BitBlt copies the graphic data to a Device Context. A Device Context is a
data structure maintained by GDI. A device context is associated with a
particular display device, such as a printer or video display. For a video
display, a device context is ussually associated with a particular window on
the display, but it can also be an offscreen display.
What you do, is create a Device Context and then associated a Bitmap with
the Device Context ( with SelectObject ). Then you draw to the Device
Context ( using BitBlt ) to alter the Bitmap. You can't use GDI functions to
directly draw to a bitmap.
After using a Device Context, you need to clean stuff up ( release and
delete ). Or else the device context stays locked and other attempts to draw
again will fail. And not cleaning up will result in memory leaks.

I hope this is correct. If not, I am sure another reader of this newsgroup
will be kind enough to correct it.
Dim hdcBitmapHandle , hdcBitmapDC, hdcControl As IntPtr
REM Get the handle to the device context associated with form Graphics
object.
hdcControl = e.Graphics.GetH dc
REM Get the handle to the device context associated with bitmap Graphics
object.
hdcBitmapDC = gph.GetHdc
REM Create the Windows HBITMAP from the image. You must de-allocate the
HBITMAP with Windows.DeleteO bject(handle).
hdcBitmapHandle = v_BackImage.Get Hbitmap
REM Link Device Context to Bitmap handle.
SelectObject( hdcBitmapDC , hdcBitmapHandle )
REM Use BitBlt to copy bits from one Device Context to another.
REM The first DeviceContext is attached to the form, the second Device
Context is attached to the bitmap.
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmapDC,
0, 0,
&HCC0020)
REM In general, when you clean things up, you do it in opposite order they
where created.
DeleteObject(hd cBitmapHandle)
e.Graphics.Rele aseHdc(hdcBitma pDC )
e.Graphics.Rele aseHdc(hdcContr ol)
This is what I ended up with..is this correct? Just curious, but what
does
the SelctObject statement do? I would have thought the BitBlt statement
would copy the pixels from the bitmap graphics object to the screen
graphics
object with the need for further manipulation but I guess I'm wrong.

Dim hdcBitmap, hdcControl As IntPtr
Dim hdcBitmapHandle As IntPtr = v_BackImage.Get Hbitmap 'Takes 85% of time
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmap,
0, 0,
&HCC0020)
SelectObject(hd cBitmap, hdcBitmapHandle )
DeleteObject(hd cBitmapHandle)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)

"Qwert" wrote:
> "SelectObject(h dcBitmap, hdcBitmapHandle )" takes up about 85% of the
> total...


....I mean 'v_BackImage.Ge tHbitmap' method...

Nov 21 '05 #7
Thanks a lot for the explaination. It was the SelectObject statement after
the BitBlt that was confusing me. I see now that it goes before the BitBlt.

"Qwert" wrote:
BitBlt copies the graphic data to a Device Context. A Device Context is a
data structure maintained by GDI. A device context is associated with a
particular display device, such as a printer or video display. For a video
display, a device context is ussually associated with a particular window on
the display, but it can also be an offscreen display.
What you do, is create a Device Context and then associated a Bitmap with
the Device Context ( with SelectObject ). Then you draw to the Device
Context ( using BitBlt ) to alter the Bitmap. You can't use GDI functions to
directly draw to a bitmap.
After using a Device Context, you need to clean stuff up ( release and
delete ). Or else the device context stays locked and other attempts to draw
again will fail. And not cleaning up will result in memory leaks.

I hope this is correct. If not, I am sure another reader of this newsgroup
will be kind enough to correct it.
Dim hdcBitmapHandle , hdcBitmapDC, hdcControl As IntPtr
REM Get the handle to the device context associated with form Graphics
object.
hdcControl = e.Graphics.GetH dc
REM Get the handle to the device context associated with bitmap Graphics
object.
hdcBitmapDC = gph.GetHdc
REM Create the Windows HBITMAP from the image. You must de-allocate the
HBITMAP with Windows.DeleteO bject(handle).
hdcBitmapHandle = v_BackImage.Get Hbitmap
REM Link Device Context to Bitmap handle.
SelectObject( hdcBitmapDC , hdcBitmapHandle )
REM Use BitBlt to copy bits from one Device Context to another.
REM The first DeviceContext is attached to the form, the second Device
Context is attached to the bitmap.
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmapDC,
0, 0,
&HCC0020)
REM In general, when you clean things up, you do it in opposite order they
where created.
DeleteObject(hd cBitmapHandle)
e.Graphics.Rele aseHdc(hdcBitma pDC )
e.Graphics.Rele aseHdc(hdcContr ol)
This is what I ended up with..is this correct? Just curious, but what
does
the SelctObject statement do? I would have thought the BitBlt statement
would copy the pixels from the bitmap graphics object to the screen
graphics
object with the need for further manipulation but I guess I'm wrong.

Dim hdcBitmap, hdcControl As IntPtr
Dim hdcBitmapHandle As IntPtr = v_BackImage.Get Hbitmap 'Takes 85% of time
hdcBitmap = gph.GetHdc
hdcControl = e.Graphics.GetH dc
BitBlt(hdcContr ol, 0, 0, v_BackImage.Wid th, v_BackImage.Hei ght, hdcBitmap,
0, 0,
&HCC0020)
SelectObject(hd cBitmap, hdcBitmapHandle )
DeleteObject(hd cBitmapHandle)
e.Graphics.Rele aseHdc(hdcContr ol)
gph.ReleaseHdc( hdcBitmap)

"Qwert" wrote:
> "SelectObject(h dcBitmap, hdcBitmapHandle )" takes up about 85% of the
> total...

....I mean 'v_BackImage.Ge tHbitmap' method...


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
31111
by: Bradford | last post by:
I have an Windows application that displays lots of different colored lines in a Panel by using the Graphics.DrawLine method. The location and length of each line comes from a database query that takes two minutes to complete. I am using the Panel's Paint event to call my drawing method, which accepts PaintEventArgs as a parameter. The problem is that every time the Window is moved or hidden, it needs to be repainted, which takes a long time...
3
8998
by: pacemkr | last post by:
Is it possible to force a control to paint to a Graphics object (or Device Context, or a bitmap, anywhere aside from the form) that I provide. I am writing a windows form class that supports full alpha blending. Basically I'm trying to make a layered window that supports controls. In order to do that I need to redirect painting of the controls to a memory DC (which is then sent to the layered window). All this asuming that I figured out...
5
2011
by: active | last post by:
This is what I do in a PictureBox New: b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics()) g1 = Graphics.FromImage(b1) Someplace I do g1.DrawString.........
2
2167
by: active | last post by:
Problem: The PictureBox display appears to have the image cut off. I.e., the image bottom does not display although the PictureBox has room for it. It occurred to me that what was displayed was the same size as the PictureBox was before it was resized. So I figured the bitmap was too small and tried to increase it's size as shown below. Two things: First it didn't fix the problem. Secondly, I'm not sure I've done it correctly and...
6
3163
by: jcrouse | last post by:
I am rotating some text is some label controls. In the one place I use it it works fine. In the other place I use it I can't figure out the syntax. I don't really understand the event. Where it works fine, it seems to fire when the form changes visibility. Here is the code. Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint If lblP1JoyUp.Visible = True Then Dim...
15
1852
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
1
5599
by: martinsmith160 | last post by:
Hi all I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is working fine. My aim is to double click the picture box and the desired image will be drawn at the mouse position. This works fine unless I scroll or minimise the form because the image isnt repainted after movement. I looked up drawing the image to...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.