4月162015
图片分割器
如题,将图片等分为若干份。
可以横向切割,也可纵向切割。
如:将一张图片分割成横切4刀,纵切4刀,即为16幅等宽等高的小图。
本意是将前几天发的那个拼数字游戏加上图片,成为真正的拼图游戏的,现放出来大家共享。
#include <GDIPlus.au3> Opt('GUIOnEventMode', 1) Dim $w_picctrl = 498, $h_picctrl = 298 $Form1 = GUICreate('图片分割器 - 3mile', 600, 600, -1, -1, -1, 0x00000010) GUISetOnEvent(-3, '_Exit') GUISetOnEvent(-13, 'Show') GUICtrlCreateGraphic(50, 50, $w_picctrl + 2, $h_picctrl + 2) GUICtrlSetGraphic(-1, 10, 0, 0, $w_picctrl + 2, $h_picctrl + 2) $ts = GUICtrlCreateLabel('请拖拽任意图片至本框中', 51, 51, $w_picctrl, $h_picctrl, 0x01 + 0x0200) GUICtrlSetState(-1, 8) $pic = GUICtrlCreatePic('', 51, 51, 1, 1) GUICtrlCreateLabel('图片文件路径:', 50, 355, 90, 17) $ts_File = GUICtrlCreateLabel('', 140, 355, 400, 17) GUICtrlCreateLabel('图片原始尺寸:', 50, 370, 90, 17) $ts_size = GUICtrlCreateLabel('', 140, 370, 200, 17) GUICtrlCreateLabel('格式(后缀名):', 50, 385, 90, 17) $ts_hzm = GUICtrlCreateLabel('', 140, 385, 90, 17) GUICtrlCreateLabel('横向切割为:', 40, 500, 90, 40) $W_split = GUICtrlCreateCombo("", 140, 500, 60, 40) GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10") GUICtrlCreateLabel('纵向切割为:', 340, 500, 90, 40) $H_split = GUICtrlCreateCombo("", 440, 500, 60, 40) GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10") ;$Button_View = GUICtrlCreateButton("预览", 40, 550, 50, 40) ;GUICtrlSetOnEvent(-1, "_View") $Button_OK = GUICtrlCreateButton("确定", 540, 550, 50, 40) GUICtrlSetOnEvent(-1, '_OK') GUISetState() WinSetOnTop($Form1, '', 1) While 1 Sleep(10) WEnd Func _Exit() GUIDelete() Exit EndFunc ;==>_Exit Func Show() Local $imgfile = @GUI_DragFile, $hImage, $h_img _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($imgfile) $w_img = _GDIPlus_ImageGetWidth($hImage) $h_img = _GDIPlus_ImageGetHeight($hImage) If $w_img * $h_img = 0 Then _GDIPlus_Shutdown() Return MsgBox(48, @error, '非图片格式', '', $Form1) EndIf GUICtrlSetData($ts, '') GUICtrlSetData($ts_size, $w_img & ' x ' & $h_img) GUICtrlSetData($ts_File, $imgfile) GUICtrlSetData($ts_hzm, StringRegExpReplace($imgfile, '.+\\.+\.', '')) Local $kgb = $h_picctrl / $w_picctrl Local $kgb1 = $h_img / $w_img Local $x_py = 0, $y_py = 0, $hBMP, $hObject If $w_img > $w_picctrl Or $h_img > $h_picctrl Then If $kgb1 > $kgb Then $h_img = $h_picctrl $w_img = Round(1 / $kgb1 * $h_img) $x_py = ($w_picctrl - $w_img) / 2 Else $w_img = $w_picctrl $h_img = Round($kgb1 * $w_img) $y_py = ($h_picctrl - $h_img) / 2 EndIf Else $x_py = ($w_picctrl - $w_img) / 2 $y_py = ($h_picctrl - $h_img) / 2 EndIf $hBMP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage) $hObject = GUICtrlSendMsg($pic, 0x0172, 0, $hBMP) _WinAPI_DeleteObject($hObject) _GDIPlus_ImageDispose($hImage) _WinAPI_DeleteObject($hBMP) _GDIPlus_Shutdown() GUICtrlSetPos($pic, 51 + $x_py - 1, 51 + $y_py - 1, $w_img, $h_img) GUICtrlSetPos($pic, 51 + $x_py, 51 + $y_py) EndFunc ;==>Show Func _View() Local $filename = GUICtrlRead($ts_File) If $filename <> "" Then $W_Combo = GUICtrlRead($W_split) If $W_Combo = "" Then Return MsgBox(4096, "警告", "横向未选择") $H_Combo = GUICtrlRead($H_split) If $H_Combo = "" Then Return MsgBox(4096, "警告", "纵向未选择") Else Return MsgBox(4096, 0, "请首先拖入文件") EndIf EndFunc ;==>_View Func _OK() Local $filename = GUICtrlRead($ts_File) If $filename <> "" Then $W_Combo = GUICtrlRead($W_split) If $W_Combo = "" Then Return MsgBox(4096, "警告", "横向未选择") $H_Combo = GUICtrlRead($H_split) If $H_Combo = "" Then Return MsgBox(4096, "警告", "纵向未选择") _Main($filename,$W_Combo,$H_Combo) MsgBox(4096,"恭喜","图片分割完成") Else Return MsgBox(4096, 0, "请首先拖入文件") EndIf EndFunc ;==>_OK Func _Main($Bmpfile,$Width,$Height) If Not FileExists($Bmpfile) Then Return SetError(1,0,"") If $width<2 And $Height<2 Then Return SetError(2,0,"") Local $hBitmap, $hClone, $hImage, $iX, $iY,$z Local $szDrive, $szDir, $szFName, $szExt $Bmpfile=FileGetShortName($Bmpfile) $TestPath = _PathSplit($Bmpfile, $szDrive, $szDir, $szFName, $szExt) $path=$TestPath[1]&$TestPath[2] _GDIPlus_StartUp () $hImage = _GDIPlus_ImageLoadFromFile($Bmpfile) $iX = _GDIPlus_ImageGetWidth ($hImage) $iY = _GDIPlus_ImageGetHeight ($hImage) $z=0 For $y=1 To $Height For $x=1 To $Width $hClone = _GDIPlus_BitmapCloneArea ($hImage, ($x-1)*($ix/$Width), ($y-1)*($iy/$Height), $iX/$Width, $iY/$Height, $GDIP_PXF24RGB) _GDIPlus_ImageSaveToFile ($hClone, $path&$z+1&".bmp") $z+=1 Next Next _GDIPlus_ImageDispose ($hClone) _GDIPlus_ImageDispose ($hImage) _WinAPI_DeleteObject ($hBitmap) _GDIPlus_ShutDown () EndFunc ;==>_Main Func _PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt) ; Set local strings to null (We use local strings in case one of the arguments is the same variable) Local $drive = "" Local $dir = "" Local $fname = "" Local $ext = "" Local $pos ; Create an array which will be filled and returned later Local $array[5] $array[0] = $szPath; $szPath can get destroyed, so it needs set now ; Get drive letter if present (Can be a UNC server) If StringMid($szPath, 2, 1) = ":" Then $drive = StringLeft($szPath, 2) $szPath = StringTrimLeft($szPath, 2) ElseIf StringLeft($szPath, 2) = "\\" Then $szPath = StringTrimLeft($szPath, 2) ; Trim the \\ $pos = StringInStr($szPath, "\") If $pos = 0 Then $pos = StringInStr($szPath, "/") If $pos = 0 Then $drive = "\\" & $szPath; Prepend the \\ we stripped earlier $szPath = ""; Set to null because the whole path was just the UNC server name Else $drive = "\\" & StringLeft($szPath, $pos - 1) ; Prepend the \\ we stripped earlier $szPath = StringTrimLeft($szPath, $pos - 1) EndIf EndIf ; Set the directory and file name if present Local $nPosForward = StringInStr($szPath, "/", 0, -1) Local $nPosBackward = StringInStr($szPath, "\", 0, -1) If $nPosForward >= $nPosBackward Then $pos = $nPosForward Else $pos = $nPosBackward EndIf $dir = StringLeft($szPath, $pos) $fname = StringRight($szPath, StringLen($szPath) - $pos) ; If $szDir wasn't set, then the whole path must just be a file, so set the filename If StringLen($dir) = 0 Then $fname = $szPath $pos = StringInStr($fname, ".", 0, -1) If $pos Then $ext = StringRight($fname, StringLen($fname) - ($pos - 1)) $fname = StringLeft($fname, $pos - 1) EndIf ; Set the strings and array to what we found $szDrive = $drive $szDir = $dir $szFName = $fname $szExt = $ext $array[1] = $drive $array[2] = $dir $array[3] = $fname $array[4] = $ext Return $array EndFunc ;==>_PathSplit```
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!