Jump to content

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

convert/resize image with Gdip (solved)


  • Please log in to reply
5 replies to this topic
closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
This is still work in progress!! I tried to find an alternative to using irfanview in AHK programs.

Using tic's gdip.ahk library functions ( thanks tic!!!!) i tried to make a resize/convert function.

But i am stuck with the if else structure to define the ratio.I can only get it to work like in the next example if i use a true/false option in the function

I hope someone has a better solution so there will be only 5 parameters to fill in left!I spent to much time staring at it without ideas.........





#SingleInstance, Force
#NoEnv
SetBatchLines, -1
/*
if Gdip.ahk is not in your standard library use the #Include Gdip.ahk
*/

;#Include, Gdip.ahk


/*
time:=A_Tickcount
loop,c:\test\*.bmp
{
source:=A_LoopFileFullPath
dest = c:\testpng\%A_Index%.png
convert_resize(source,dest,200,0,"true",0.8)
}

MsgBox, % A_Tickcount - time




to convert:
convert_resize(source,dest)
to fixed width:
convert_resize(source,dest,200)
to fixed height:
convert_resize(source,dest,0,150)
to ratio 0.8:
convert_resize(source,dest,0,0,"true",0.8)
*/


convert_resize(source_file,out_file,fixed_width=0,fixed_height=0,enable_ratio="false",ratio=1){
If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}

pBitmapFile := Gdip_CreateBitmapFromFile(source_file)
Width := Gdip_GetImageWidth(pBitmapFile), Height := Gdip_GetImageHeight(pBitmapFile)

if (enable_ratio = "true")
ratio:=ratio

Else
{
if !fixed_height and fixed_width
ratio:=fixed_width/width
if !fixed_width and  fixed_height
ratio:=fixed_height/height
}
;msgbox %ratio%
w:=floor(width*ratio)
h:=floor(height*ratio)
pBitmap := Gdip_CreateBitmap(w, h)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_DrawImage(G, pBitmapFile, 0, 0, w, h, 0, 0, Width, Height)
Gdip_SaveBitmapToFile(pBitmap, out_file)

Gdip_DisposeImage(pBitmapFile)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
}



polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

I tried to find an alternative to using irfanview in AHK programs.

I haven't used gdip much, but ImageMagick can also resize images.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
I should have mentioned i was looking for an AHK solution.

But the solution is embarrassing simple..............i wonder why i missed it yesterday. :oops:

syntax will be:

to convert BMP,PNG,JPG:
convert_resize(source,dest)
to fixed width 200:
convert_resize(source,dest,"k_width",200)
to fixed height 150 :
convert_resize(source,dest,"k_height",150)
to ratio 0.8:
convert_resize(source,dest,"k_ratio",0.8 )

will post it in scripts section

update:

did a test with irfanview 1009 jpg files (306Mb) resized to width 200 and saved as png

using irfanview
runwait,%A_Scriptdir%\i_view32.exe  %A_LoopFileFullPath% /resize_long=200 /aspectratio /resample /convert=c:\testpng\%A_Index%.png

using gdip

convert_resize(source,dest,"k_width",200)


irfanview took 170 sec
gdip took 83 sec

PC has an old celeron processor with 512Mb ram running XP SP3

rapidfoxlab
  • Members
  • 3 posts
  • Last active: Apr 24 2010 02:34 PM
  • Joined: 19 Apr 2010
Nice one Yume, this is exactly what I was looking for. Thanks!

closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
I never posted the final version because i found convert programs recently added to tic's gidplus post.


But here is the final version of the above post,you can increase speed some more when converting a lot of images in one go by getting the Gdip_Startup and Gdip_shutdown separate from the function.

convert_resize(source_file,out_file,function="",value=1){
If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}

pBitmapFile := Gdip_CreateBitmapFromFile(source_file)
Width := Gdip_GetImageWidth(pBitmapFile), Height := Gdip_GetImageHeight(pBitmapFile)
ratio=1
if (function = "k_ratio")
ratio:=value
if (function = "k_width")
ratio:=value/width
if (function = "k_height")
ratio:=value/height
w:=floor(width*ratio)
h:=floor(height*ratio)

pBitmap := Gdip_CreateBitmap(w, h)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_DrawImage(G, pBitmapFile, 0, 0, w, h, 0, 0, Width, Height)
Gdip_SaveBitmapToFile(pBitmap, out_file)

Gdip_DisposeImage(pBitmapFile)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
}


closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
Added fixed width and height option and the area not covered by the resized image is filled_in with a chosen color (default is black) .

Needs gdip.ahk library in standard lib directory!

example image from clipboard to width 81 height 81
source_file=clipboard
convert_resize(source_file,"e:\test.png","k_fixed_width_height","81|81","0xde000000")




convert_resize(source_file,out_file,function="",value=1,color="0xff000000"){
global
If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

if (source_file="clipboard")
pBitmapFile :=Gdip_CreateBitmapFromClipboard()
else
pBitmapFile :=Gdip_CreateBitmapFromFile(source_file)

Width := Gdip_GetImageWidth(pBitmapFile), Height := Gdip_GetImageHeight(pBitmapFile)
ratio=1
if (function = "k_ratio")
{
ratio:=value
w:=width*ratio
h:=height*ratio
}
if (function = "k_width")
{
ratio:=value/width
w:=width*ratio
h:=height*ratio
}
if (function = "k_height")
{
ratio:=value/height
w:=width*ratio
h:=height*ratio
}

if (function = "k_fixed_width_height")
{
stringsplit,out,value,|
wf:=out1
hf:=out2

if !wf or ! hf
{
msgbox error in value parameter for fixed width and height
Gdip_Shutdown(pToken)  
return
}

if (width>wf)
{
r1:=wf/width

  w:=wf
  h:=height*r1
  
    if (h>hf)
    {
    r2:=hf/h
    w:=w*r2
    h:=hf
    }
}
else
{
if (width<wf) and (height<hf)
{
w:=width
h:=height
}
else
{
r1:=hf/height

  h:=hf
  w:=width*r1
  
    if (w>wf)
    {
    r2:=wf/w
    w:=wf
    h:=hf*r2
    }
}
}
}

if (function = "k_fixed_width_height")
pBitmap := Gdip_CreateBitmap(wf, hf)
else
pBitmap := Gdip_CreateBitmap(w,h)

G := Gdip_GraphicsFromImage(pBitmap)

if (function = "k_fixed_width_height")
{
pbrush:=Gdip_BrushCreateSolid(color)
Gdip_FillRectangle(G, pBrush, 0, 0, wf, hf)

x:=floor((wf-w)/2)
y:=floor((hf-h)/2)
}
else
{
x=0
y=0
}



Gdip_DrawImage(G, pBitmapFile, x, y, w, h, 0, 0, Width, Height)
Gdip_SaveBitmapToFile(pBitmap, out_file)
if (function = "k_fixed_width_height")
Gdip_DeleteBrush(pBrush)
Gdip_DisposeImage(pBitmapFile)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
}
[/code]