Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API graphics beginner 1

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
0
0
US
I am a beginner in using API functions to do graphics.
I tried this simple program which loads a BMP into Picture1 and then copies part of the image to Picture2.
(I realize there is a simpler way to do this but I want to learn to use the API calls.)

Option Explicit
Dim Result As Long
Dim hCC As Long

Private Sub cmdLoadPic_Click()
Picture1.Picture = LoadPicture("midcity.bmp")
hCC = CreateCompatibleDC(Picture1.hdc)
Result = SelectObject(hCC, Picture1.hdc)
MsgBox "hCC = " & hCC & " SelectObject returned " & Result, vbOKOnly
' Result is now zero
End Sub

Private Sub cmdBitBlt_Click()
Result = BitBlt(Picture2.hdc, 0, 0, 40, 40, _
hCC, 10, 10, SRCCOPY)
End Sub

hCC has a positive value, but Result is 0.
What is the sequence of API calls needed to accomplish this task?
 
Hi,

to copy a part of the bitmap, you don't need to do so much work ;)

Option Explicit

Private Sub cmdLoadPic_Click()
Picture1.Picture = LoadPicture("midcity.bmp")
End Sub

Private Sub cmdBitBlt_Click()
BitBlt Picture2.hDC, 0, 0, 40, 40, _
Picture1.hDC, 10, 10, SRCCOPY
End Sub

That's all

Bye

LauDse
 
Yes, that works great when
(1) all of the image in Picture1 is showing on the screen
(2) Picture2 is not overlapping Picture1.

But I want Picture1 to be a large (1792 x 1100, say) image which won't fit on the screen. Also, I want Picture2 to be a scrollable viewport into the contents of Picture1. Picture2 may even sit on top of Picture1. In these cases, BitBlt bithely blits bogus bits into Picture2, including whatever surrounds/covers the source.
I believe I can do this only by using a Device Context. Am I wrong about this?
 
Hi GPerk,

ok i've got it.
Replace your "SelectObject" statement with:

Result = SelectObject(hCC, Picture1.Picture.Handle)

You tried to select a DC into a DC, that's not possible;)
Picture1.Picture.Handle is the handle to the bitmap,
this can be selected into a decice context!

Bye
LauDse
 
LauDse, You have saved me from giving up on VB in frustration!
This code works:

Picture1.Picture = LoadPicture("c:\Table.jpg")
MsgBox " ", vbOKOnly
hDCsrc = CreateCompatibleDC(0&)
hDCsrcOld = SelectObject(hDCsrc, Picture1.Picture.Handle)

Picture2.Cls
Result = BitBlt(Picture2.hdc, 0, 0, 200, 100, _
hDCsrc, 400, 300, SRCCOPY)

(BTW, if I remove the MsgBox the image never shows in Picture1)
But where did "Picture1.Picture.Handle" come from?
I have studied numerous books on Win32 API and on Graphics Programming. The only hint that this property existed was in Hardcore Visual Basic by Bruce McKinney. On page 409 he has this statement:
hdcSrcOld=SelectObject(hdcSrc,picSrc.Handle)
which does NOT work.
So, how did you come by this knowledge?
Thanks a million!!!

Gene
 
It is documented in the VB help files. Sadly, you essentially need to know what you are looking for before you can effectively find it.

Try the following, for example: type 'picture' in the Immediate window, highlight it and hit F1, then click on the 'See Also' option in the resulting help window, scroll right down to the bottom and select 'StdPicture Object'

Alternatively, use the Object Browser which - unlike the Help file - at least shows you that a PictureBoxes Picture property is in fact an object itself
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top