March 28, 2024, 07:54:24 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


save window contents as JPG

Started by TonyMUK, June 21, 2010, 04:14:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TonyMUK

I have been struggling with this for a week now. I have tried gdi+ and can't get it to work. All I want to do is save the contents of a window, within my program, to a jpg. If anyone has code I can copy or a dll/api that will do the job I would be eternally grateful.

Thanks

TonyM
Check out www.arnoldchiari.co.uk. If you don't know what Arnold Chiari is you are very lucky.

Ficko

June 22, 2010, 02:02:05 AM #1 Last Edit: June 22, 2010, 03:13:56 AM by Ficko
i_view32.exe /capture=0

Capture the whole screen.

;D

EDIT

capture values:

0 = whole screen
1 = current monitor, where mouse is located
2 = foreground window
3 = foreground window - client area
4 = rectangle selection
5 = object selected with the mouse
6 = start in capture mode (can't be combined with other commandline options)


pistol350

Hi Tony,
you've probably already found the answer to your question.
But just in case, i think that you might find this program very interesting.
It's from an old member of our community.


'----------------------------------------------- 
'Capture color and Window by Mary Nicolas 
'Version 2.0 
'-----------------------------------------------
'Converted to pro 25/Jul 2005. Jpow
'Version 2.01
'-----------------------------------------------
$MAIN
AutoDefine "Off"
Def Version:String:Version="2.01"
$include "Api\\allapi.inc"

TYPE BITMAP
DEF bmType:INT
DEF bmWidth:INT
DEF bmHeight:INT
DEF bmWidthCHARs:INT
DEF bmPlanes:WORD
DEF bmBitsPixel:WORD
DEF bmBits:INT
ENDTYPE

TYPE BITMAPINFOHEADER
DEF biSize:INT
DEF biWidth:INT
DEF biHeight:INT
DEF biPlanes:WORD
DEF biBitCount:WORD
DEF biCompression:INT
DEF biSizeImage:INT
DEF biXPelsPerMeter:INT
DEF biYPelsPerMeter:INT
DEF biClrUsed:INT
DEF biClrImportant:INT
ENDTYPE

TYPE BITMAPFILEHEADER,1
DEF bfType:WORD
DEF bfSize:INT
DEF bfReserved1:WORD
DEF bfReserved2:WORD
DEF bfOffBits:INT
ENDTYPE

DECLARE SaveBitmap(filename:string,win:window,l:int,t:int,w:int,h:int)
DECLARE CreateInfoStructure(win:WINDOW,hbitmap:int,info:BITMAPINFOHEADER),INT   

DEF filename,filter:STRING   
SETID "TRUE",1 
SETID "RGN_XOR", 3 
SETID "WM_NCLBUTTONDOWN",0xA1   
SETID "HTCAPTION",2   
DEF Region1,Region2:INT 
DEF RegionWin:WINDOW 
def PosX1,PosY1,PosX2,PosY2,a:int 
'-------------------------------------------------- 
DEF col,r,g,b,x2,y2,capture,HwndPoint,handleHdc,key,hdlcursor,cursor:INT   
DEF w:WINDOW 
DEF win,view,ViewFrame:WINDOW 
DEF resultatS,hdcScreen,hwndscreen,Hdlebitmap,HmemDC,run:int   
DEF ref,hr,hb,hg:STRING   
DEF handle1,a1,b1:INT   
'SETID "WM_GETMINMAXINFO", 0x24 
SETID "CF_BITMAP", 2 
SETID "SRCCOPY", 0xCC0020
 
TYPE POINTAPI   
   DEF X:INT   
   DEF Y:INT   
ENDTYPE   
TYPE RECTA   
   DEF Left:INT   
   DEF Top:INT   
   DEF Right:INT   
   DEF Bottom:INT   
ENDTYPE   
DECLARE "user32",GetWindowRect(hwnd:INT, lpRect:RECTA),INT 
DECLARE "gdi32",MyGetPixel ALIAS GetPixel(hdc:INT, x:INT, y:INT),INT
   
DEF position:POINTAPI   
DEF dimens:RECTA 
'--------------------selected------------------------ 
OpenWindow RegionWin,0,0,12,12,@NOCAPTION,0," ",&RegionWinProc 
SETWINDOWCOLOR RegionWin,RGB(255,0,0) 
SHOWWINDOW RegionWin, @SWHIDE   
'------------------------------------------------------- 
DEF background,test,number,Choice:INT 
DEF Width,Height:INT 
DEF varL,varT,varW,varH,varW1,varH1:INT 
OpenWindow win,0,0,430,220,@caption|@minbox,0,"Capture Color and Window",&main 
OpenWindow w,180,20,63,63,@nocaption|@NOAUTODRAW,win,"Color Reference",&colref 
OpenWindow View,0,0,200,200,@NOAUTODRAW|@CAPTION|@MINBOX|@MAXBOX,0,"View Image",&subview 
BeginMenu View
MenuTitle "&Save"
MenuItem "Bitmap",0,1 
EndMenu
SHOWWINDOW View, @SWHIDE   
CONTROL win,@sysButton,"Color",50,140,60,20,0,1
CONTROL win,@sysButton,"Capture",300,140,60,20,0,2
CONTROL win,@RadioButton,"Capture Color",260,10,150,20,0x50000009,6
CONTROL win,@RadioButton,"Capture Window",260,40,150,20,0x50000009,7
CONTROL win,@RadioButton,"Capture Free",260,70,150,20,0x50000009,8
SETSTATE win, 6, 1 
choice=6 
CONTROL win,@Edit,"",50,10,105,20,0,3
CONTROL win,@Edit,"",50,50,105,20,0,4
CONTROL win,@Edit,"",50,90,105,20,0,5
background = GetSysColor(15)
SETWINDOWCOLOR win,background
SetWindowPos(win.hwnd, -1, 0, 0, 0, 0, 0x1|0x2)
backpen win,background     
MOVE win,10,10   
PRINT win,"Raw" 
MOVE win,190,2 
PRINT win,"Zoom" 
MOVE win,192,88 
PRINT win,"Color" 
MOVE win,10,50   
PRINT win,"RGB"   
MOVE win,10,90   
PRINT win,"Hex"   
   
RECT win,179,105,65,65,RGB(0,0,0),background 
RECT win,179,19,65,65,RGB(0,0,0),RGB(0,0,0) 
run=1   
WAITUNTIL run=0   
SETFOCUS win 
CLOSEWINDOW RegionWin 
CLOSEWINDOW View 
CLOSEWINDOW w 
CLOSEWINDOW win 
END 
SUB colref   
   SELECT @CLASS   
ENDSELECT 
RETURN 
EndSub
SUB main   
   SELECT @CLASS   
      CASE @IDCONTROL   
         SELECT @CONTROLID
     
            CASE 1
if @notifycode = 0 
    STOPTIMER(win,0) 
ENABLECONTROL win, 2, 1   
col=COLORREQUEST (win)   
IF col>-1   
CALCUL() 
    RECT win,179,105,65,65,RGB(0,0,0),col 
    RECT w,0,0,63,63,RGB(0,0,0),col 
ENDIF
endif   
            CASE 2
if @notifycode = 0
                ENABLECONTROL win, 1, 0   
                ENABLECONTROL win, 2, 0 
                ENABLECONTROL win, 6, 0 
                ENABLECONTROL win, 7, 0 
                ENABLECONTROL win, 8, 0   
SELECT choice 
case 6 
MESSAGEBOX win,"Move the cursor and press space!","Warning"   
case 7 
MESSAGEBOX win,"Select Caption Window or Client Area Window and press Space !","Warning"   
SHOWWINDOW win, @SWMINIMIZED 
case 8 
MESSAGEBOX win,"Click Right for adjust the frame and Click Left for move the frame and press Space !","Warning" 
SHOWWINDOW win, @SWMINIMIZED 
ENDSELECT 
GetKeyState(0x20)   
                STARTTIMER(win,150,0)
  endif
CASE 6
if @notifycode = 0 
Choice=6
endif 
CASE 7
if @notifycode = 0 
Choice=7
endif 
CASE 8
if @notifycode = 0 
Choice=8
    endif 
         ENDSELECT   
CASE @IDTIMER 
SELECT CHOICE 
CASE 6 
            GetCursorPos(position) 
a1=position.x:b1=position.y 
hwndscreen = GetDesktopWindow() 
    hdcScreen = GetDC(hwndscreen) 
handle1 = GetHDC (w) 
resultatS=StretchBlt(handle1, 0, 0, 63, 63, hdcScreen, a1-10,b1-10, 21 , 21, @SRCCOPY) 
                col = MyGetPixel(handle1, 31 ,31) 
ReleaseDC (hwndscreen,hdcscreen) 
ReleaseHDC (w, handle1) 
Calcul() 
                RECT win,179,105,65,65,RGB(0,0,0),col   
RECT w,30,30,3,3,RGB(0,0,0),RGB(0,0,0) 
CASE 7 
hwndscreen = GetDesktopWindow() 
    hdcScreen = GetDC(hwndscreen) 
            GetCursorPos(position) 
            HwndPoint=WindowFromPoint(position.x ,position.y)   
            IF HwndPoint>0   
                HwndPoint=WindowFromPoint(position.x ,position.y)   
                GetWindowRect(HwndPoint, dimens)   
                handleHdc =GetWindowDC(HwndPoint) 
            ReleaseDC (HwndPoint, handleHdc)   
            ENDIF   
CASE 8 
STOPTIMER(win,0) 
                GetWindowRect(RegionWin.hwnd, dimens)   
GetCursorPos(position) 
PosX2=position.X 
PosY2=position.Y 
                PosX1=PosX2+6-80 
                PosY1=PosY2+6-80  
SETSIZE RegionWin, PosX1-6, PosY1-6, PosX2+10-PosX1, PosY2+10-PosY1 
Region() 
A=0 
SetWindowPos(RegionWin.hwnd, -1, 0, 0, 0, 0, 0x1|0x2)   
SHOWWINDOW RegionWin, @SWRESTORE 
STARTTIMER(RegionWin,50,0) 
ENDSELECT 
IF GetKeyState(0x20) 
STOPTIMER(win,0)   
IF GETSTATE (win, 7) 
Width=dimens.Right-dimens.left 
Height=dimens.Bottom-dimens.top 
viewver() 
SETSIZE View, 100,  100, 200, 200 
GETCLIENTSIZE View, varL,varT,varW,varH 
varW1=Width-varW 
varH1=Height-varH 
SETSIZE View, 100,  100, 200+varW1, 200+varH1  
CENTERWINDOW View 
SHOWWINDOW View, @SWRESTORE 
SetWindowPos(View.hwnd, -1, 0, 0, 0, 0, 0x1|0x2)   
ENDIF 
ReleaseDC (hwndscreen,hdcscreen) 
ReleaseHDC (w, handle1)   
Enable()                 
ENDIF   
CASE @IDCLOSEWINDOW 
            STOPTIMER(win,0)   
ReleaseDc (HwndScreen,HdcScreen)   
ReleaseHDC (w, handle1) 
run=0 
        CASE @IDCREATE 
            CENTERWINDOW win   
   ENDSELECT   
RETURN   
EndSub
SUB CALCUL()   
SETCONTROLTEXT win,3,str$(col)   
b=int(col/65536)   
g=int(col%65536/256)   
r=int(col-b*65536-g*256)   
ref=str$(r)+","+str$(g)+","+str$(b)   
SETCONTROLTEXT win,4,ref   
hr = hex$(r): hg = hex$(g): hb = hex$(b)   
if len(hr) = 1 then hr = "0" + hr   
if len(hg) = 1 then hg = "0" + hg   
if len(hb) = 1 then hb = "0" + hb   
ref="0x"+hb+hg+hr   
SETCONTROLTEXT win,5,ref   
RETURN 
EndSub
SUB viewver() 
Hdlebitmap=CreateCompatibleBitmap(hdcScreen,Width, Height) 
HmemDC=CreateCompatibleDC(hdcScreen) 
SelectObject(HmemDC, Hdlebitmap)  
resultatS=StretchBlt(HmemDC, 0, 0, Width, Height, hdcScreen, dimens.left ,dimens.top, Width, Height, @SRCCOPY) 
RETURN 
EndSub
SUB subview 
   SELECT @CLASS 
CASE @IDMENUPICK 
SELECT @MENUNUM 
CASE 1 
filter = "Bitmap Files (*.*)|*.*||" 
filename = FILEREQUEST("Save Bitmap",view,0,filter,"bmp") 
if len(filename) 
SaveBitmap (filename, view, 0, 0, Width,Height)
ENDIF 
ENDSELECT 
CASE @IDCLOSEWINDOW 
    SHOWWINDOW View, @SWHIDE 
    SHOWWINDOW win, @SWRESTORE   
DeleteDC(HmemDC) 
DeleteObject(Hdlebitmap) 
CASE @IDPAINT 
DEF HdcViewer:INT 
HdcViewer = GetHDC (View) 
StretchBlt(HdcViewer, 0, 0, Width, Height, HmemDC, 0, 0, Width, Height, @SRCCOPY) 
ReleaseHDC (View, HdcViewer)   
ENDSELECT 
RETURN 
EndSub
'-------------------------Capture free------------------------ 
SUB RegionWinProc 
     SELECT @CLASS 
case @IDRBUTTONDN 
a=2 
case @IDRBUTTONUP 
a=3 
case @IDLBUTTONDN 
a=3 
SENDMESSAGE RegionWin, @WM_NCLBUTTONDOWN,@HTCAPTION,0   
case @IDTIMER 
IF GetKeyState(0x20) 
    STOPTIMER(RegionWin,0) 
                GetWindowRect(RegionWin.hwnd, dimens) 
hwndscreen = GetDesktopWindow() 
    hdcScreen = GetDC(hwndscreen) 
dimens.left=dimens.left+6 
dimens.top=dimens.top+6 
Width=dimens.Right-dimens.left-6 
Height=dimens.Bottom-dimens.top-6 
viewver()   
ReleaseDC (hwndscreen,hdcscreen)   
ReleaseHDC (w, handle1) 
Enable() 
SHOWWINDOW RegionWin, @SWHIDE 
SETSIZE View, 100,  100, 200, 200 
GETCLIENTSIZE View, varL,varT,varW,varH 
varW1=Width-varW 
varH1=Height-varH 
SETSIZE View, 100,  100, 200+varW1, 200+varH1  
CENTERWINDOW View 
SHOWWINDOW View, @SWRESTORE 
SetWindowPos(View.hwnd, -1, 0, 0, 0, 0, 0x1|0x2) 
ENDIF 
SELECT A 
CASE 2 
                GetWindowRect(RegionWin.hwnd, dimens)   
GetCursorPos(position) 
PosX2=position.X 
PosY2=position.Y 
                PosX1=dimens.left+6 
                PosY1=dimens.top+6 
SETSIZE RegionWin, PosX1-6, PosY1-6, PosX2+10-PosX1, PosY2+10-PosY1 
Region() 
      ENDSELECT 
     ENDSELECT 
RETURN 
EndSub
SUB Region () 
       Region1 = CreateRectRgn(0,0,PosX2+10-PosX1,PosY2+10-PosY1) 
       Region2 = CreateRectRgn(6,6,PosX2-PosX1+4,PosY2-PosY1+4) 
       CombineRgn(Region1, Region1, Region2, @RGN_XOR) 
       SetWindowRgn(RegionWin.hwnd, Region1, @TRUE) 
       DeleteObject(Region1) 
       DeleteObject(Region2) 
RETURN 
EndSub
SUB Enable() 
ENABLECONTROL win, 1, 1 
ENABLECONTROL win, 2, 1 
    ENABLECONTROL win, 6, 1 
    ENABLECONTROL win, 7, 1 
    ENABLECONTROL win, 8, 1 
RETURN 
EndSub
'-------------------------------------------------------- 
'functions for saving bitmap files
SUB SaveBitmap(filename:string,win:window,l:int,t:int,w:int,h:int)
def hdcWin,hdcComp,hbitmap,hbitmapold,quadsize:int
def info:BITMAPINFOHEADER
def fileheader:BITMAPFILEHEADER
def lpbits,lpbminfo:MEMORY
def fileb:BFILE
ZeroMemory(info,len(info))
ZeroMemory(fileheader,len(fileheader))
hdcWin = GetHDC(win)
hdcComp = CreateCompatibleDC(hdcWin)
if hdcComp = 0
messagebox win,"Couldn't create DC","Error"
ReleaseHDC win,hdcWin
return
endif
hbitmap = CreateCompatibleBitmap(hdcwin,w,h)
if hbitmap = 0
messagebox win,"Couldn't create bitmap","Error"
ReleaseHDC win,hdcWin
endif
hbitmapold = SelectObject(hdcComp,hbitmap)
BitBlt(hdcComp,0,0,w,h,hdcWin,l,t,0x00CC0020)
SelectObject(hdcComp,hbitmapold)
DeleteDC(hdcComp)
REM at this point hbitmap contains a valid bitmap handle.
REM fill in the BITMAPINFOHEADER and compute the color data
quadsize = CreateInfoStructure(win,hbitmap,info)
REM allocate memory and get the 'bits' of the bitmap
allocmem lpbits,1,info.biSizeImage
allocmem lpbminfo,1,len(info) + quadsize
writemem lpbminfo,1,info
GetDIBits(hdcWin,hbitmap,0,info.biHeight,lpbits,lpbminfo,0)
REM open a binary file and write the header,color data and bitmap bits
if(openfile(fileb,filename,"W") = 0)
fileheader.bfType = 0x4d42
fileheader.bfSize = len(fileheader) + info.biSize + quadsize + info.biSizeImage
fileheader.bfOffBits = len(fileheader) + info.biSize + quadsize
write fileb,fileheader
write fileb,lpbminfo
write fileb,lpbits
closefile fileb
endif
freemem lpbminfo
freemem lpbits
ReleaseHDC win,hdcWin
DeleteObject(hbitmap)
RETURN
ENDSUB

SUB CreateInfoStructure(win:WINDOW,hbitmap:int,info:BITMAPINFOHEADER)
def bmp:BITMAP
def mem:MEMORY
def cClrBits:WORD
def quadsize:int : quadsize = 0
Allocmem mem,1,len(bmp)
GetObjectA(hbitmap,len(bmp),mem)
readmem mem,1,bmp
freemem mem
cClrBits = bmp.bmPlanes * bmp.bmBitsPixel
    if (cClrBits = 1)
        cClrBits = 1
    else
if (cClrBits <= 4)
      cClrBits = 4
    else
if (cClrBits <= 8)
        cClrBits = 8
    else
if (cClrBits <= 16)
        cClrBits = 16
    else
if (cClrBits <= 24)
        cClrBits = 24
    else
        cClrBits = 32
endif
endif
endif
endif
endif
    info.biSize = len(info)
    info.biWidth = bmp.bmWidth
    info.biHeight = bmp.bmHeight
    info.biPlanes = bmp.bmPlanes
    info.biBitCount = bmp.bmBitsPixel
    if (cClrBits < 24)
        info.biClrUsed = 2^cClrBits
endif
info.biCompression = 0
info.biSizeImage = (info.biWidth + 7.0) / 8.0 * info.biHeight * cClrBits
if(cClrBits < 24)
quadsize = 4 * (2^cClrBits)
endif
RETURN quadsize
ENDSUB
Regards,

Peter B.

TonyMUK

With the kind help of Sapero I managed to create a dll that does the job I wanted.
Check out www.arnoldchiari.co.uk. If you don't know what Arnold Chiari is you are very lucky.