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
  • 输出缓冲区大小:确保为输出字符串分配足够的空间,以避免缓冲区溢出。
  • 错误处理:在实际应用中,应检查这些函数的返回值以处理可能的错误(例如,当输出缓冲区太小或输入字符串包含无法转换的字符时)。
相关推荐
Cyanto25 分钟前
Spring注解IoC与JUnit整合实战
java·开发语言·spring·mybatis
写不出来就跑路32 分钟前
WebClient与HTTPInterface远程调用对比
java·开发语言·后端·spring·springboot
悠哉清闲43 分钟前
C++ MediaCodec H264解码
开发语言·c++
张人玉44 分钟前
c#中Random类、DateTime类、String类
开发语言·c#
Jinkxs1 小时前
JavaScript性能优化实战技术
开发语言·javascript·性能优化
ydm_ymz2 小时前
C语言初阶4-数组
c语言·开发语言
presenttttt3 小时前
用Python和OpenCV从零搭建一个完整的双目视觉系统(六 最终篇)
开发语言·python·opencv·计算机视觉
逐花归海.3 小时前
『 C++ 入门到放弃 』- 多态
开发语言·c++·笔记·程序人生
卜锦元3 小时前
Go中使用wire进行统一依赖注入管理
开发语言·后端·golang
卡卡_R-Python4 小时前
C++编程基础
c++