Create Screenshot specific area Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Loop
Posts: 167
Joined: 07 Jan 2019, 14:51

Create Screenshot specific area

Post by Loop » 19 Feb 2019, 18:16

Hi,
I want to capture a certain x-y coordinate region of a window and save as a .png file.

how i can do that?

this script not working:


Code: Select all

#Include GDIP.ahk
#Include Gdip_ImageSearch.ahk

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen(15 15 400 400)
Gdip_SaveBitmapToFile(snap, "Shot.png")

Gdip_DisposeImage(snap)

thanks in advance

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Create Screenshot specific area

Post by FanaticGuru » 19 Feb 2019, 18:25

Loop wrote:
19 Feb 2019, 18:16
Hi,
I want to capture a certain x-y coordinate region of a window and save as a .png file.

how i can do that?

this script not working:


Code: Select all

#Include GDIP.ahk
#Include Gdip_ImageSearch.ahk

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen(15 15 400 400)
Gdip_SaveBitmapToFile(snap, "Shot.png")

Gdip_DisposeImage(snap)

thanks in advance
snap := Gdip_BitmapFromScreen("15|15|400|400")
The input for Gdip_BitmapFromScreen is kind of clunky; numbers seperated by |.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Loop
Posts: 167
Joined: 07 Jan 2019, 14:51

Re: Create Screenshot specific area

Post by Loop » 19 Feb 2019, 19:06

thank you for the answer

Unfortunately, there is still no screenshot taken.

Code: Select all

#Include Gdip.ahk
#Include Gdip_ImageSearch.ahk

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("15|15|400|400")
Gdip_SaveBitmapToFile(snap, "Shot.png")

Gdip_DisposeImage(snap)

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Create Screenshot specific area  Topic is solved

Post by FanaticGuru » 19 Feb 2019, 19:53

Loop wrote:
19 Feb 2019, 19:06
thank you for the answer

Unfortunately, there is still no screenshot taken.

Code: Select all

#Include Gdip.ahk
#Include Gdip_ImageSearch.ahk

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("15|15|400|400")
Gdip_SaveBitmapToFile(snap, "Shot.png")

Gdip_DisposeImage(snap)
I can confirm that the exact code below works for me:

Code: Select all

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("15|15|400|400")
Gdip_SaveBitmapToFile(snap, "Shot.png")

Gdip_DisposeImage(snap)
I have Gdip.ahk in my standard library so no #include used.

I assume you know Gdip is a library that has to be downloaded. Here is the most current version I could easily google: https://github.com/mmikeww/AHKv2-Gdip/blob/master/Gdip_All.ahk

FG
Last edited by FanaticGuru on 19 Feb 2019, 20:03, edited 1 time in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Create Screenshot specific area

Post by Scr1pter » 19 Feb 2019, 19:57

Maybe you have to create a path.
In your header, initialize a variable with the whole path + filename.
Inside of Gdip_SaveBitmapToFile, replace “Shot.png“ by the variable.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Loop
Posts: 167
Joined: 07 Jan 2019, 14:51

Re: Create Screenshot specific area

Post by Loop » 20 Feb 2019, 09:09

Thanks!
it was on Gdip.ahk file

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 02 Apr 2022, 11:12

@FanaticGuru Instead of Shot.png, how can I save to a folder as well as to not replace the file, but create new?

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Create Screenshot specific area

Post by Hellbent » 05 Apr 2022, 20:14

LAPIII wrote:
02 Apr 2022, 11:12
Instead of Shot.png, how can I save to a folder as well as to not replace the file, but create new?
You can have a look through this and see if you find anything useful.
I wrote this a few years ago live on camera so obviously some improvements can be made to it but I have used it whenever I need to keep a screen cap on screen and haven't had any issues with it.

Code: Select all

;Written By: Hellbent
;Date Started: Dec 6th, 2019
;Date of Last Edit: Dec 7th, 2019
;Name: Screen Clipper
;Inspired by the screen clipping tool by Joe Glines.
;Pastebin Save: https://pastebin.com/fSErF35q  (last save)

;User Defined Values
;************************************************
Hotkey, RALT , CreateCapWindow , On  ;<------------------    Adjust the hotkey value to suit your needs / wants 
;************************************************
SaveToFile := 1 		 		   		;<------------------    Set this to 1 to save all clips with a unique name , Set it to 0 to overwrite the saved clip every time a new clip is made.
;************************************************
ShowCloseButton := 1 			   		;<------------------    Set this to 1 to show a small close button in the top right corner of the clip. Set this to 0 to keep the close button, but not show it.
;************************************************

#SingleInstance, Force  ; Force the script to close any other instances of this script. (Run one copy at a time)
SetBatchLines, -1 ;Set the script to run at top speed.
CoordMode, Mouse , Screen ;Use the screen as the refrence to get positions from.

IfNotExist, %A_ScriptDir%\Saved Clips ; if there is no folder for saved clips
	FileCreateDir, %A_ScriptDir%\Saved Clips ; create the folder.
SetWorkingDir, %A_ScriptDir%\Saved Clips ;Set the saved clips folder as the working dir.

Handles := [] ; Create an array to hold the name of the different gui's.
Index := 0 ;Used as the name of the current gui cap window.
return   ; End of Auto-Execute Section.

;**************************************************************************************
;**************************************************************************************
*^ESC::	ExitApp ; Hotkey to exit the script.
;**************************************************************************************
;**************************************************************************************

CloseClip: ;Close (Destroy this gui)
	hwnd := WinActive() ;Get the handle to the active window
	Gui, % Handles[ hwnd ] ": Destroy"  ;Destroy the gui with the name stored in the Handles array at position hwnd.
	return

MoveWindow: 
	PostMessage, 0xA1 , 2  ;Move the active window
	return

CreateCapWindow: ;Create a gui to used for setting the screen cap area and to display the resulting screen shot.
	Index++ ;Increment the current index. (the gui's name)
	Gui, %Index% : New , +AlwaysOnTop -Caption -DPIScale +ToolWindow +LastFound +Border hwndHwnd ;Create a new gui and set its options.
	Handles[hwnd] := Index  ;Use the windows handle (hwnd) as the index for the the value of the windows name. 
	Gui, %Index% : Color , 123456 ;Set the color of the gui (This color will be made transparent in the next step)
	WinSet, TransColor , 123456 ;Set only this color as transparent
	Gui, %Index% : Font, cMaroon s10 Bold Q5 , Segoe UI ;Set this gui's font. (Used for the close button)
	Active := 1
	ToolTip, Click and drag to set capture area. ;Display a tooltip that tells the user what to do.
	SetTimer, Tooltips , 30 ;Set a timer to move the tooltip around with the users cursor.
	return

Tooltips:
	ToolTip, Click and drag to set capture area. ;Display a tooltip that tells the user what to do.
	return

DrawCapArea:
	MouseGetPos, EX , EY ;Get the current position of the cursor.
	if( SX <= EX && SY <= EY )  ;If the current position is below and to the right of the starting position.
		WinPos := { X: SX , Y: SY , W: EX - SX , H: EY - SY } ;Create a object to hold the windows positions.
	else if( SX > EX && SY <= EY ) ;If the current position is below and to the left of the starting position.
		WinPos := { X: EX , Y: SY , W: SX - EX , H: EY - SY } ;Create a object to hold the windows positions.
	else if( SX <= EX && SY > EY ) ;If the current position is above and to the right of the starting position.
		WinPos := { X: SX , Y: EY , W: EX - SX , H: SY - EY } ;Create a object to hold the windows positions.
	else if( SX > EX && SY > EY ) ;If the current position is above and to the left of the starting position.
		WinPos := { X: EX , Y: EY , W: SX - EX , H: SY - EY } ;Create a object to hold the windows positions.
	Gui, %Index% : Show , % "x" WinPos.X " y" WinPos.Y " w" WinPos.W " h" WinPos.H " NA" ;Show the window in the correct position.
	return

TakeScreenShot:
	pToken := Gdip_Startup() ;Start using Gdip
	ClipBitmap := Gdip_BitmapFromScreen( WinPos.X "|" WinPos.Y "|" WinPos.W "|" WinPos.H ) ;Create a bitmap of the screen.
	
	Gdip_SaveBitmapToFile( ClipBitmap , A_WorkingDir "\" ( ( SaveToFile = 1 ) ? ( ClipName := "Saved Clip " A_Now ) : ( ClipName := "Temp Clip" ) ) ".png" , 100 ) ; Save the bitmap to file
	Gdip_DisposeImage( ClipBitmap ) ;Dispose of the bitmap to free memory.
	Gdip_Shutdown( pToken ) ;Turn off gdip
	return

#If (Active) ;Context sensitive hotkeys.

LButton::
	MouseGetPos, SX , SY ;Get the x and y starting position.
	SetTimer, DrawCapArea , 30 ;Set a timer for drawing a rectangle around the capture area.
	gosub, DrawCapArea ;Run the DrawCapArea routine immediately 
	return

LButton Up::
	Active := 0 ;Set context hotkeys off
	SetTimer, DrawCapArea , Off ;Turn off the drawing timer.
	SetTimer, Tooltips , Off ;Turn off the tooltips timer
	ToolTip, ;Turn off any tooltips.
	if( WinPos.W < 10 || WinPos.H < 10 ) { ; if the cap area width or height is less than 10px.
		Gui, %Index% : Destroy ;Destroy the gui
		return ; Skip taking a screen clip.
	}
	Gui, %Index% : -Border ;Remove the border before taking the screen clip
	gosub, TakeScreenShot ;Take a screen shot of the cap area.
	Gui, %Index% : +Border +LastFound ;Add the border again and Set the window as "LastFound" to use winset to turn off the transcolor a few lines down
	Gui, %Index% : Add , Text , % ( ( ShowCloseButton ) ? ( " Center 0x200 Border " ) : ( "" ) ) " x" WinPos.W - 20 " y0 w20 h20 BackgroundTrans gCloseClip" , % ( ( ShowCloseButton ) ? ( "X" ) : ( "" ) ) ;Create a trigger used for closing this window.
	Gui, %Index% : Add , Text , % "x0 y0 w" WinPos.W " h" WinPos.H " BackgroundTrans gMoveWindow" ;Create a trigger used for moving the window around.
	Gui, %Index% : Add , Picture , % "x0 y0 w" WinPos.W " h" WinPos.H ,% ClipName ".png"   ;Add the Screen clip image
	WinSet, TransColor , Off ;Turn off the transcolr setting.
	return
	
RButton::  ;If the right mouse button is pressed while selecting the screen clip area, Cancel the action and restore variables etc.
	Active := 0 ;Set context hotkeys off
	SetTimer, DrawCapArea , Off ;Turn off the drawing timer.
	SetTimer, Tooltips , Off ;Turn off the tooltips timer
	ToolTip, ;Turn off any tooltips.
	Gui, %Index% : Destroy ;Destroy the current gui
	return
	
#If

;******************************************************************************************************************************************
;******************************************************************************************************************************************
;***************************************************      GDIP Functions      *************************************************************
;******************************************************************************************************************************************
;******************************************************************************************************************************************
;From the GDIP lib by TIC
Gdip_Startup(){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("LoadLibrary", "str", "gdiplus")
	VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
	return pToken
}
Gdip_BitmapFromScreen(Screen=0, Raster=""){
	if (Screen = 0){
		Sysget, x, 76
		Sysget, y, 77	
		Sysget, w, 78
		Sysget, h, 79
	}else if (SubStr(Screen, 1, 5) = "hwnd:"){
		Screen := SubStr(Screen, 6)
		if !WinExist( "ahk_id " Screen)
			return -2
		WinGetPos,,, w, h, ahk_id %Screen%
		x := y := 0
		hhdc := GetDCEx(Screen, 3)
	}else if (Screen&1 != ""){
		Sysget, M, Monitor, %Screen%
		x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
	}else	{
		StringSplit, S, Screen, |
		x := S1, y := S2, w := S3, h := S4
	}
	if (x = "") || (y = "") || (w = "") || (h = "")
		return -1
	chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
	BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
	ReleaseDC(hhdc)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
	return pBitmap
}
Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	SplitPath, sOutput,,, Extension
	if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
		return -1
	Extension := "." Extension
	DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
	VarSetCapacity(ci, nSize)
	DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
	if !(nCount && nSize)
		return -2
	If (A_IsUnicode){
		StrGet_Name := "StrGet"
		Loop, % nCount 	{
			sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
			if !InStr(sString, "*" Extension)
				continue
			pCodec := &ci+idx
			break
		}
	} else {
		Loop, % nCount 	{
			Location := NumGet(ci, 76*(A_Index-1)+44)
			nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
			VarSetCapacity(sString, nSize)
			DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
			if !InStr(sString, "*" Extension)
				continue
			pCodec := &ci+76*(A_Index-1)
			break
		}
	}
	if !pCodec
		return -3
	if (Quality != 75){
		Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
		if Extension in .JPG,.JPEG,.JPE,.JFIF
		{
			DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
			VarSetCapacity(EncoderParameters, nSize, 0)
			DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
			Loop, % NumGet(EncoderParameters, "UInt") 	{
				elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
				if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6){
					p := elem+&EncoderParameters-pad-4
					NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
					break
				}
			}      
		}
	}
	if (!A_IsUnicode){
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
		VarSetCapacity(wOutput, nSize*2)
		DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
		VarSetCapacity(wOutput, -1)
		if !VarSetCapacity(wOutput)
			return -4
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
	}
	else
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
	return E ? -5 : 0
}
Gdip_DisposeImage(pBitmap){
   return DllCall("gdiplus\GdipDisposeImage", A_PtrSize ? "UPtr" : "UInt", pBitmap)
}
Gdip_Shutdown(pToken){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
	if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("FreeLibrary", Ptr, hModule)
	return 0
}
GetDCEx(hwnd, flags=0, hrgnClip=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
    return DllCall("GetDCEx", Ptr, hwnd, Ptr, hrgnClip, "int", flags)
}
CreateCompatibleDC(hdc=0){
   return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	hdc2 := hdc ? hdc : GetDC()
	VarSetCapacity(bi, 40, 0)
	NumPut(w, bi, 4, "uint") , NumPut(h, bi, 8, "uint") , NumPut(40, bi, 0, "uint") , NumPut(1, bi, 12, "ushort") , NumPut(0, bi, 16, "uInt") , NumPut(bpp, bi, 14, "ushort")
	hbm := DllCall("CreateDIBSection" , Ptr, hdc2 , Ptr, &bi , "uint", 0 , A_PtrSize ? "UPtr*" : "uint*", ppvBits , Ptr, 0 , "uint", 0, Ptr)
	if !hdc
		ReleaseDC(hdc2)
	return hbm
}
SelectObject(hdc, hgdiobj){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
}
GetDC(hwnd=0){
	return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
}
BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster=""){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("gdi32\BitBlt" , Ptr, dDC , "int", dx , "int", dy , "int", dw , "int", dh , Ptr, sDC , "int", sx , "int", sy , "uint", Raster ? Raster : 0x00CC0020)
}
ReleaseDC(hdc, hwnd=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
}
Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
	return pBitmap
}
DeleteObject(hObject){
   return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
}
DeleteDC(hdc){
   return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
This is the part that you are looking for.

Code: Select all

TakeScreenShot:
	pToken := Gdip_Startup() ;Start using Gdip
	ClipBitmap := Gdip_BitmapFromScreen( WinPos.X "|" WinPos.Y "|" WinPos.W "|" WinPos.H ) ;Create a bitmap of the screen.
	
	Gdip_SaveBitmapToFile( ClipBitmap , A_WorkingDir "\" ( ( SaveToFile = 1 ) ? ( ClipName := "Saved Clip " A_Now ) : ( ClipName := "Temp Clip" ) ) ".png" , 100 ) ; Save the bitmap to file
	Gdip_DisposeImage( ClipBitmap ) ;Dispose of the bitmap to free memory.
	Gdip_Shutdown( pToken ) ;Turn off gdip
	return
Up at the top of the script is where you can find a few variables that are set up to adjust the settings, particularly the variable "SaveToFile"

Here is the video if you would like to see it written from scratch.



GL

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 15 Apr 2022, 00:27

Thanks for sharing this all! Took some time to get the hang of it, but finally got a few scripts working. Nice time saver.

How can I choose to save the pictures in a specific location, like my desktop? Right now it saves wherever the .ahk file is located.

Code: Select all

#include C:\Users\Owner\Documents\Other\AutoHotkey Scripts\Gdip.ahk.ahk

^u::
; Charts
pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("1400|148|1142|1163")
Gdip_SaveBitmapToFile(snap, "test.png")

Gdip_DisposeImage(snap)
Return

Thanks

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Create Screenshot specific area

Post by BoBo » 15 Apr 2022, 00:35

I'd guess that you simply have to add a path to the filename?

Code: Select all

Gdip_SaveBitmapToFile(snap, "test.png")

Gdip_SaveBitmapToFile(snap, "c:\folder\subfolder\test.png")

fPath := "c:\folder\subfolder"
Gdip_SaveBitmapToFile(snap,fPath "\test.png")

Gdip_SaveBitmapToFile(snap,A_Desktop "\test.png")

fName	:= "my.txt"
fPath	:=  A_Desktop
Gdip_SaveBitmapToFile(snap,fPath "\" fName)

Extras:
:arrow: SetWorkingDir
:arrow: A_Desktop ;)

HTH

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 15 Apr 2022, 10:37

Bobo, would I just add that to my original script? I'll try that once I'm behind my computer again and see if that works.

Code: Select all


#include C:\Users\Owner\Documents\Other\AutoHotkey Scripts\Gdip.ahk.ahk

^u::
; Charts
pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("1400|148|1142|1163")
Gdip_SaveBitmapToFile(snap, "test.png")

Gdip_DisposeImage(snap)

Gdip_SaveBitmapToFile(snap, "test.png")

Gdip_SaveBitmapToFile(snap, "c:\folder\subfolder\test.png")

fPath := "c:\folder\subfolder"
Gdip_SaveBitmapToFile(snap,fPath "\test.png")

Gdip_SaveBitmapToFile(snap,A_Desktop "\test.png")

fName	:= "my.txt"
fPath	:=  A_Desktop

Return

[Mode edit: Changed "quote" to "code" tags.]

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 15 Apr 2022, 13:59

Can someone please help with Line number 14:

Code: Select all

#SingleInstance, Force
#NoEnv 
SetWorkingDir %A_ScriptDir%
#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\Gdip_ImageSearch.ahk

^+PrintScreen::

pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("15|15|400|400")
FormatTime, DateTime, d-M-yy h-mm-ss tt
    WinGetActiveTitle, Title
    Screenshot(snap, Title "_" DateTime ".png")

Gdip_DisposeImage(snap)

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 15 Apr 2022, 16:36

Hey Bobo, that did the trick! Here's the updated code I'm using. Thank you.

Code: Select all

^i::
; Test
pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("548|571|1172|571")
Gdip_SaveBitmapToFile(snap, "C:\Users\Owner\Desktop\Test.png")

fPath := "C:\Users\Owner\Desktop"
Gdip_SaveBitmapToFile(snap,fPath "\Test.png")

Gdip_SaveBitmapToFile(snap,A_Desktop "\Test.png")

fName	:= "my.txt"
fPath	:=  A_Desktop

Return

And LAPIII, I'm not experienced enough with AHK to know what's wrong.

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 15 Apr 2022, 16:39

Here's a screen coordinate tool I found and worked for me. I couldn't get Window Spy down to figure out the right coords, so this worked for me instead.

http://www.breakthrusoftware.com/html/onlinedocs/kb/installkb/ScreenCoordTool.html

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 15 Apr 2022, 16:57

feeko wrote:
15 Apr 2022, 16:39
Here's a screen coordinate tool I found and worked for me. I couldn't get Window Spy down to figure out the right coords, so this worked for me instead.

http://www.breakthrusoftware.com/html/onlinedocs/kb/installkb/ScreenCoordTool.html

I have an AHK script that will get any active window's X and Y coordinates as well as width and height to copy it to the clipboard:

Code: Select all

#SingleInstance, Force
^+s::
WinGetPos, X, Y, W, H, A
MsgBox, The position %X%`,%Y%`,%W%`,%Y% is copied to the clipboard.
Clipboard := "{x: " X ", y: " Y ", w: " W ", h: " H "}"
return

Also, in your last script lined 3 has a 'Call to nonexistent function.'

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 15 Apr 2022, 20:12

Nice LAPII, so I would only have to click on the screen I want be screenshot and then it automatically goes to my clipboard?

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 15 Apr 2022, 21:34

Yeah

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 19 Apr 2022, 11:54

Can someone please share how to change this script to copy to the clipboard instead of saving as a file? Is that possible or do I need to build a new script calling different features?

Code: Select all

^i::
; Test
pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("548|571|1172|571")
Gdip_SaveBitmapToFile(snap, "C:\Users\Owner\Desktop\Test.png")

fPath := "C:\Users\Owner\Desktop"
Gdip_SaveBitmapToFile(snap,fPath "\Test.png")

Gdip_SaveBitmapToFile(snap,A_Desktop "\Test.png")

fName	:= "my.txt"
fPath	:=  A_Desktop

Return

User avatar
boiler
Posts: 16914
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 12:55

Depending on where you have your Gdip_All.ahk file, you may need to add an #Include statement at the top.

Code: Select all

^i::
	pToken := Gdip_Startup()
	pBitmap := Gdip_BitmapFromScreen("548|571|1172|571")
	Gdip_SetBitmapToClipboard(pBitMap)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
return

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 19 Apr 2022, 13:12

@boiler thank you for that. And thanks for starting a new thread with my older question.

Yeah, I have that included at the top of my script. Any reason why my file extension has 2 .ahk? I don't think it worked if I didn't have it.

Code: Select all

#include C:\Users\Owner\Documents\Other\AutoHotkey Scripts\Gdip.ahk.ahk

edit:
Your script worked @boiler. Thank you!

Post Reply

Return to “Ask for Help (v1)”