如何从路径字符串加载 Gdiplus::Bitmap?
How can I load a Gdiplus::Bitmap from a path string?
我在使用 GDI+ 库时遇到了问题。我想使用一个字符串变量来加载一个Bitmap
变量。我不知道怎么称呼它,因为我是这个库的新手。
我的程序只是在字符串变量中获取 image.bmp
路径:
string username()
{
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName(username, &username_len);
string pcuser = username;
return pcuser;
}
int main()
{
Gdiplus::Bitmap bmp("C:\Users\" + username() + "\Documents\Visual Studio Things\image.bmp");
return 0;
}
我尝试将 .c_str()
与 username()
一起使用,但这不起作用。有什么建议吗?
我收到此错误:
Error (active) E0289 no instance of constructor "Gdiplus::Bitmap::Bitmap" matches the argument list argument types are: (std::basic_string<char, std::char_traits<char>, std::allocator<char>>)
那么,如何使用 username()
加载 Bitmap
?
您尝试调用的 Bitmap
构造函数将 const wchar_t*
作为输入,而不是 const char*
,因此您需要使用 std::wstring
而不是 std::string
,例如:
#include <windows.h>
#include <gdiplusheaders.h>
#include <string>
wstring username()
{
wstring pcuser;
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
if (GetUserNameW(username, &username_len))
pcuser.assign(username, username_len-1);
return pcuser;
}
void doWork()
{
wstring path = L"C:\Users\" + username() + L"\Documents\Visual Studio Things\image.bmp";
Gdiplus::Bitmap bmp(path.c_str());
...
}
int main()
{
GdiplusStartupInput input;
ULONG_PTR token;
GdiplusStartup(&token, &input, NULL);
doWork();
GdiplusShutdown(token);
return 0;
}
也就是说,使用 GetUserName()
建立用户 Documents
文件夹的路径是错误的方法。用户配置文件并不总是位于每台计算机上的 C:\Users\
。用户的 Documents
文件夹并不总是位于用户的配置文件中,也不总是命名为 "Documents"
。该路径可以由用户自定义,因此它实际上可以位于机器上的任何地方。
您不应该在代码中手动建立这样的路径。 Shell API 具有 SHGetFolderPath()
and SHGetKnownFolderPath()
函数,这些函数是 专门设计的 以了解预定义系统文件夹和用户特定文件夹的位置,包括用户的 Documents
文件夹。使用那些 API 来获得 真实的 路径,不要 假设 你知道路径在哪里,有时你会错的.
例如:
#include <windows.h>
#include <Shlobj.h>
#include <shlwapi.h>
#include <gdiplusheaders.h>
#include <string>
wstring userdocs()
{
wstring pcdocs;
wchar_t path[MAX_PATH];
if (SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
{
if (PathAddBackslashW(path))
pcdocs = path;
}
return pcdocs;
}
void doWork()
{
wstring path = userdocs();
if (path.empty()) return;
path += L"Visual Studio Things\image.bmp";
Gdiplus::Bitmap bmp(path.c_str());
...
}
int main()
{
GdiplusStartupInput input;
ULONG_PTR token;
GdiplusStartup(&token, &input, NULL);
doWork();
GdiplusShutdown(token);
return 0;
}
或者:
wstring userdocs()
{
wstring pcdocs;
wchar_t *path;
if (SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path) == S_OK)
{
pcdocs = path;
pcdocs += L"\";
CoTaskMemFree(path);
}
return pcdocs;
}
我在使用 GDI+ 库时遇到了问题。我想使用一个字符串变量来加载一个Bitmap
变量。我不知道怎么称呼它,因为我是这个库的新手。
我的程序只是在字符串变量中获取 image.bmp
路径:
string username()
{
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName(username, &username_len);
string pcuser = username;
return pcuser;
}
int main()
{
Gdiplus::Bitmap bmp("C:\Users\" + username() + "\Documents\Visual Studio Things\image.bmp");
return 0;
}
我尝试将 .c_str()
与 username()
一起使用,但这不起作用。有什么建议吗?
我收到此错误:
Error (active) E0289 no instance of constructor "Gdiplus::Bitmap::Bitmap" matches the argument list argument types are: (std::basic_string<char, std::char_traits<char>, std::allocator<char>>)
那么,如何使用 username()
加载 Bitmap
?
您尝试调用的 Bitmap
构造函数将 const wchar_t*
作为输入,而不是 const char*
,因此您需要使用 std::wstring
而不是 std::string
,例如:
#include <windows.h>
#include <gdiplusheaders.h>
#include <string>
wstring username()
{
wstring pcuser;
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
if (GetUserNameW(username, &username_len))
pcuser.assign(username, username_len-1);
return pcuser;
}
void doWork()
{
wstring path = L"C:\Users\" + username() + L"\Documents\Visual Studio Things\image.bmp";
Gdiplus::Bitmap bmp(path.c_str());
...
}
int main()
{
GdiplusStartupInput input;
ULONG_PTR token;
GdiplusStartup(&token, &input, NULL);
doWork();
GdiplusShutdown(token);
return 0;
}
也就是说,使用 GetUserName()
建立用户 Documents
文件夹的路径是错误的方法。用户配置文件并不总是位于每台计算机上的 C:\Users\
。用户的 Documents
文件夹并不总是位于用户的配置文件中,也不总是命名为 "Documents"
。该路径可以由用户自定义,因此它实际上可以位于机器上的任何地方。
您不应该在代码中手动建立这样的路径。 Shell API 具有 SHGetFolderPath()
and SHGetKnownFolderPath()
函数,这些函数是 专门设计的 以了解预定义系统文件夹和用户特定文件夹的位置,包括用户的 Documents
文件夹。使用那些 API 来获得 真实的 路径,不要 假设 你知道路径在哪里,有时你会错的.
例如:
#include <windows.h>
#include <Shlobj.h>
#include <shlwapi.h>
#include <gdiplusheaders.h>
#include <string>
wstring userdocs()
{
wstring pcdocs;
wchar_t path[MAX_PATH];
if (SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
{
if (PathAddBackslashW(path))
pcdocs = path;
}
return pcdocs;
}
void doWork()
{
wstring path = userdocs();
if (path.empty()) return;
path += L"Visual Studio Things\image.bmp";
Gdiplus::Bitmap bmp(path.c_str());
...
}
int main()
{
GdiplusStartupInput input;
ULONG_PTR token;
GdiplusStartup(&token, &input, NULL);
doWork();
GdiplusShutdown(token);
return 0;
}
或者:
wstring userdocs()
{
wstring pcdocs;
wchar_t *path;
if (SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path) == S_OK)
{
pcdocs = path;
pcdocs += L"\";
CoTaskMemFree(path);
}
return pcdocs;
}