direct2D图片处理

转自:http://blog.csdn.net/augusdi/article/details/9040177

 Using Bitmap Brushes

Illustration of a square painted with plant bitmap

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

 

[cpp] view plaincopy
 
  1. LRESULT InItD2D(HWND hWnd)    
  2. {    
  3.     //创建一个Direct2D资源指针    
  4.      HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);    
  5.      CoInitialize(NULL);    
  6.     
  7.      hr = CoCreateInstance(    
  8.         CLSID_WICImagingFactory,    
  9.         NULL,    
  10.         CLSCTX_INPROC_SERVER,    
  11.         IID_PPV_ARGS(&p_pImageFactory)    
  12.         );    
  13.      if(SUCCEEDED(hr))    
  14.     {    
  15.         // Create a DirectWrite factory.    
  16.         hr = DWriteCreateFactory(    
  17.             DWRITE_FACTORY_TYPE_SHARED,    
  18.             __uuidof(p_pDWriteFactory),    
  19.             reinterpret_cast<IUnknown **>(&p_pDWriteFactory)    
  20.             );    
  21.     }    
  22.     
  23.     if (SUCCEEDED(hr))    
  24.     {    
  25.         // Create a DirectWrite text format object.    
  26.         hr = p_pDWriteFactory->CreateTextFormat(    
  27.             L"微软雅黑",    
  28.             NULL,    
  29.             DWRITE_FONT_WEIGHT_DEMI_BOLD,    
  30.             DWRITE_FONT_STYLE_NORMAL,    
  31.             DWRITE_FONT_STRETCH_ULTRA_EXPANDED,    
  32.             24,    
  33.             L"MyFont"//locale    
  34.             &p_pText    
  35.             );    
  36.     }    
  37.     
  38.     if (SUCCEEDED(hr))    
  39.     {    
  40.         // Center the text horizontally and vertically.    
  41.         p_pText->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);    
  42.     
  43.         p_pText->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);    
  44.     }    
  45.     // Obtain the size of the drawing area.    
  46.     GetClientRect(hWnd, &rc);    
  47.     
  48.     // Create a Direct2D render target              
  49.     hr = pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),    
  50.                                              D2D1::HwndRenderTargetProperties(hWnd,D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)),    
  51.                                             &p_pRenderTarget);    
  52.     
  53.     //从资源中载入一张图片    
  54.     //LoadResourceBitmap(p_pRenderTarget,   p_pImageFactory,MAKEINTRESOURCE(IDR_JPG1),L"jpg",&p_pBitmap);    
  55.         
  56.     //从硬盘上载入一张图片    
  57.     LoadBitmapFromFile(p_pRenderTarget,p_pImageFactory,L"car.jpg",0,0,&p_pBitmap);    
  58.     
  59.     // 设置笔刷    
  60.     if (SUCCEEDED(hr))    
  61.     {    
  62.         p_pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black),&p_pBlackBrush);    
  63.         p_pRenderTarget->CreateBitmapBrush(  p_pBitmap ,&p_pBitmapBrush  );    
  64.     }    
  65.     
  66.     return hr;    
  67. }   

 

 

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

 

[cpp] view plaincopy
 
  1. hr = CoCreateInstance(    
  2.        CLSID_WICImagingFactory,    
  3.        NULL,    
  4.        CLSCTX_INPROC_SERVER,    
  5.        IID_PPV_ARGS(&p_pImageFactory)    
  6.        );    

 

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

 

 

[cpp] view plaincopy
 
    1. VOID Render()    
    2. {    
    3.     if(!p_pRenderTarget)    
    4.         return ;    
    5.     
    6.     static const WCHAR szBitmapBrushText[] = L"图片笔刷";    
    7.     
    8.     // Define the shape of rectangles to be filled with brushes.     
    9.     D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 470, 613);    
    10.     
    11.     // Define the area where captions are drawn.    
    12.     D2D1_RECT_F rcTextRect = D2D1::RectF(0, 1065, 450, 200);    
    13.     
    14.     p_pRenderTarget->BeginDraw();    
    15.     p_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());    
    16.     
    17.     p_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));    
    18.     
    19.     // Translate for the bitmap brush.    
    20.     p_pRenderTarget-
posted @ 2013-11-17 08:12  fff8965  阅读(1707)  评论(0编辑  收藏  举报