News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

GetPixel question

Started by georgek01, April 04, 2007, 03:45:24 PM

Previous topic - Next topic

georgek01

Hello,

I would like to get the pixel colour from a bitmap image. When I open the file, can I select it into a DC?



invoke CreateFile,addr szUserPath,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov hFile,eax
.if hFile != INVALID_HANDLE_VALUE
invoke CreateCompatibleDC,NULL
mov hDC,eax
invoke SelectObject,hDC,hFile
mov hOldDC,eax
.if (eax != NULL) || (eax != GDI_ERROR)
invoke GetPixel,hDC,1,1
.if eax != CLR_INVALID
mov aColor[0],eax
mov eax,COLOR_DESKTOP
mov aSystem[0],eax
invoke SetSysColors,1,addr aSystem,addr aColor
.if eax == 0
; error ...
.endif
.endif
.else
; error...
.endif
invoke SelectObject,hDC,hOldDC
invoke DeleteDC,hDC
invoke CloseHandle,hFile
.else
; error...
.endif

What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

georgek01

It's so nice to be able to provide myself with the solution...

Using LoadImage instead of CreateFile did the trick.


invoke LoadImage,NULL,addr szUserPath,IMAGE_BITMAP,5,5,LR_LOADFROMFILE
mov hFile,eax
.if hFile != INVALID_HANDLE_VALUE
invoke CreateCompatibleDC,NULL
mov hDC,eax
invoke SelectObject,hDC,hFile
mov hOldDC,eax
.if (eax != NULL) || (eax != GDI_ERROR)
invoke GetPixel,hDC,1,1
.if eax != CLR_INVALID
mov aColor[0],eax
mov eax,COLOR_DESKTOP
mov aSystem[0],eax
invoke SetSysColors,1,addr aSystem,addr aColor
.if eax == 0
; error ...
.endif
.endif
.else
; error...
.endif
invoke SelectObject,hDC,hOldDC
invoke DeleteDC,hDC
invoke CloseHandle,hFile
.else
; error...
.endif

What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

zooba

If you're planning to read more than a couple of pixels (which from the code you're not, but just in case) you will want to consider using the GetDIBits function. This will copy the colour values of each pixel into an array. Using this array is much more efficient than using GetPixel (which IIRC includes a transition to kernel mode).

Also, keep in mind that without LR_CREATEDIBSECTION in your LoadImage call, the actual colours may change depending on the display. 24-bits is very common these days, but 8-bit or 16-bit displays may map some colours to displayable ones. If your code requires the colour stored in the file (as opposed to the colour actually displayed), use LR_CREATEDIBSECTION.

Cheers,

Zooba :U

georgek01

Zooba, thanks for the information!!
What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)