Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Solved]GDI+ screen capture and resize



  • Please log in to reply
11 replies to this topic
tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
I am noob to GDI and am afraid i just dont understand at all.

I am using the following code [snippet from something larger]
	pToken := Gdip_Startup()
	Gdip_SaveBitmapToFile(pBitmap := Gdip_BitmapFromScreen( "hwnd:" hwnd ), DragFileCapt)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
Super works marvelously
What i also need is a scaled down version of the exact same image at DragFileCapt path i can handle renaming it. I just cant figure out how to make a 100x100 copy of the same image and yes i know quality reduction
I also know image skewing could(likely) occur. So in the end i will have a shrunken and a full sized image..... Oh i feel the ridicule coming..... :wink:

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
dang it all to hell 2 more minutes of searching and i found this
ConvertImage()~tic

Oh the noob name calling i got commin for this :oops:

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
Well now i seem to still need help the previous link leads here to this function

ConvertImage(sInput, sOutput, Width="", Height="", Method="Percent")
{
   pBitmap := Gdip_CreateBitmapFromFile(sInput)
   Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
   
   If (Method = "Percent")
   {
      Width := (Width = -1) ? Height : Width, Height := (Height = -1) ? Width : Height
      dWidth := Round(sWidth*(Width/100)), dHeight := Round(sHeight*(Height/100))
   }
   else If (Method = "Pixels")
   {
      if (Width = -1)
      dWidth := Round((Height/sHeight)*sWidth), dHeight := Height
      else if (Height = -1)
      dHeight := Round((Width/sWidth)*sHeight), dWidth := Width
      else
      dWidth := Width, dHeight := Height
   }
   else
      return -1

   pBitmap1 := Gdip_CreateBitmap(dWidth, dHeight)
   G1 := Gdip_GraphicsFromImage(pBitmap1), Gdip_SetInterpolationMode(G1, 7)

   Gdip_SaveBitmapToFile(pBitmap, sOutput)
   Gdip_DeleteGraphics(G1)
   Gdip_DisposeImage(pBitmap), Gdip_DisposeImage(pBitmap1)
   return 0
}

and it does create a new file but it is NOT resized :cry:
Win 7 64 bit home premium
ahk_l Version v1.1.04.00
the entire call looks like this
	pToken := Gdip_Startup()
	Gdip_SaveBitmapToFile(pBitmap := Gdip_BitmapFromScreen( "hwnd:" hwnd ), DragFileCapt)
	ConvertImage( DragFileCapt, pennyDragFileCapt, 100, 100, "Pixels" )
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
pennyDragFileCapt is just an exact copy of DragFileCapt :twisted: :evil:

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
I hope this example will help you. More functions and info here.
F1::
hwnd := WinExist("A")	; let's take a screenshot of active window
pToken := Gdip_Startup()

pBitmap := Gdip_BitmapFromScreen( "hwnd:" hwnd )	; or	pBitmap := Gdip_BitmapFromHWND(hwnd)
Gdip_SaveBitmapToFile(pBitmap, "Full sized image.png")	; save Full sized image

pResizedBitmap := Gdip_ResizeBitmap(pBitmap, 50)   ; resizes to 50% and disposes of pBitmap. Look below for more resizing examples.
Gdip_SaveBitmapToFile(pResizedBitmap, "Resized image.png")	; save Resized image

Gdip_DisposeImage(pResizedBitmap)
Gdip_Shutdown(pToken)
return



#Include Gdip.ahk				; by Tic	http://www.autohotkey.com/forum/topic32238.html

Gdip_ResizeBitmap(pBitmap, PercentOrWH, Dispose=1) {   ; returns resized bitmap. By Learning one.
	; http://www.autohotkey.com/forum/post-477333.html#477333
   Gdip_GetImageDimensions(pBitmap, origW, origH)
   if PercentOrWH contains w,h
   {
      RegExMatch(PercentOrWH, "i)w(\d*)", w), RegExMatch(PercentOrWH, "i)h(\d*)", h)
      NewWidth := w1, NewHeight := h1
      NewWidth := (NewWidth = "") ? origW/(origH/NewHeight) : NewWidth
      NewHeight := (NewHeight = "") ? origH/(origW/NewWidth) : NewHeight
   }
   else
   NewWidth := origW*PercentOrWH/100, NewHeight := origH*PercentOrWH/100      
   pBitmap2 := Gdip_CreateBitmap(NewWidth, NewHeight)
   G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
   Gdip_DrawImage(G2, pBitmap, 0, 0, NewWidth, NewHeight)
   Gdip_DeleteGraphics(G2)
   if Dispose
      Gdip_DisposeImage(pBitmap)
   return pBitmap2
}

/*
;Examples:
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, 120)   ; resizes to 120%. Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, 50, 0)   ; resizes to 50%. Does not dispose of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "w180 h80")   ; resizes to width: 180 height: 80 (does not maintain aspect ratio). Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "w200")   ; resizes to width: 200, automatically adjusts height (maintains aspect ratio). Disposes of pBitmap.
pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "h50")   ; resizes to height: 50, automatically adjusts width (maintains aspect ratio). Disposes of pBitmap.
*/


nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
you can just use Gdip_DrawImage() to resize...

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
✓  Best Answer
How about this :?: Fullsize & 100 x 100 thumbnail

Screen Capture with Transparent Windows and Mouse Cursor
<!-- m -->http://www.autohotke... ... 630#114630<!-- m -->

SysGet, MonitorCount, MonitorCount

Loop, %MonitorCount%
{
  sysget,  MonitorName%A_Index% ,MonitorName 
   this_mon=Mon%a_index%
    SysGet, this_mon, Monitor, %A_Index%
  CaptureScreen(this_monleft "," this_montop "," this_monright "," this_monbottom "," 100 "," 100, false, "thumbnail"this_mon ".jpg")
  CaptureScreen(this_monleft "," this_montop "," this_monright "," this_monbottom ",""," , false, this_mon ".jpg")
}

#include ScreenCapture.ahk


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
@Leef_me In the past i have used Seans function. And in fact this could serve my purpose. However i was hoping to use tic's library to do it. Mostly so i can learn from it.

@nimda I saw this function but cant wrap my tiny little mind around how i would even make such a call

@Learning one some how i didnt even see your post at first. This appears to be in line with my goal. I find this to be exactly what i needed thank you for this

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
tank: I actually posted the same question a while back :shock:
GDIP Resized Screenshots?
Note the drawImage() call

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
just in case this non completed non optimized concept code helps anyone else since i got such great responses. My finished product wont look anything like this but this is just some simplistic code to help conceptualize
My next search will be to find better method of detecting drag and drop events but perhaps that becomes a new thread if i don't figure it out
#ErrorStdOut
#SingleInstance,force
#NoEnv
#Warn,all,off
#Persistent
CoordMode, Mouse, Screen
SetTitleMatchMode,slow
SetTitleMatchMode,2
DetectHiddenText,on
DetectHiddenWindows,on
SetWorkingDir, %A_ScriptDir%
Clipboard := A_AhkVersion
Esc:: ExitApp
^Esc:: Reload


~LButton:: ;;attempt detect drag by detecting mousedown
	{
	i := 0 ;; initialize Counter
	x := xx := y := yy := "" ;; init some variables
	MouseGetPos,x,y,fhwnd,,3
	loop
		{
		
		if GetKeyState( "LButton", "P" ) ;; start a counter if the mouse is down
			{
			MouseGetPos,xx,yy,shwnd,,3
			if ( x != xx || y != yy ) ;; only care if the mouse has moved
				{
				i++ ;; increment counter
				}
			
			if ( i && i < 2 ) ;; only capture the image the first time
				{	
				dragCapture( fhwnd )
				}
			
			Sleep,100
			}
		else 
			{
			dragWrite( fhwnd, shwnd )
			break
			}
		}
	return	
	}


dragWrite( fhwnd, shwnd )
	{
	
	global DragFileCapt,PennyDragFileCapt
	WinGetTitle, fTitle, ahk_id %fhwnd%
	WinGetTitle, sTitle, ahk_id %shwnd%
	entry =
	(
	<p><a href="%DragFileCapt%"><img  src="%PennyDragFileCapt%" alt="%fTitle%" /></a>

	From -> %fTitle%
	To -> %sTitle%</p>
	)
	FileAppend, %entry%, DragLog.html
	ToolTip,
	
	return
	}


dragCapture(hwnd)
	{
	global DragFileCapt,PennyDragFileCapt
	dragStamp := A_Now
	WinGetTitle, sTitle, ahk_id %hwnd%
	DragFileCapt := A_ScriptDir "\ScreenCaptDrag_" dragStamp ".png"
	PennyDragFileCapt := A_ScriptDir "\PennyScreenCaptDrag_" dragStamp ".png"
	nQuality := ""
	pToken := Gdip_Startup()
	Gdip_SaveBitmapToFile(pBitmap := Gdip_BitmapFromScreen( "hwnd:" hwnd ), DragFileCapt)
	
	
	pResizedBitmap := Gdip_ResizeBitmap(pBitmap, "w100 h100")   ; resizes to 50% and disposes of pBitmap. Look below for more resizing examples.
	Gdip_SaveBitmapToFile(pResizedBitmap, PennyDragFileCapt)   ; save Resized image

	Gdip_DisposeImage(pResizedBitmap)
	
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
	return
	}

Gdip_ResizeBitmap(pBitmap, PercentOrWH, Dispose=1) {   ; returns resized bitmap. By Learning one.
   ; http://www.autohotkey.com/forum/topic32238-855.html
   Gdip_GetImageDimensions(pBitmap, origW, origH)
   if PercentOrWH contains w,h
   {
      RegExMatch(PercentOrWH, "i)w(\d*)", w), RegExMatch(PercentOrWH, "i)h(\d*)", h)
      NewWidth := w1, NewHeight := h1
      NewWidth := (NewWidth = "") ? origW/(origH/NewHeight) : NewWidth
      NewHeight := (NewHeight = "") ? origH/(origW/NewWidth) : NewHeight
   }
   else
   NewWidth := origW*PercentOrWH/100, NewHeight := origH*PercentOrWH/100      
   pBitmap2 := Gdip_CreateBitmap(NewWidth, NewHeight)
   G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
   Gdip_DrawImage(G2, pBitmap, 0, 0, NewWidth, NewHeight)
   Gdip_DeleteGraphics(G2)
   if Dispose
      Gdip_DisposeImage(pBitmap)
   return pBitmap2
}


nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
tank that looks like Zizorz...

Also the resize funtion is overkill. It can be done with CreateBitmap() - GraphicsFromBitmap() - DrawImage()
could probably even do a one-liner

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007

tank that looks like Zizorz...

Also the resize funtion is overkill. It can be done with CreateBitmap() - GraphicsFromBitmap() - DrawImage()
could probably even do a one-liner

No not like Zizorz but thats interesting as well i have another script just like that.

You keep suggesting an easier way but not demonstrating it perhaps its a secret or your scrapbooking for your grandkids :p Or you could share your idea since i haven't figured it out yet

nimda nli
  • Guests
  • Last active:
  • Joined: --
OK when I'm home I'll post a one-liner
It'll resemble this (which probably doesn't work)
resize(pBitmap, w, h){
   return ((pB := gdip_CreateBitmap()) && (pG := gdip_GraphicsFromBitmap(pB)) && (gdip_DrawImage(pG, pBitmap, 0, 0, w, h) ? pB : 0), gdip_DisposeGraphics(pG)
}