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
  • 输出缓冲区大小:确保为输出字符串分配足够的空间,以避免缓冲区溢出。
  • 错误处理:在实际应用中,应检查这些函数的返回值以处理可能的错误(例如,当输出缓冲区太小或输入字符串包含无法转换的字符时)。
相关推荐
_wyt00112 小时前
多重背包问题详解
c++·背包dp
段一凡-华北理工大学12 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe13 小时前
C++——多态
开发语言·c++
越甲八千13 小时前
STL stack为何没有迭代器
c++
心平气和量大福大13 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
从零开始的代码生活_15 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸15 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
GIS阵地16 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis
王维同学16 小时前
[原创][Windows C++]LSA 认证、安全与通知包的注册表枚举
c++·windows·安全
程序员Rock16 小时前
上位机开发-MODBUS面试常见问题
c语言·c++·面试·职场和发展·上位机