1.在应用程序的stdafx,h中导入库的代码:
#define countof(x) (sizeof(x) / sizeof(x[0]))
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
2.在application::InitInstance()
// Initialize GDI+
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
3.application::ExitInstance()
// release gdi+
GdiplusShutdown(m_gdiplusToken);
4.在对话框Static Text显示图像。
Unit units;
CRect rect;
dynamic_cast<CWnd *>(GetDlgItem(IDC_STATIC_PREVIEW_SCANNER))->GetClientRect(&rect);
RectF destRect(REAL(rect.left), REAL(rect.top),REAL(rect.Width()),REAL(rect.Height()));
Image *image=new Image(_T("C://image//1.jpg"));
//创建任何对象,都要先错误检测一下,然后才调用它的方法。
if(OK==image->GetLastStatus())
{
image->GetBounds(&destRect, &units);
Graphics graphics(dynamic_cast<CWnd *>(GetDlgItem(IDC_STATIC_PREVIEW_SCANNER))->GetDC()->GetSafeHdc());
if(OK==graphics.GetLastStatus())
{
//PointF pointF(REAL(10),REAL(100));
graphics.DrawImage(image,destRect);
}
}
delete image;
如果要对图像操作频繁,并且要求很快。采用
Bitmap bitmap(_T("C://image//1.jpg"));
Graphics graphics(dynamic_cast<CWnd *>(GetDlgItem(IDC_STATIC_PREVIEW_SCANNER))->GetDC()->GetSafeHdc());
if(Ok==graphics.GetLastStatus())
{
CachedBitmap cachedBitmap(&bitmap, &graphics);
if(Ok==cachedBitmap.GetLastStatus())
{
graphics.DrawCachedBitmap(&cachedBitmap,rect.left,rect.top);
}
}
5.先创建一个graphicsPath,可能是一个矢量图像。调用这个对象就可以画这个图像了。就是自己创建一种图像矢量。方便反复调用,不需要调用的时候每次都要创建。
GraphicsPath.transform()可以对图像进行一些变形。
6.GDI+和C++类接口画线
HDC hdc;
PAINTSTRUCT ps;
Pen* myPen;
Graphics* myGraphics;
hdc = BeginPaint(hWnd, &ps);
myPen = new Pen(Color(255, 255, 0, 0), 3);
myGraphics = new Graphics(hdc);
myGraphics->DrawLine(myPen, 20, 10, 200, 100);
delete myGraphics;
delete myPen;
EndPaint(hWnd, &ps);
7.在指点大小的窗口显示图像,可以自动调节图像大小,以便全部显示在窗口中。
8.请注意,页面坐标空间的原点一律位于工作区的左上角。
此外,由于度量单位为像素,因此设备坐标和页面坐标是相同的。如果您将度量单位设为像素以外的单位 (例如英吋),则设备坐标便与页面坐标不同。
将世界坐标映射到页面坐标称为“世界变换”,由Graphics对象来进行。
myGraphics.TranslateTransform(
100.0f
,
50.0f
);
myGraphics.DrawLine(&myPen, 0, 0, 160, 80);
将页面坐标映射到设备坐标称为“页面变换”。Graphics类提供了4种方法用于操作和检测页面变换:SetPageUnit、GetPageUnit、SetPageScale和GetPageScale。Graphics类同时提供2个方法GetDpiX和GetDpiY,用于获取显示设备每英吋的水平点数和垂直点数。