GDIP方法 resize 图片(1)

本文为 GDIP 图像缩放示例 :通过 GDI+ 库把图片无损缩放到指定尺寸。核心思路:用 Gdip_CreateBitmapFromFile 读取原图 → Gdip_GetImageWidth/Height 获取原图宽高 → 按比例计算新

本文为 GDIP 图像缩放示例:通过 GDI+ 库把图片无损缩放到指定尺寸。核心思路:用 Gdip_CreateBitmapFromFile 读取原图 → Gdip_GetImageWidth/Height 获取原图宽高 → 按比例计算新尺寸(PreserveAspectRatio 保留纵横比)→ Gdip_CreateBitmap 新建位图 → Gdip_DrawImage 绘制 → Gdip_SaveBitmapToFile 保存。调用示例:ResConImg(原图路径, 新宽, 新高, 新名, "扩展名", 输出目录),例如 ResConImg("D:\photos\a.jpg", 3000, 2000, "111", "jpg", "d:") 会把图片缩放到 3000×2000 并存为 d:\111.jpg。该函数来自 AutoHotkey 官方论坛 GDI+ 库封装。

版本注意:本示例为 AHK v1 写法。在 AHK v2 中,需改用 Gdip_Startup/Gdip_CreateBitmapFromFile(v2 的 GDI+ 库 API 基本一致),但 MsgBox、SplitPath 等命令语法需迁移为函数式调用;也可直接使用较新的 GDI+ 绑定库或 System.Drawing 方案。核心缩放思路在 v1/v2 下通用。

If !pToken := Gdip_Startup()
{
    MsgBox, 16, Gdiplus Error, 老大,你不知道GDIP?请先了解下。
    ExitApp
}
ResConImg("D:照片井冈山1IMG_1554.JPG", 3000,2000, "111", "jpg", "d:")
Gdip_Shutdown(pToken)  ; Close Gdip

; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=2505#
ResConImg(OriginalFile, NewWidth:="", NewHeight:="", NewName:="", NewExt:="", NewDir:="", PreserveAspectRatio:=true, BitDepth:=24)
{
    SplitPath, OriginalFile, SplitFileName, SplitDir, SplitExtension, SplitNameNoExt, SplitDrive
    pBitmapFile := Gdip_CreateBitmapFromFile(OriginalFile)                  ; Get the bitmap of the original file
    Width := Gdip_GetImageWidth(pBitmapFile)                                ; Original width
    Height := Gdip_GetImageHeight(pBitmapFile)                              ; Original height
    NewWidth := NewWidth ? NewWidth : Width
    NewHeight := NewHeight ? NewHeight : Height
    NewExt := NewExt ? NewExt : SplitExtension
    if SubStr(NewExt, 1, 1) != "."                                          ; Add the "." to the extension if required
        NewExt := "." NewExt
    NewPath := ((NewDir != "") ? NewDir : SplitDir)                         ; NewPath := Directory
            . "" ((NewName != "") ? NewName : "Resized_" SplitNameNoExt)       ; File name
            . NewExt                                                            ; .Extension
    if (PreserveAspectRatio) {                                              ; Recalcultate NewWidth/NewHeight if required
        if ((r1 := Width / NewWidth) > (r2 := Height / NewHeight))          ; NewWidth/NewHeight will be treated as max width/height
            NewHeight := Height / r1
        else
            NewWidth := Width / r2
    }
    pBitmap := Gdip_CreateBitmap(NewWidth, NewHeight                        ; Create a new bitmap
    , (SubStr(NewExt, -2) = "bmp" && BitDepth = 24) ? 0x21808 : 0x26200A)   ; .bmp files use a bit depth of 24 by default

    G := Gdip_GraphicsFromImage(pBitmap)                                    ; Get a pointer to the graphics of the bitmap
    Gdip_SetSmoothingMode(G, 4)                                             ; Quality settings
    Gdip_SetInterpolationMode(G, 7)
    Gdip_DrawImage(G, pBitmapFile, 0, 0, NewWidth, NewHeight)               ; Draw the original image onto the new bitmap
    Gdip_DisposeImage(pBitmapFile)                                          ; Delete the bitmap of the original image
    Gdip_SaveBitmapToFile(pBitmap, NewPath)                                 ; Save the new bitmap to file
    Gdip_DisposeImage(pBitmap)                                              ; Delete the new bitmap
    Gdip_DeleteGraphics(G)                                                  ; The graphics may now be deleted
}

使用前提:首次使用前需加载 GDI+ 库(gdip.ahk 及其依赖),并确保目标目录存在。缩放后若需保留原图纵横比,保持 PreserveAspectRatio:=true(默认)即可,此时指定的宽高会被当作最大宽高。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
教程

GDIP例子进度条

2020-3-29 2:04:52

教程

GDI画画

2020-3-29 2:21:29

❯
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索