Jul 29

在VC6.0下用GDI+画图 不指定

felix021 @ 2010-7-29 00:50 [IT » 程序设计] 评论(0) , 引用(0) , 阅读(5321) | Via 本站原创 | |
1. 下载gdi+的sdk,安装。当然,可以简化一点,从这里[ http://download.csdn.net/down/1645798/huohuo1120 ]下载gdiplus所需文件包,解压并导入到vc6(将include和lib目录加入vc6的配置中)

2. 在VC6中创建一个Win32 Application,选择“一个简单的Win32程序”。假设项目名是main

3. 在StdAfx.h中加入
#include <winbase.h>
#define UNICODE
#include <comdef.h>
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include <GdiPlus.h>
using namespace Gdiplus;
#endif

4. 在main.cpp的WinMain前面生命全局的两个GDI变量
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR          gdiplusToken;

5. 在程序退出之前的地方加上
GdiplusShutdown(gdiplusToken);

6. 画图

6.1 创建一个Bitmap对象
Bitmap *pBitmap = new Bitmap(width, height, PixelFormat24bppRGB);

6.2 从Bitmap中获取Graphics对象
Graphics *g = Graphics::FromImage(pBitmap);

6.3 使用各种东西的组合来搞g,比如Pen, Brush, Region, Rect, PointF, Font……
Pen pen_black(Color(255, 0, 0, 0), 3);
g->DrawLine(&pen_black, 0, 0, 100, 100);
更具体的说明和各种例子可以在这里找到:GDI+ SDK参考(翻译版本) http://download.csdn.net/source/642128

7. 保存文件,这个比较麻烦
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
  UINT num= 0, size= 0;

  ImageCodecInfo* pImageCodecInfo= NULL;
  GetImageEncodersSize(&num, &size);
  if(size== 0)
    return -1;
  pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
  if(pImageCodecInfo== NULL)
    return -1;

  GetImageEncoders(num, size, pImageCodecInfo);
  for(UINT j=0; j< num; ++j)
  {
    if(wcscmp(pImageCodecInfo[j].MimeType, format)== 0)
    {
      *pClsid= pImageCodecInfo[j].Clsid;
      free(pImageCodecInfo);
      return j;
    }
  }
  free(pImageCodecInfo);
  return -1;
}

......

CLSID encoder;
GetEncoderClsid(L"image/png", &encoder); // L"image/jpeg", ...
pb->Save(L"result.png", &encoder, NULL);




欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]