将HwndRenderTarget中的内容保存到位图文件中

D2D1_PIXEL_FORMAT pixelFormat = PixelFormat(
    DXGI_FORMAT_B8G8R8A8_UNORM,
    D2D1_ALPHA_MODE_PREMULTIPLIED);
IFR(m_spD2dFactory->CreateHwndRenderTarget(
    //RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,pixelFormat),
    RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_SOFTWARE,pixelFormat),
    HwndRenderTargetProperties(m_hWnd,size),
    &m_spHwndRT));
D2D1_SIZE_F size = m_spHwndRT->GetSize();
FLOAT width = size.width, height = size.height;
//Create WIC bitmap...
CComPtr<ID2D1RenderTarget> spBitmapRT;
CWicBitmap wicBitmap;
IFR(wicBitmap.Create(m_spD2dFactory,m_spWicImagingFactory,width,height,&spBitmapRT));
//Copy the content in the HwndRenderTarget to D2D1Bitmap
D2D1_PIXEL_FORMAT pixelFormat = PixelFormat(
    DXGI_FORMAT_B8G8R8A8_UNORM,
    D2D1_ALPHA_MODE_PREMULTIPLIED);
CComPtr<ID2D1Bitmap> spCopyBitmap;
IFR(spBitmapRT->CreateBitmap(
    SizeU(width,height),
    BitmapProperties(pixelFormat),
    &spCopyBitmap));
D2D1_POINT_2U destPoint = Point2U();
D2D1_RECT_U srcRect = RectU(0,0,width,height);
IFR(spCopyBitmap->CopyFromRenderTarget(&destPoint,m_spHwndRT,&srcRect));
CComPtr<IDWriteTextFormat> spTextFormat;
IFR(CreateTextFormat(m_spDWriteFactory,&spTextFormat,_T("Microsoft Yahei"),32));
CComPtr<ID2D1SolidColorBrush> spTextBrush;
IFR(spBitmapRT->CreateSolidColorBrush(ColorF(ColorF::Red),&spTextBrush));
SYSTEMTIME st;
::GetLocalTime(&st);
CString text;
text.Format(_T("File path: %s\nSave time: %2d:%2d:%2d"),
    filePath,st.wHour,st.wMinute,st.wSecond);
//Drawing...
spBitmapRT->BeginDraw();
spBitmapRT->Clear(ColorF(ColorF::White,0));
spBitmapRT->DrawBitmap(spCopyBitmap,RectF(0,0,width,height));
spBitmapRT->DrawText(
    text,
    text.GetLength(),
    spTextFormat,
    RectF(0,0,width/4,height),
    spTextBrush);
spBitmapRT->EndDraw();
//Save the plot as an image file...
IFR(wicBitmap.SaveAsBitmapFile(filePath));

posted on 2010-10-21 14:11  wudong  阅读(1933)  评论(0编辑  收藏  举报

导航