PDA

View Full Version : Get Pixel Color from a Picture



zak
09-03-2011, 09:37
there is a Dialog_ChooseColor.tbasic example in thinbasic examples but we may need to pick a pixel color from a picture, the following example get the mouse click coordinates on a form, then get pixel color of that pixel, then displaying the mouse click coordinates, color values, and the color.
i have used the major codes from Petr examples i listed below in the references.


Uses "UI"

Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SetPixelV Lib "gdi32" Alias "SetPixelV" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

' -- ID numbers of controls
Begin Const
%bClose = 1000
%canv
%canv2
%txtbox
%txtbox2
%LABEL1
End Const
Global hDlg, hDC_canvas, colrVal As Long

' -- Create dialog here
Function TBMain()
Local sImageBackground As String = APP_SourcePath+"ColorGradient.bmp"
Local nWidth, nHeight As Long

Canvas_BitmapGetFileInfo(sImageBackground, nWidth, nHeight)

'Local hDlg As DWord
Dialog New Pixels,0,"Mouse Click Pos + Pixel color",0,0,600,400,%WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_CAPTION To hDlg
' -- Place controls here
Control Add Canvas, hDlg, %canv, "", 0, 0, nWidth, nHeight
Control Add Canvas, hDlg, %canv2, "", 450, 30, 50, 50
Control Add Button, hDlg, %bClose, "Click to close", 532, 340, 80, 30, Call bCloseProc
Control Add Textbox , hDlg, %txtbox, " " ,535 ,30 ,60 ,30 , _
%WS_BORDER Or _
%WS_TABSTOP Or _
%ES_WANTRETURN

Control Add Textbox , hDlg, %txtbox2, " " ,535 ,110 ,60 ,30 , _
%WS_BORDER Or _
%WS_TABSTOP Or _
%ES_WANTRETURN
Control Add Label , hDlg, %LABEL1, "RGB" , 535,80, 60, 30, %WS_CHILD Or %WS_VISIBLE Or %WS_BORDER Or %SS_CENTERIMAGE Or %SS_CENTER
Canvas_Attach(hDlg, %canv, TRUE)
' -- Make sure coordinate system is per pixel
Canvas_Scale Pixels
' -- Render image
Canvas_BitmapRender(sImageBackground)
Canvas_Redraw

hDC_canvas = Canvas_GetDC()

Circle(200,200,10,Rgb(0, 0, 0),Rgb(255, 0, 0))
Circle(250,200,10,Rgb(0, 0, 0),Rgb(0, 255, 0))
Circle(320,200,10,Rgb(0, 0, 0),Rgb(0, 0, 255))
Dialog Show Modal hDlg, Call dlgProc

End Function

' -- Callback for dialog
CallBack Function dlgProc()
Dim mousePosition As POINTAPI
Dim locX, locY As Long

' -- Test for messages
Select Case CBMSG
Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
Canvas_Attach(CBHNDL, %canv, %FALSE)
Canvas_Scale Pixels
Case %WM_LBUTTONDOWN 'when clicking left mouse button
' -- Get mouse position, convert it to canvas local coordinates
Control Get Loc CBHNDL, %canv To locX, locY
Win_GetCursorPos(mousePosition)
Win_ScreenToClient(CBHNDL, mousePosition)
mousePosition.x -= locX
mousePosition.y -= locY
Control Set Text CBHNDL, %txtbox, Str$(mousePosition.x)+", "+Str$(mousePosition.y)
colrVal = GetPixel(hDC_canvas, mousePosition.x, mousePosition.y)
Control Set Text CBHNDL, %txtbox2, StrReverse$(Hex$(colrVal, 6))
Canvas_Attach(CBHNDL, %canv2, %FALSE)
Canvas_Clear(colrVal)
'Canvas_Scale Pixels
'Circle(25,25,20,Rgb(0, 0, 0),colrVal)

Case %WM_CLOSE
' -- Put code to be executed before dialog end here

End Select

End Function

' -- Callback for close button
CallBack Function bCloseProc()

If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
' -- Closes the dialog
Dialog End CBHNDL
End If
End If

End Function

Function Circle(x As Long, y As Long, r As Long, colr, colrFill)
Canvas_Ellipse(x-r, y-r, x+r, y+r, colr, colrFill)
End Function


http://i54.tinypic.com/t0hr1d.png

there is a thinBasic_GDIp.dll with a good examples in C:\thinBasic\SampleScripts\GDIp , i wonder if it is a smaller version of gdi32.dll wich i have used to declare GetPixel .


References:
1- draw line canvas:
http://www.thinbasic.com/community/showthread.php?10133-draw-paint-Demo-(canvas)-)

2- DrawingMaskedImage.zip‎ :
http://www.thinbasic.com/community/showthread.php?11037-creating-a-shape

3- background1B.zip‎ :
http://www.thinbasic.com/community/showthread.php?11037-creating-a-shape/page2

Petr Schreiber
09-03-2011, 12:41
Hi Zak,

thanks a lot for the example, its looking good (just maybe the Close button is a bit shy ;))!

I think GDIP module is not built around GDI32, but GDIPlus (http://msdn.microsoft.com/en-us/library/ms533798%28v=vs.85%29.aspx) system DLL.


Petr

ErosOlmi
09-03-2011, 12:53
Hi zak, thanks a lot for this nice example.

thinBasic_GDIp.dll is a starting module based on GDI Plus that will be implemented in future releases of thinBasic when we will abandon support for Win9x operating systems.

ErosOlmi
09-03-2011, 17:07
Added the following function to next update (1.8.7)

Win_GetPixel
Canvas_Circle


If anything else is needed, please place a feature request in Support (http://www.thinbasic.com/community/project.php) area

Thanks
Eros