D2D图片工厂类

#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 ;
	}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值