GDI+入门

#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
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值