MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / LoadPicture

Function LoadPicture

Source/util.cpp:1172–1466  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1170
1171
1172HBITMAP LoadPicture(char *aFilespec, int aWidth, int aHeight, int &aImageType, int aIconIndex, bool aUseGDIPlusIfAvailable)
1173// Loads a JPG/GIF/BMP/ICO/etc. and returns an HBITMAP or HICON to the caller (which it may call
1174// DeleteObject()/DestroyIcon() upon, though upon program termination all such handles are freed
1175// automatically). The image is scaled to the specified width and height. If zero is specified
1176// for either, the image's actual size will be used for that dimension. If -1 is specified for one,
1177// that dimension will be kept proportional to the other dimension's size so that the original aspect
1178// ratio is retained. Caller should specify -1 for aIconIndex unless an icon other than 1 is desired.
1179// .ico/.cur/.ani files are normally loaded as HICON (unless aUseGDIPlusIfAvailable is true of something
1180// else unusual happened such as file contents not matching file's extension). This is done to preserve
1181// any properties that HICONs have but HBITMAPs lack, namely the ability to be animated and perhaps other things.
1182// Returns NULL on failure.
1183{
1184 HBITMAP hbitmap = NULL;
1185 aImageType = -1; // The type of image currently inside hbitmap. Set default value for output parameter as "unknown".
1186
1187 char *file_ext = strrchr(aFilespec, '.');
1188 if (file_ext)
1189 ++file_ext;
1190
1191 // Must use ExtractIcon() if either of the following is true:
1192 // 1) Caller gave an explicit icon index, i.e. it wants us to use ExtractIcon() even for the first icon.
1193 // 2) The target file is an EXE or DLL (LoadImage() is documented not to work on those file types).
1194 bool ExtractIcon_was_used = aIconIndex >= 0 || (file_ext && (!stricmp(file_ext, "exe") || !stricmp(file_ext, "dll")));
1195 if (ExtractIcon_was_used)
1196 {
1197 aImageType = IMAGE_ICON;
1198 if (aIconIndex < 0)
1199 aIconIndex = 0; // Use the default, which is the first icon.
1200 hbitmap = (HBITMAP)ExtractIcon(g_hInstance, aFilespec, aIconIndex); // Return value of 1 means "incorrect file type".
1201 if (!hbitmap || hbitmap == (HBITMAP)1 || (!aWidth && !aHeight)) // Couldn't load icon, or could but no resizing is needed.
1202 return hbitmap;
1203 //else continue on below so that the icon can be resized to the caller's specified dimensions.
1204 }
1205
1206 // Make an initial guess of the type of image if the above didn't already determine the type:
1207 if (aImageType < 0)
1208 {
1209 if (file_ext) // Assume generic file-loading method if there's no file extension.
1210 {
1211 if (!stricmp(file_ext, "ico"))
1212 aImageType = IMAGE_ICON;
1213 else if (!stricmp(file_ext, "cur") || !stricmp(file_ext, "ani"))
1214 aImageType = IMAGE_CURSOR;
1215 else if (!stricmp(file_ext, "bmp"))
1216 aImageType = IMAGE_BITMAP;
1217 //else for other extensions, leave set to "unknown" so that the below knows to use IPic or GDI+ to load it.
1218 }
1219 //else same comment as above.
1220 }
1221
1222 if ((aWidth == -1 || aHeight == -1) && (!aWidth || !aHeight))
1223 aWidth = aHeight = 0; // i.e. One dimension is zero and the other is -1, which resolves to the same as "keep original size".
1224 bool keep_aspect_ratio = (aWidth == -1 || aHeight == -1);
1225
1226 HINSTANCE hinstGDI = NULL;
1227 if (aUseGDIPlusIfAvailable && !(hinstGDI = LoadLibrary("GdiPlus.dll")))
1228 aUseGDIPlusIfAvailable = false; // Override this value as a signal for the section below.
1229

Callers 3

GuiControlMethod · 0.85
AddControlMethod · 0.85
ImageSearchMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected