将长图片按A4规格剪裁为1列或2列
拖动图片到窗体上。
代码最后,请
#Include Gdip_All.ahk
A4Scale:=1.4143
Label_DrawGui: ;{ ; 绘制窗体
try
Gui,Destroy
Gui New
Gui +AlwaysOnTop
Gui Add, checkbox, x30 y40 w80 h30 vMyV_2Col,A4双列
Gui Show, w270 h120 , 长图片按A4规格剪裁为1列或2列
Return
;}
GuiDropFiles: ;{主窗体:拖动文件到主窗体
GuiControlGet,MyV_2Col,,MyV_2Col
; 初始化 GDI+ 。要用到 GDI+ 的各种功能,必须先初始化!检测一下初始化是否成功,失败就弹窗告知,并退出程序。
If !pToken := Gdip_Startup(){
MsgBox, 48, 错误, Gdi`+初始化失败!
ExitApp
}
; 设置程序结束时,要跳转到名为 GdipExit 的标签去运行。通常在那里执行释放资源以及关闭 GDI+ 等收尾操作。
OnExit, GdipExit
MySel=%A_GuiEvent%
Loop,parse,MySel,`n,`r
{
if (A_LoopField="")
continue
GWidth:=0
GHeight:=0
pBitmap1 := Gdip_CreateBitmapFromFile(A_LoopField)
If !pBitmap1
{
MsgBox, 48, 文件载入错误, % "文件不能载入"
return
}
PicWidth1 := Gdip_GetImageWidth(pBitmap1), PicHeight1 := Gdip_GetImageHeight(pBitmap1)
GWidth:=PicWidth1
GHeight:=floor(PicWidth1*A4Scale)
; 创建画布
gBitmap := Gdip_CreateBitmap(GWidth, GHeight)
G := Gdip_GraphicsFromImage(gBitmap)
TotalHeight:=0
SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
if (MyV_2Col!=1) {
Loop
{
NowY:=0
NowX:=0
NowWidth:=GWidth
FromX:=0
FromY:=TotalHeight
FromWidth:=GWidth
if (TotalHeight+GHeight>=PicHeight1){
FromHeight:=PicHeight1-TotalHeight
NowHeight:=FromHeight
} else {
FromHeight:=GHeight
NowHeight:=GHeight
}
; 使用画刷将背景画为白色
pBrush := Gdip_BrushCreateSolid(0xffffffff)
Gdip_FillRectangle(G, pBrush, 0, 0, GWidth, GHeight)
Gdip_DeleteBrush(pBrush)
Gdip_DrawImage(G, pBitmap1, NowX, NowY, NowWidth, NowHeight , FromX, FromY, FromWidth, FromHeight)
savefilename:= MyOutDir . "\" . MyOutNameNoExt . "_" . substr("000" . A_Index,-1) . "." . MyOutExt
;~ msgbox % savefilename
Gdip_SaveBitmapToFile(gBitmap, savefilename)
if (TotalHeight+GHeight>=PicHeight1)
break
TotalHeight:=TotalHeight+GHeight
}
} else {
iIndex:=1
Loop
{
; 使用画刷将背景画为白色
pBrush := Gdip_BrushCreateSolid(0xffffffff)
Gdip_FillRectangle(G, pBrush, 0, 0, GWidth, GHeight)
Gdip_DeleteBrush(pBrush)
;计算左侧坐标
NowX1:=0
NowY1:=0
NowWidth1:=floor(GWidth/2)
FromX1:=0
FromY1:=TotalHeight
FromWidth1:=GWidth
if (TotalHeight+GHeight*2>=PicHeight1){
FromHeight1:=PicHeight1-TotalHeight
NowHeight1:=floor(FromHeight2/2)
} else {
FromHeight1:=GHeight*2
NowHeight1:=GHeight
}
Gdip_DrawImage(G, pBitmap1, NowX1, NowY1, NowWidth1, NowHeight1 , FromX1, FromY1, FromWidth1, FromHeight1)
if (TotalHeight+GHeight*2>=PicHeight1){
savefilename:= MyOutDir . "\" . MyOutNameNoExt . "_" . substr("000" . iIndex,-1) . "." . MyOutExt
;~ msgbox % savefilename
Gdip_SaveBitmapToFile(gBitmap, savefilename)
break
}
TotalHeight:=TotalHeight+GHeight*2
;计算右侧坐标
NowX2:=floor(GWidth/2)
NowY2:=0
NowWidth2:=floor(GWidth/2)
FromX2:=0
FromY2:=TotalHeight
FromWidth2:=GWidth
if (TotalHeight+GHeight*2>=PicHeight1){
FromHeight2:=PicHeight1-TotalHeight
NowHeight2:=floor(FromHeight2/2)
} else {
FromHeight2:=GHeight*2
NowHeight2:=GHeight
}
Gdip_DrawImage(G, pBitmap1, NowX2, NowY2, NowWidth2, NowHeight2 , FromX2, FromY2, FromWidth2, FromHeight2)
if (TotalHeight+GHeight*2>=PicHeight1){
savefilename:= MyOutDir . "\" . MyOutNameNoExt . "_" . substr("000" . iIndex,-1) . "." . MyOutExt
;~ msgbox % savefilename
Gdip_SaveBitmapToFile(gBitmap, savefilename)
break
}
savefilename:= MyOutDir . "\" . MyOutNameNoExt . "_" . substr("000" . iIndex,-1) . "." . MyOutExt
;~ msgbox % savefilename
Gdip_SaveBitmapToFile(gBitmap, savefilename)
iIndex+=1
TotalHeight:=TotalHeight+GHeight*2
}
}
; 释放图片的资源
Gdip_DisposeImage(gBitmap)
; 删除画布。
Gdip_DeleteGraphics(G)
; 释放图片的资源
Gdip_DisposeImage(pBitmap1)
}
MsgBox, 64, 提示, 处理完成!,0.8
run,%MyOutDir%
return
;}
GuiClose:
GuiEscape:
GdipExit:
; 别忘了,我们最开始用 Gdip_Startup() 启动了,这里对应要用 Gdip_Shutdown() 关闭它。
Gdip_Shutdown(pToken)
ExitApp
Return
; 加载 GDI+ 库。
#Include Gdip_All.ahk