Direct2D draw bitmap

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

http://files.cnblogs.com/youhui/D2DImageExample.rar
在 Windows 平台上,可以使用 Direct2D 和 WIC API 来进行图像处理和转换。如果需要将一个 ID2D1Bitmap 对象转换成一个 IWICBitmap 对象,可以按照以下步骤进行操作: 1. 创建一个 IWICBitmap 对象,可以使用 IWICImagingFactory 来创建: ```c++ IWICImagingFactory *pFactory = nullptr; CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory)); IWICBitmap *pWICBitmap = nullptr; pFactory->CreateBitmap(width, height, GUID_WICPixelFormat32bppPBGRA, WICBitmapCacheOnLoad, &pWICBitmap); ``` 其中,width 和 height 分别代表位图的宽度和高度,GUID_WICPixelFormat32bppPBGRA 表示像素格式为 32 位 RGBA 格式。 2. 创建一个 ID2D1RenderTarget 对象,并将其与 IWICBitmap 关联: ```c++ D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)); ID2D1RenderTarget *pRenderTarget = nullptr; HRESULT hr = pFactory->CreateWicBitmapRenderTarget(pWICBitmap, rtProps, &pRenderTarget); ``` 3. 将 ID2D1Bitmap 绘制到 ID2D1RenderTarget 上: ```c++ pRenderTarget->BeginDraw(); pRenderTarget->DrawBitmap(pD2DBitmap); pRenderTarget->EndDraw(); ``` 其中,pD2DBitmap 是要转换的 ID2D1Bitmap 对象。 4. 释放资源: ```c++ pRenderTarget->Release(); pWICBitmap->Release(); pFactory->Release(); ``` 完整代码示例: ```c++ IWICImagingFactory *pFactory = nullptr; CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory)); IWICBitmap *pWICBitmap = nullptr; pFactory->CreateBitmap(width, height, GUID_WICPixelFormat32bppPBGRA, WICBitmapCacheOnLoad, &pWICBitmap); D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)); ID2D1RenderTarget *pRenderTarget = nullptr; HRESULT hr = pFactory->CreateWicBitmapRenderTarget(pWICBitmap, rtProps, &pRenderTarget); pRenderTarget->BeginDraw(); pRenderTarget->DrawBitmap(pD2DBitmap); pRenderTarget->EndDraw(); pRenderTarget->Release(); pWICBitmap->Release(); pFactory->Release(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值