你的问题是混用了不同的字符集。项目中建议使用相同的字符集。
你定义的 name 字符串数组是 char 类型,而保存图片的时候使用的参数是 _T 类型。
解决办法有三个,要么都用 MBCS 字符集,要么都用 Unicode 字符集,要么都用自适应字符集。针对你的代码,可以做如下修改:
使用 MBCS 字符集:
// 需要设置项目字符集为 MBCS
//
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
HWND hwnd = NULL;
int main()
{
hwnd = initgraph(240, 240);
char name[][10] = { "YEL.png", "RED.png", "GREEN.png", "BLUE.png" };
int col[4] = { YELLOW, RED, GREEN, BLUE };
for (int i = 0; i < 4; i++)
{
cleardevice();
setlinecolor(col[i]);
setfillcolor(col[i]);
fillrectangle(0, 0, 240, 240);
saveimage(name[i]);
}
_getch();
return 0;
}
使用 Unicode 字符集:
// 需要设置项目字符集为 Unicode
//
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
HWND hwnd = NULL;
int main()
{
hwnd = initgraph(240, 240);
wchar_t name[][10] = { L"YEL.png", L"RED.png", L"GREEN.png", L"BLUE.png" };
int col[4] = { YELLOW, RED, GREEN, BLUE };
for (int i = 0; i < 4; i++)
{
cleardevice();
setlinecolor(col[i]);
setfillcolor(col[i]);
fillrectangle(0, 0, 240, 240);
saveimage(name[i]);
}
_getch();
return 0;
}
使用自适应字符集:
// 无论项目字符集如何设置都可以使用
//
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
HWND hwnd = NULL;
int main()
{
hwnd = initgraph(240, 240);
TCHAR name[][10] = { _T("YEL.png"), _T("RED.png"), _T("GREEN.png"), _T("BLUE.png") };
int col[4] = { YELLOW, RED, GREEN, BLUE };
for (int i = 0; i < 4; i++)
{
cleardevice();
setlinecolor(col[i]);
setfillcolor(col[i]);
fillrectangle(0, 0, 240, 240);
saveimage(name[i]);
}
_getch();
return 0;
}
关于 Unicode 字符集的优势,请参考:https://codebus.cn/yangw/about-unicode