绿色风's Blog
专注AutoIT(Au3)
  • 首页
  • 流●年
  • 笔●记
    • 学习随记
    • 源码示例
  • 脚●本
    • UDF(收集)
    • 工作室UDF
    • 工具●教程
    • 教程之GDI
  • 作●品
  • 下●载
  • 情怀ExcelTip
4月162015

图片分割器

作者:绿色风   发布:2015-4-16 13:54 Thursday   分类:   阅读:5458次   评论:0条  

如题,将图片等分为若干份。

可以横向切割,也可纵向切割。

如:将一张图片分割成横切4刀,纵切4刀,即为16幅等宽等高的小图。

本意是将前几天发的那个拼数字游戏加上图片,成为真正的拼图游戏的,现放出来大家共享。

1.JPG


#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```





本文固定链接: http://www.jianyiit.com/post-95.html

blogger
该日志由 绿色风 于2015-4-16 13:54 Thursday发表在 分类下。
版权所有:《绿色风's Blog》 → 《图片分割器》;
除特别标注,本博客很多文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:

扫描二维码,在手机上阅读
上一篇::系统自动登陆
下一篇:阿拉伯数字转换成中文大写数字

热门文章

相关文章

  • AU3小游戏 - 撸杆过桥
  • Au3小游戏 2048
  • 有点意思的代码-模拟混沌单摆
  • 窗体边缘阴影-示例
  • AU3键盘HOOK
取消回复

发表评论

亲,头像对么?

22 + 66 =

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


    站点统计
    • 运行时间: 20228 天
    • 日志总数: 365 篇
    • 评论数量: 6210 条
    • 微语数量: 6 条
    • 附件总量: 388 件
  • 逝出的青春

  • 打赏"绿色风"



      扫码关注本站公众号 可搜本站内容

  • Autoit V3 脚本交流群

      常驻群1:905774875
      常驻群2:40672266


  • 链接

    • AU3中文论坛
    • Excel资料库
    • 完美者博客
    • 顺网小哥'S Blog
    • 猛牛哥的博客
    • 网吧系统下载
  • 分类

    • 流●年(66)
    • 笔●记(0)
    • 脚●本(0)
    • 作品(21)
    • 学习随记(51)
    • 源码示例(68)
    • UDF(收集)(26)
    • 工作室UDF(30)
    • 工具●教程(62)
    • 教程之GDI(24)
Copyright © 2013 绿色风's Blog. Powered by emlog. Theme by 射雕天龙. 鄂ICP备2021011689号-1 鄂公网安备42102302000078号 sitemap