Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching color from images
#1
Hello! I searched QM manual and forum but didn't find solution to my small and trivial problem.

1) I am using CaptureImageOnScreen function to save my screen image to bmp file (2 times per second) and the size of the file is ~6MB. Is it possible to save directly to some other format so that it would not take so much space? png?
2) Now when I have saved the images to different files - is it possible to recognize the color of certain coordinates of that file without opening the files in viewer? All commands and functions like pixel/FindColor/scan are for opened screen recognizing...
#2
Examples with GflAx component.
Image manipulation
Need it to save/load png.

Macro capture image and save in png format
Code:
Copy      Help
;file
str sFile="$desktop$\qm test png.png"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.SetPicture(BitmapHandleToIPicture(hb))
g.SaveFormat=GflAx.AX_PNG ;;use other constants to save in other formats
g.SaveBitmap(_s.expandpath(sFile))

;________________________

;show the image
run sFile

Macro get color from png file
Code:
Copy      Help
;file, pixel
str sFile="$desktop$\qm test png.png"
int x(8) y(100)
;int x(xm) y(ym) ;;from mouse

;________________________

;open file
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.LoadBitmap(_s.expandpath(sFile))

;get pixel color
int color=g.GetColorAt(x y)
out "0x%06X" color

Macro find color or image in png file
Code:
Copy      Help
;file, pixel
str sFile="$desktop$\qm test png.png"
;int color=0xFFE0C0
int color=pixel(xm ym) ;;color from mouse

;________________________

;open file
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.LoadBitmap(_s.expandpath(sFile))

;get bitmap handle
IPicture p=g.GetPicture
int hb; p.get_Handle(hb)

;find color in bitmap (in memory)
RECT r
if scan(F"color:{color}" hb r 0x280)
,out "color 0x%06X found at x=%i y=%i" color r.left r.top
else out "not found"

;to find image: if scan("file.bmp" hb r 0x280)

Function BitmapHandleToIPicture
Code:
Copy      Help
;/
function'IPicture hBitmap

;Creates IPicture (OLE picture object) from Windows bitmap handle.


IPicture ip
PICTDESC pd.cbSizeofstruct=sizeof(pd)
pd.picType=PICTYPE_BITMAP
pd.bmp.hbitmap=hBitmap

OleCreatePictureIndirect(&pd uuidof(IPicture) 0 &ip)
ret ip
#3
Examples with GDI+.
Does not require external components, just import GDI+.qml from GDI+
Need it to save/load png.

Macro capture image and save in png format, using GDI+
Code:
Copy      Help
;file
str sFile="$desktop$\qm test png gdip.png"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
#compile "__Gdip"
GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile


;NOTES
;GDI+ may be unavailable on Windows 2000.
;Png file created with GDI+ is 33% bigger than with GflAx.

Macro get color from png file, using GDI+
Code:
Copy      Help
;file, pixel
str sFile="$desktop$\qm test png gdip.png"
;int x(8) y(100)
int x(xm) y(ym) ;;from mouse

;________________________

;open file
#compile "__Gdip"
GdipBitmap im
if(!im.FromFile(sFile)) end "error"

;get pixel color
int color
GDIP.GdipBitmapGetPixel(+im x y &color)
color=ColorToARGB(color 0) ;;0xAARRGGBB to 0xBBGGRR
out "0x%06X" color

Macro find color or image in png file, using GDI+
Code:
Copy      Help
;file, pixel
str sFile="$desktop$\qm test png.png"
;int color=0xFFE0C0
int color=pixel(xm ym) ;;color from mouse

;________________________

;open file
#compile "__Gdip"
GdipBitmap im
if(!im.FromFile(sFile)) end "error"

;get bitmap handle
__GdiHandle hb=im.GetHBITMAP

;find color in bitmap (in memory)
RECT r
if scan(F"color:{color}" hb r 0x280)
,out "color 0x%06X found at x=%i y=%i" color r.left r.top
else out "not found"

;to find image: if scan("file.bmp" hb r 0x280)

Member function GdipBitmap.FromHBITMAP
Code:
Copy      Help
function'GDIP.GpBitmap* hBitmap [hPalette]

;Create this bitmap from GDI bitmap.


if(!GdipInit) ret
Delete

_hresult=GDIP.GdipCreateBitmapFromHBITMAP(hBitmap hPalette +&m_i)
ret +m_i
#4
Is it possible to modify the below example to get the clipboard contents which is bitmap and then convert it to JPG?

Macro Macro16
Code:
Copy      Help
;file
str sFile="$desktop$\qm test jpg gdip.jpg"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
#compile "__Gdip"



GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile
#5
Macro Macro2900
Code:
Copy      Help
;file
str sFile="$desktop$\qm test jpg gdip.jpg"

;________________________

;get clipboard bitmap
if(!OpenClipboard(0)) end "failed" ;;TODO: wait until succeeds
_i=GetClipboardData(CF_BITMAP)
__GdiHandle hb; if(_i) hb=CopyImage(_i 0 0 0 LR_CREATEDIBSECTION)
CloseClipboard
if(_i=0) end "no bitmap in clipboard"

;save as jpg
#compile "__Gdip"



GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile
#6
Thank you!


Forum Jump:


Users browsing this thread: 1 Guest(s)