我开发了一个小的bmp到jpg转换器。以下代码正在工作并提供准确的结果,因为我需要
BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path,const WCHAR *dest_jpeg_path);
则调用函数为,
const WCHAR *src_bmp_path = L"test.bmp";
const WCHAR *dest_jpeg_path= L"test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);
然而,我需要按照以下方式更改函数(根据我得到的要求),但这样做会导致编译错误。
BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path);
那么函数将被调用为,(尽管我只需要遵循上面的原型),
char *src_bmp_path = "test.bmp";
char *dest_jpeg_path= "test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);
关于stackover的另一个问题提供了太多关于Win32类型的信息,但我仍然无法解决这个问题。我在Win32 API方面不是很好,请指导我在以后的方法中出了什么问题。
编辑:
错误消息:错误C2664:"Gdipls::Status Gdiplus::Image::Save(const WCHAR*,const CLSID*,const Gdipls::EncoderParameters*)":无法将参数1从"char*"转换为"const WCHAR*"1> 指向的类型是不相关的;转换需要重新解释cast、C样式强制转换或函数样式强制转换
Image::Save()
只接受WCHAR*
值,因此char*
包装器必须转换为WCHAR*
,例如使用MultiByteToWideChar()
(就像Win32 API Ansi函数在内部调用Win32 API Unicode函数时所做的那样),例如:
std::wstring towstring(const char *src)
{
std::wstring output;
int src_len = strlen(src);
if (src_len > 0)
{
int out_len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
if (out_len > 0)
{
output.resize(out_len);
MultiByteToWideChar(CP_ACP, 0, src, src_len, &output[0], out_len);
}
}
return output;
}
BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path)
{
return convertBMPtoJPG(towstring(src_bmp_path).c_str(), towstring(dest_jpeg_path).c_str());
}
BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path, const WCHAR *dest_jpeg_path)
{
// your existing conversion logic here...
}
看起来您正在为Unicode支持进行编译。Win32数据类型的列表可以在这里找到
WCHAR定义为-
A 16-bit Unicode character. For more information, see Character Sets Used By Fonts.
This type is declared in WinNT.h as follows:
typedef wchar_t WCHAR;
这里有一个链接,显示如何在各种字符串类型之间转换字符串转换示例。