MFC C++ BMP图片向右旋转90度示例函数 WCHAR与CHAR互转 CStringW CStringA互转

在MFC中,可以使用GDI+库来实现图像的旋转。以下是一个示例函数,展示如何将BMP图像向右旋转90度。首先,确保在项目中包含GDI+库,并在项目设置中添加#include <gdiplus.h>#pragma comment(lib, "gdiplus.lib")

cpp 复制代码
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace Gdiplus;

// 初始化GDI+
ULONG_PTR InitializeGDIPlus() {
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    return gdiplusToken;
}

// 关闭GDI+
void ShutdownGDIPlus(ULONG_PTR gdiplusToken) {
    GdiplusShutdown(gdiplusToken);
}

// 函数:将BMP图像向右旋转90度
void RotateImageRight90(const WCHAR* inputPath, const WCHAR* outputPath) {
    // 初始化GDI+
    ULONG_PTR token = InitializeGDIPlus();

    // 加载图像
    Bitmap* bitmap = new Bitmap(inputPath);

    // 旋转图像
    bitmap->RotateFlip(Rotate90FlipNone);

    // 保存图像
    CLSID bmpClsid;
    CLSIDFromString(L"{557CF400-1A04-11D3-9A73-0000F81EF32E}", &bmpClsid);
    bitmap->Save(outputPath, &bmpClsid, NULL);

    // 清理资源
    delete bitmap;
    ShutdownGDIPlus(token);
}

int main() {
    // 调用函数
    RotateImageRight90(L"D:\\path\\to\\input.bmp", L"D:\\path\\to\\output.bmp");
    return 0;
}

说明:

  1. 初始化和关闭GDI+ :使用GdiplusStartupGdiplusShutdown来管理GDI+资源。
  2. 加载和保存图像 :使用Bitmap类从文件加载图像,并使用Save方法保存旋转后的图像。
  3. 旋转图像RotateFlip方法用于旋转图像。Rotate90FlipNone参数表示图像向右旋转90度,不进行翻转。
  4. CLSID:用于指定保存图像的格式,这里使用的是BMP格式的CLSID。

确保在实际应用中正确设置文件路径,并处理可能的错误(例如文件不存在或读取错误)。

CStringW CStringA互转

cpp 复制代码
//
// CStringA转CStringW
//
CStringW CStrA2CStrW(const CStringA& cstrSrcA)
{
	int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len];
	memset(wstr, 0, len * sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len);
	CStringW cstrDestW = wstr;
	delete[] wstr;

	return cstrDestW;
}

//
// CStringW转CStringA
//
CStringA CStrW2CStrA(const CStringW& cstrSrcW)
{
	int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);
	char* str = new char[len];
	memset(str, 0, len);
	WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);
	CStringA cstrDestA = str;
	delete[] str;
	return cstrDestA;
}

WCHAR与CHAR互转

在Windows编程中,经常需要在WCHAR(宽字符,通常用于Unicode字符串)和CHAR(单字节字符,通常用于ANSI字符串)之间进行转换。以下是两个函数,分别用于实现这两种转换:

1. WCHAR 到 CHAR 的转换

cpp 复制代码
#include <windows.h>

void WCHARToCHAR(const WCHAR* input, CHAR* output, int outputSize) {
    // 使用WideCharToMultiByte函数进行转换
    WideCharToMultiByte(CP_ACP, 0, input, -1, output, outputSize, NULL, NULL);
}

2. CHAR 到 WCHAR 的转换

cpp 复制代码
#include <windows.h>

void CHARToWCHAR(const CHAR* input, WCHAR* output, int outputSize) {
    // 使用MultiByteToWideChar函数进行转换
    MultiByteToWideChar(CP_ACP, 0, input, -1, output, outputSize);
}

使用示例

cpp 复制代码
int main() {
    // WCHAR 到 CHAR
    WCHAR wText[] = L"Hello, World!";
    CHAR cText[50];
    WCHARToCHAR(wText, cText, sizeof(cText));
    printf("Converted to CHAR: %s\n", cText);

    // CHAR 到 WCHAR
    CHAR aText[] = "Hello, World!";
    WCHAR wConvertedText[50];
    CHARToWCHAR(aText, wConvertedText, sizeof(wConvertedText)/sizeof(wConvertedText[0]));
    wprintf(L"Converted to WCHAR: %ls\n", wConvertedText);

    return 0;
}

注意事项

  • 转换函数WideCharToMultiByteMultiByteToWideChar 是Windows API中用于字符编码转换的函数。
  • 代码页 :这里使用的是CP_ACP(ANSI Code Page),它表示当前系统的默认Windows ANSI代码页。如果需要支持全球语言字符,可以考虑使用CP_UTF8
  • 输出缓冲区大小:确保为输出字符串分配足够的空间,以避免缓冲区溢出。
  • 错误处理:在实际应用中,应检查这些函数的返回值以处理可能的错误(例如,当输出缓冲区太小或输入字符串包含无法转换的字符时)。
相关推荐
CodeWithMe18 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found28 分钟前
Day46 Python打卡训练营
开发语言·python
love530love30 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月31 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华235 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen37 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰37 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心39 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程42 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
whoarethenext1 小时前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv