加载图片 - LoadImage
函数简介
加载一张图片到内存,如果SetConfig配置了数据库连接则优先读取数据库内图片信息,如果数据库没有找到图片或者没有配置数据库,则从工作目录下读取指定文件。
接口名称
LoadImage
DLL调用
long LoadImage(long ola, string file_path)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。file_path
(字符串): 图片文件的完整路径。支持相对路径和绝对路径。
示例:
// 加载本地图片文件
long image = LoadImage(ola, "D:\\test\\sample.png");
if (image != 0) {
printf("图片加载成功,句柄:%ld\n", image);
// 获取图片大小
int width, height;
GetImageSize(ola, image, &width, &height);
printf("图片大小:%d x %d\n", width, height);
// 使用完后释放图片内存
FreeImagePtr(ola, image);
} else {
printf("图片加载失败\n");
}
// 加载相对路径的图片
image = LoadImage(ola, ".\\images\\button.jpg");
if (image != 0) {
printf("按钮图片加载成功\n");
// 在屏幕上查找该图片
int x = 0, y = 0;
if (FindPic(ola, 0, 0, 1920, 1080, image, &x, &y) == 1) {
printf("找到按钮位置:(%d, %d)\n", x, y);
}
FreeImagePtr(ola, image);
}
COM调用
long LoadImage(string file_path)
参数定义:
file_path
(字符串): 图片文件的完整路径。支持相对路径和绝对路径。
示例:
# 加载本地图片文件
image = ola.LoadImage("D:\\test\\sample.png")
if image != 0:
messagebox(f"图片加载成功,句柄:{image}")
# 获取图片大小
width, height = 0, 0
ola.GetImageSize(image, width, height)
messagebox(f"图片大小:{width} x {height}")
# 使用完后释放图片内存
ola.FreeImagePtr(image)
else:
messagebox("图片加载失败")
# 加载相对路径的图片
image = ola.LoadImage(".\\images\\button.jpg")
if image != 0:
messagebox("按钮图片加载成功")
# 在屏幕上查找该图片
x = y = 0
if ola.FindPic(0, 0, 1920, 1080, image, x, y) == 1:
messagebox(f"找到按钮位置:({x}, {y})")
ola.FreeImagePtr(image)
返回值
长整型数:
- 0: 加载失败
- 非0: 加载成功,返回图片句柄
注意事项
- 支持的图片格式包括:BMP、JPG、PNG
- 加载成功后必须使用 FreeImagePtr 释放图片内存,否则会造成内存泄漏
- 建议使用绝对路径以避免路径问题,如果使用相对路径,需要确保当前工作目录正确