[Direct2D]图片处理

 Using Bitmap Brushes

 

 

Direct2D 中的图片处理增加了很多的灵活的特性,现在的Direct2D可以很好的和GUI,GUI+以及Direct3D混合使用,它支持更多的图形格式,更丰富的绘制方法。使用Direct2D绘制的win7程序,显示效果更好,绘制速度更快。

LRESULT InItD2D(HWND hWnd) { //创建一个Direct2D资源指针 HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory); CoInitialize(NULL); hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&p_pImageFactory) ); if(SUCCEEDED(hr)) { // Create a DirectWrite factory. hr = DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED, __uuidof(p_pDWriteFactory), reinterpret_cast(&p_pDWriteFactory) ); } if (SUCCEEDED(hr)) { // Create a DirectWrite text format object. hr = p_pDWriteFactory->CreateTextFormat( L"微软雅黑", NULL, DWRITE_FONT_WEIGHT_DEMI_BOLD, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_ULTRA_EXPANDED, 24, L"MyFont", //locale &p_pText ); } if (SUCCEEDED(hr)) { // Center the text horizontally and vertically. p_pText->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); p_pText->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); } // Obtain the size of the drawing area. GetClientRect(hWnd, &rc); // Create a Direct2D render target hr = pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd,D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)), &p_pRenderTarget); //从资源中载入一张图片 //LoadResourceBitmap(p_pRenderTarget, p_pImageFactory,MAKEINTRESOURCE(IDR_JPG1),L"jpg",&p_pBitmap); //从硬盘上载入一张图片 LoadBitmapFromFile(p_pRenderTarget,p_pImageFactory,L"car.jpg",0,0,&p_pBitmap); // 设置笔刷 if (SUCCEEDED(hr)) { p_pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black),&p_pBlackBrush); p_pRenderTarget->CreateBitmapBrush( p_pBitmap ,&p_pBitmapBrush ); } return hr; }

 

 

其中IWICImagingFactory类提供了一个高效的图形绘制接口

 

hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&p_pImageFactory) );

 

 还有两个函数LoadResourceBitmap()和LoadBitmapFromFile()都是载入图片的函数,这两个函数可以载入我们常见的图片格式,要研究这一块的内容,估计一时半会说不明白,微软现在已经通过类似于控件的方式提供了多种图片的载入方式,目前直接支持的有jpg,bmp,gif,png等。还可以按照自己的解析器来完成自己需要图片格式的读写~

 

 

 

 

VOID Rander() { if(!p_pRenderTarget) return ; static const WCHAR szBitmapBrushText[] = L"图片笔刷"; // Define the shape of rectangles to be filled with brushes. D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 470, 613); // Define the area where captions are drawn. D2D1_RECT_F rcTextRect = D2D1::RectF(0, 1065, 450, 200); p_pRenderTarget->BeginDraw(); p_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); p_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); // Translate for the bitmap brush. p_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(150, 50))); // Draw a caption. //Demonstrate a bitmap brush. p_pRenderTarget->FillRectangle(&rcBrushRect, p_pBitmapBrush); p_pRenderTarget->DrawRectangle(&rcBrushRect, p_pBlackBrush, 1, NULL); p_pRenderTarget->DrawText( szBitmapBrushText, ARRAYSIZE(szBitmapBrushText) - 1, p_pText, &rcTextRect, p_pBlackBrush ); p_pRenderTarget->EndDraw(); }

 

你可能感兴趣的:(ShaderX)