前6章里,我们把 GDI+ 中最常用的绘画工具都演示了一遍。此时就有一个疑问了,我想保存画好的东西该怎么办呢?
本章,我们会将两张图片内容合并在一起,并保存为一个新文件。
1.使用定式直接创建出画布
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip_All.ahk
; 初始化 gdi+ 需要的一切
gosub, init
2.将背景画为绿色
; Create a green brush (this will be used to fill the background with green). The brush is fully opaque (ARGB)
; Filll the entire graphics of the bitmap with the green brush (this will be out background colour)
; Delete the brush created to save memory as we don't need the same brush anymore
; 使用画刷将背景画为绿色
pBrush := Gdip_BrushCreateSolid(0xff00ff00)
Gdip_FillRectangle(G, pBrush, 0, 0, 600, 600)
Gdip_DeleteBrush(pBrush)
3.载入两张素材
; Get bitmaps for both the files we are going to be working with
; 从本地图片文件创建两个 pBitmap
pBitmapFile1 := Gdip_CreateBitmapFromFile(File1), pBitmapFile2 := Gdip_CreateBitmapFromFile(File2)
4.把素材贴画布上
; Get the width and height of the 1st bitmap
; Draw the 1st bitmap (1st image) onto our "canvas" (the graphics of the original bitmap we created) with the same height and same width
; at coordinates (100,160).....We will be ignoring the matrix parameter for now. This can be used to change opacity and colours when drawing
; 把图1画上来
Width := Gdip_GetImageWidth(pBitmapFile1), Height := Gdip_GetImageHeight(pBitmapFile1)
Gdip_DrawImage(G, pBitmapFile1, 100, 160, Width, Height, 0, 0, Width, Height)
; Do the same again for the 2nd file, but change the coordinates to (220,100).....
; 把图2也画上来
Width := Gdip_GetImageWidth(pBitmapFile2), Height := Gdip_GetImageHeight(pBitmapFile2)
Gdip_DrawImage(G, pBitmapFile2, 220, 100, Width, Height, 0, 0, Width, Height)
; Dispose of both of these bitmaps we created from the images on disk, as they are now been used...They are on
; the graphics of the bitmap of our created "canvas"
; 释放两张图片的资源
Gdip_DisposeImage(pBitmapFile1), Gdip_DisposeImage(pBitmapFile2)
5.保存为文件,并打开
; Save the bitmap to file "File.png" (extension can be .png,.bmp,.jpg,.tiff,.gif)
; Bear in mind transparencies may be lost with some image formats and will appear black
; 将画好的图保存为文件并打开
Gdip_SaveBitmapToFile(pBitmap, "FinalImage.png")
Run, FinalImage.png
Return
6.收工善后
ExitGdip:
; The bitmap can be deleted
Gdip_DisposeImage(pBitmap)
; The graphics may now be deleted
Gdip_DeleteGraphics(G)
; ...and gdi+ may now be shutdown
Gdip_Shutdown(pToken)
ExitApp
Return
0.定式
init:
; Specify both of the files we are going to use
File1 = mario.png
File2 = site_logo.gif
; If the images we want to work with do not exist on disk, then download them...
If !(FileExist(File1) && FileExist(File2))
{
MsgBox, 缺少文件 mario.png 与 site_logo.gif 。`r`n重新去教程里下载,或者也可以随便找两张背景透明的png图片改名替代看效果!
ExitApp
}
; Start gdi+
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, ExitGdip
; Create a 500x500 pixel gdi+ bitmap (this will be the entire drawing area we have to play with)
; 用指定的宽高创建一个 pBitmap
pBitmap := Gdip_CreateBitmap(600, 600)
; Get a pointer to the graphics of the bitmap, for use with drawing functions
; 创建画布
G := Gdip_GraphicsFromImage(pBitmap)
Return
本章习题:使用 Gdip_BitmapFromHWND(hwnd),将记事本窗口内容截图并保存为文件。
全部代码与库文件下载地址:
https://ahk.lanzoux.com/b01nypnuh
密码:
第一遍完成