How to get an images device context?

BlitzPlus Forums/BlitzPlus Programming/How to get an images device context?

REDi(Posted 2004) [#1]
Does anyone know how to get the DC of an image?

I thought maybe ImageBuffer() might pass it, but it seems it doesn't :(
Any ideas?


soja(Posted 2004) [#2]
Yes, it's as simple as calling GetDC from User32. =) I've done it successfully in my projects.

; .lib "user32.dll"
; GetDC%(hwnd%):"GetDC"
; ReleaseDC%(hwnd%, hdc%):"ReleaseDC"

dc% = GetDC(QueryObject(gadget,1))
If Not dc Then DebugLog "Error getting DC"
If Not ReleaseDC(QueryObject(gadget,1), dc) Then DebugLog "Error releasing DC"


<EDIT> Oh, incidentally I just noticed that you said "device context of image"... well, I suppose you mean the gadget/window that the image is drawn to, which is known by Blitz (assuming it's using GDI?)

So maybe this isn't the answer you're looking for. I don't think Blitz will give it to you anyway. Still you CAN get another DC to the same space if you want to do your own painting.


REDi(Posted 2004) [#3]
Really I'm after drawing to an image using the Gdi32 functions like ExtFloodFill ect, I've managed it okay with purebasic because it allows you to get the DC, but i can't figure it out in blitz.

I didn't know that you can create new DCs :) I'm new to all this windows stuff, I'll have to look into that.

Thanx soja


soja(Posted 2004) [#4]
Oh yeah, in that case, that's what you do.

1) GetDC
2) Call GDI drawing functions
3) Release DC
4) Repeat when you need to

For example, in order to fill the background of a window with a certain pattern, here's what I did/used:

1) CreateBitmap (Creates a bitmap)
2) CreatePatternBrush (creates a brush with the bitmap's pattern)
3) GetDC (get a device context to window)
4) GetSysColor (gets color of window background)
5) SetBkColor (make sure the brush is the right color)
6) FillRect (paint the brush onto the window background)
7) ReleaseDC (release the device context)

I do steps 3-7 60 times a second with almost no CPU usage.


REDi(Posted 2004) [#5]
???

ImageID = CreateImage(256,256)
Debuglog GetDC(ImageID) ; returns 0 :(

GetDC works okay for windows ect but not on images, am i missing something here?

Purebasic allows this...

CreateImage(1,256,256)
UseImage(1)
OutPut = ImageOutput()
DC = StartDrawing(OutPut)
Debug Str(DC) ; shows the DC for the image
EXTFloodFill_(DC,10,10,Point(10,10),1)
StopDrawing()

Sorry to be a pain, but can this be done in blitz? Thx


Snarty(Posted 2004) [#6]
You may be able to get the DC of a Canvas rather than the image. I'm not 100% sure on this, but it's my theory.


soja(Posted 2004) [#7]
Yes, I think that PureBasic must be doing something underneath, because AFAIK you can only get device contexts for gadgets/windows. Try getting the a DC to the canvas like Snarty says, but make sure you use QueryObject(gadget,1) as the argument to GetDC.


REDi(Posted 2004) [#8]
Snarty your theory is fact! :D it does work well with a canvas.

But i still can't use it because i need it to work with Blitz3D as well as BlitzPlus :(

Well i'll either have to write a fast flood fill algorithm or just use pure for this project, lets hope BlitzMax covers this when we get it;)

Thanx all


<Edit>
I've just realized that when the window is repainted any changes you made to canvas through the DC are lost, it seems to draw to the gadget and not to the canvas buffer :(


soja(Posted 2004) [#9]
...which is why it needs to be updated by you. I have a loop that updates my stuff 60 times per second. To minimize the CPU hit, I tried peeking into Blitz' message queue to find messages indicating that I would need to repaint the gadget, but I wasn't able to make anything work. Unfortunately.

Anyway, mixing GDI and Blitz is a pain. Yes, it sure would be nicer if Blitz "supported" more...


Snarty(Posted 2004) [#10]
Thats good to know Cliff, handy for Blitz+ as you said, but, starts becoming a pain for Blitz3D.

Blitz Flood Fill

soja, I agree with WinAPI support.

At least I can have a play with canvas DC's now :)


REDi(Posted 2004) [#11]
@Snarty

Did you read my edit at the bottom?... :(

Thanx for the link m8, nice work :D