#include "StdAfx.h" #include <comdef.h> #include <windows.h> #ifndef ULONG_PTR #define ULONG_PTR unsigned long #endif // ULONG_PTR #include <gdiplus/GdiPlus.h> #include <iostream> using namespace Gdiplus; HWND GetConsoleHwnd(void); int GetEncoderClsid(const WCHAR* format, CLSID* pClsid); void draw(HDC hdc); int main() { ULONG_PTR gdiplusToken; GdiplusStartupInput gdiplusStartupInput; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); {//这个大括号很重要,决定了里面变量的声明周期 Bitmap bmp(500, 500); Graphics g(&bmp); Pen pen(Color(255, 255, 0, 255), 5); g.DrawArc(&pen, 100, 100, 300, 300, 0.0, 360.0); //显示 HDC hdc = GetDC(GetConsoleHwnd()); Graphics windowGraphic(hdc); windowGraphic.DrawImage(&bmp, 0, 0, 500, 500); //保存 CLSID bmpClsid; GetEncoderClsid(L"image/bmp", &bmpClsid); bmp.Save(L"qwer.bmp", &bmpClsid, NULL); } GdiplusShutdown(gdiplusToken); char a; std::cin >> a; return 0; } // 获取控制台窗口句柄 微软官方网站的程序 直接拿来用了 HWND GetConsoleHwnd(void) { #define MY_BUFSIZE 1024 // Buffer size for console window titles. HWND hwndFound; // This is what is returned to the caller. char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated char pszOldWindowTitle[MY_BUFSIZE]; // Contains original GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE); wsprintf(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId()); SetConsoleTitle(pszNewWindowTitle); Sleep(40); hwndFound = FindWindow(NULL, pszNewWindowTitle); SetConsoleTitle(pszOldWindowTitle); return (hwndFound); } int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = 0; // number of image encoders UINT size = 0; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if (size == 0) return -1; // Failure //pImageCodecInfo = (ImageCodecInfo*)(malloc(size));//C pImageCodecInfo = new ImageCodecInfo[size]; //C++ if (pImageCodecInfo == NULL) return -1; // Failure GetImageEncoders(num, size, pImageCodecInfo); for (UINT j = 0; j < num; ++j) { if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) { *pClsid = pImageCodecInfo[j].Clsid; //free(pImageCodecInfo);//C delete[] pImageCodecInfo; return j; // Success } } //free(pImageCodecInfo);//C delete[] pImageCodecInfo; return -1; // Failure }
原文:http://blog.csdn.net/heyzol/article/details/24552833