BMAX Desktop pixel auslesen

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Trust

Betreff: BMAX Desktop pixel auslesen

BeitragSa, Sep 09, 2017 19:36
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo,

der Originalcode ist von Suco-X, ich habe nur die kleine Funktion ToRGB() hinzugefügt und ein Beispiel wie man einen Pixel ausliest.
Nichts besonderes - aber so steht mal alles zusammen.

BlitzMax: [AUSKLAPPEN]
Rem
DesktopPixmap function by Suco-X
End Rem


Strict


Function GetDesktopPixmap:TPixmap()

Extern "Win32"
Function GetDIBits(hdc:Int, bitmap:Int, Start:Int, Num:Int, bits:Byte Ptr, lpbi:Byte Ptr, usage:Int)
Function CreateCompatibleBitmap(hdc:Int, Width:Int, Height:Int)
Function CreateDIBSection(hdc:Int, pbmi:Byte Ptr, usage:Int, Bits:Byte Ptr, hSection:Int, Offset:Int)
Function SelectObject(hdc:Int, obj:Int)
Function CreateCompatibleDC(hdc:Int)
Function GetDesktopWindow()
Function GetWindowDC(hwnd:Int)
Function GetDeviceCaps(hdc:Int, index:Int)
Function DeleteDC(hdc:Int)
Function DeleteObject(obj:Int)
Function ReleaseDC(hwdn:Int, hdc:Int)
Function BitBlt(hdc,x,y,w,h,src_dc,src_x,src_y,dwrop)
End Extern


?Win32

Type BITMAPINFO
Field biSize:Int
Field biWidth:Int
Field biHeight:Int
Field biPlanes:Short
Field biBitCount:Short
Field biCompression:Int
Field biSizeImage:Int
Field biXPelsPerMeter:Int
Field biYPelsPerMeter:Int
Field biClrUsed:Int
Field biClrImportant:Int

Field R:Byte
Field G:Byte
Field B:Byte
Field Res:Byte
End Type


Const HORZRES:Int = 8
Const VERTRES:Int = 10

Local HwndDesktop:Int
Local hdcDesktop:Int
Local hdcMem:Int
Local DesktopWidth:Int
Local DesktopHeight:Int
Local bmpMem:Int
Local INFO:BITMAPINFO
Local FinalPixmap:TPixmap

HwndDesktop = GetDesktopWindow()

If Not HwndDesktop
Return Null
EndIf


hdcDesktop = GetWindowDC(HwndDesktop)

If Not HdcDesktop
Return Null
EndIf


hdcMem = CreateCompatibleDC(hdcDesktop)

If Not HdcMem
Return Null
EndIf


DesktopWidth = GetDeviceCaps(hdcDesktop, HORZRES)
DesktopHeight = GetDeviceCaps(hdcDesktop, VERTRES)

If DesktopWidth = 0 Or DesktopHeight = 0
Return Null
EndIf


bmpMem = CreateCompatibleBitmap(hdcDesktop, DesktopWidth, DesktopHeight)

If Not BmpMem
Return Null
EndIf


If Not SelectObject(HdcMem, bmpMem)
Return Null
EndIf

Info = New BITMAPINFO
Info.bisize = SizeOf(INFO)
info.BiWidth = DesktopWidth
Info.biHeight = DesktopHeight
Info.biPlanes = 1
info.biBitCount = 24
Info.biCompression = 0


If Not BitBlt(hdcMem,0,0,Info.biWidth,Info.biHeight, hdcDesktop,0,0,ROP_SRCCOPY)
Return Null
EndIf

FinalPixmap = CreatePixmap(info.biWidth, info.biHeight, PF_BGR888)

If Not GetDIBits(hdcMem, bmpMem, 0, Info.biHeight, FinalPixmap.PixelPtr(0,0), info, 0)
Return Null
EndIf

FinalPixmap = YFlipPixmap(FinalPixmap)

DeleteDC(HdcMem)
DeleteObject(bmpMem)
ReleaseDC(hwndDesktop, hdcDesktop)

Return FinalPixmap

?

?Linux
Return Null
?

?MacOs
Return Null
?
End Function


' Converts a 32 bit pixel value into r, g, b, values
Function ToRgb(_32BitPixelValue:Int, r:Byte Var, g:Byte Var, b:Byte Var)
Local rgb:Int = _32BitPixelValue
' a:Byte = (rgb & $FF000000) / $1000000
r = (rgb & $FF0000) / $10000
g = (rgb & $FF00) / $100
b = rgb & $FF
End Function




' ############ Example ##############


' - taking a screenshot
' - reading a pixel at a given position
' - converting 32 bit pixel into r, g ,b values
' - drawing screenshot




Local Time:Int = MilliSecs()

Local desktopScreen:TPixmap = GetDesktopPixmap()

Local pixel:Int = ReadPixel(desktopScreen, 10, 10)

Local r:Byte ,g:Byte ,b:Byte

ToRgb(pixel, r, g, b)

Print r + " " + g + " " + b

Time = MilliSecs()-Time

Print ""
Print "PixmapsPerSecond: "+(1000/Time)
Print ""

If DesktopScreen = Null
Print "TEST FEHLGESCHLAGEN"
EndIf


SetGraphicsDriver(GLMax2DDriver())

Graphics 1024, 768,0

DrawPixmap DesktopScreen,0,0
Flip
WaitKey()


G,
Trust
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group