#include <d2d1.h>
#include <wincodec.h>
#include <string.h>
#include <tchar.h>
#define SAFE_RELEASE(P) if(P){P->Release() ; P = NULL ;}
ID2D1Factory* pD2DFactory ; // Direct2D factory
class PictureFactory
{
protected:
ID2D1HwndRenderTarget* pRenderTarget; // Render target
ID2D1Bitmap* pBitmap ;
IWICImagingFactory* pWICFactory ;
PTSTR pathname;
RECT rc ;
HWND m_Hwnd;
public:
VOID setdata(HWND hwnd,PTSTR url)
{
pD2DFactory=NULL;
pRenderTarget=NULL;
pBitmap=NULL;
pWICFactory=NULL;
pathname=(PTSTR)malloc(_tcslen(url));
_tcscpy(pathname,url);
m_Hwnd=hwnd;
}
HRESULT LoadBitmapFromFile(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCTSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
HRESULT hr = S_OK;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
hr = pIWICFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth, originalHeight;
hr = pSource->GetSize(&originalWidth, &originalHeight);
if (SUCCEEDED(hr))
{
if (destinationWidth == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destinationHeight == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
}
hr = pIWICFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr))
{
hr = pScaler->Initialize(
pSource,
destinationWidth,
destinationHeight,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
}
else // Don't scale the image.
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}
SAFE_RELEASE(pDecoder);
SAFE_RELEASE(pSource);
SAFE_RELEASE(pStream);
SAFE_RELEASE(pConverter);
SAFE_RELEASE(pScaler);
return hr;
}
VOID CreateD2DResource(HWND hWnd)
{
if (!pRenderTarget)
{
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory) ;
if (FAILED(hr))
{
MessageBox(hWnd, TEXT("Create D2D factory failed!"), TEXT("Error"), 0) ;
return ;
}
// Create WIC factory
hr = CoCreateInstance(
CLSID_WICImagingFactory1,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void **>(&pWICFactory)
) ;
// 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)
),
&pRenderTarget
) ;
if (FAILED(hr))
{
MessageBox(hWnd, TEXT("Create render target failed!"), TEXT("Error"), 0) ;
return ;
}
hr = LoadBitmapFromFile(
pRenderTarget,
pWICFactory,
pathname,
0,
0,
&pBitmap
) ;
if (FAILED(hr))
{
MessageBox(hWnd, TEXT("Create bitmap failed!"), TEXT("Error"), 0) ;
return ;
}
}
}
void Cleanup()
{
SAFE_RELEASE(pRenderTarget) ;
SAFE_RELEASE(pD2DFactory) ;
}
void DrawBitmap()
{
CreateD2DResource(m_Hwnd) ;
pRenderTarget->BeginDraw() ;
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F size = pBitmap->GetSize() ;
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(0.f, 0.f) ;//****************************************************************
float xx;
pRenderTarget->DrawBitmap(
pBitmap,
D2D1::RectF(
0,
0,
float(GetSystemMetrics(SM_CYSCREEN))*size.width/size.height , //*********************************************************************************
float(GetSystemMetrics(SM_CYSCREEN)))
);
HRESULT hr = pRenderTarget->EndDraw() ;
if (FAILED(hr))
{
MessageBox(NULL, TEXT("Draw failed!"), TEXT("Error"), 0) ;
return ;
}
ValidateRect(m_Hwnd,NULL);
}
void init()
{
pD2DFactory = NULL ; // Direct2D factory
pRenderTarget = NULL; // Render target
pBitmap = NULL ;
pWICFactory = NULL ;
}
};
D2D图片工厂类
最新推荐文章于 2018-08-27 16:26:32 发布