direct2d draw bitmap
d2d 画图
环境:Windows 8 Pro +Visual Studio Ultimate 2012
http://www.cnblogs.com/graphics/archive/2013/02/27/2764869.html
// D2DImageExample.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "D2DImageExample.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_D2DIMAGEEXAMPLE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_D2DIMAGEEXAMPLE));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
HRESULT CreateDeviceResources(HWND hWnd)
{
HRESULT hr=S_OK;
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&m_pDirect2DFactory
);
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_pWICFactory)
);
}
if(SUCCEEDED(hr))
{
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&m_pWriteFactory)
);
}
if(SUCCEEDED(hr))
{
hr=m_pWriteFactory->CreateTextFormat(
L"Gabriola",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
60.f,
L"en-us",
&m_pWriteTextFormat
);
}
RECT rc;
GetClientRect(hWnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
if(SUCCEEDED(hr))
{
hr = m_pDirect2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hWnd, size),
&m_pRenderTarget);
}
return hr;
}
HRESULT LoadBitmapFromFile( PCWSTR uri,UINT width,UINT height,ID2D1Bitmap **ppBitmap)
{
HRESULT hr=S_OK;
if(width<=0 ||height<=0) return S_FALSE;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
hr = m_pWICFactory->CreateDecoderFromFilename(uri,NULL,GENERIC_READ,WICDecodeMetadataCacheOnLoad,&pDecoder);
if(SUCCEEDED(hr))
{
hr = pDecoder->GetFrame(0,&pSource);
}
UINT originalWidth, originalHeight;
if(SUCCEEDED(hr))
{
pSource->GetSize(&originalWidth,&originalHeight);
}
if (SUCCEEDED(hr))
{
double rate= originalWidth/(double)originalHeight;
if(width>height)
{
width=static_cast<UINT>(height*rate);
}
else
{
height=static_cast<UINT>(width/rate);
}
hr =m_pWICFactory->CreateBitmapScaler(&pScaler);
if(SUCCEEDED(hr))
{
hr = pScaler->Initialize(
pSource,
width,
height,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr))
{
hr = m_pWICFactory->CreateFormatConverter(&pConverter);
}
if(SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
if(SUCCEEDED(hr))
{
hr= m_pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}
}
SafeRelease(&pDecoder);
SafeRelease(&pSource);
SafeRelease(&pStream);
SafeRelease(&pConverter);
SafeRelease(&pScaler);
return hr;
}
VOID WriteText(){
ID2D1SolidColorBrush * pForegroundBrush;
m_pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red,1.f),&pForegroundBrush);
m_pRenderTarget->BeginDraw();
//m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White,0.f));
wchar_t text[]=L"Direct2d,^_^";
m_pRenderTarget->DrawTextW(
text,
wcslen(text),
m_pWriteTextFormat,
D2D1::RectF(0,0,500,200),
pForegroundBrush,
D2D1_DRAW_TEXT_OPTIONS_NONE,
DWRITE_MEASURING_MODE_NATURAL);
//m_pRenderTarget->SetTransform(
m_pRenderTarget->EndDraw();
SafeRelease(&pForegroundBrush);
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_D2DIMAGEEXAMPLE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_D2DIMAGEEXAMPLE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HRESULT hr=S_OK;
RECT rect;
//ID2D1BitmapBrush* pBitmapImageBrush;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_OPEN:
MessageBox(hWnd,L"ok",L"提示",0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
CreateDeviceResources(hWnd);
GetClientRect(hWnd,&rect);
hr= LoadBitmapFromFile(L"6.jpg",rect.right-rect.left,rect.bottom-rect.top,&m_pImageBitmap);
if(SUCCEEDED(hr))
{
D2D1_SIZE_F size = m_pImageBitmap->GetSize() ;
m_pRenderTarget->BeginDraw() ;
// Clear background color to dark cyan
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
/* hr=m_pRenderTarget->CreateBitmapBrush(
m_pImageBitmap,
&pBitmapImageBrush
);
m_pRenderTarget->FillRectangle(
D2D1::RectF(0,0,size.width,size.height),
pBitmapImageBrush
);*/
m_pRenderTarget->DrawBitmap(
m_pImageBitmap,
D2D1::RectF(
0,
0,
size.width,
size.height)
) ;
hr = m_pRenderTarget->EndDraw() ;
// SafeRelease(&pBitmapImageBrush);
if (FAILED(hr))
{
MessageBox(NULL, L"Draw failed!", L"Error", 0) ;
}
}
WriteText();
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
http://www.cnblogs.com/youhui/articles/2989287.html