Direct2D WIC绘制图片

绘制图片需要用到WIC,WIC的功能包括:
  • 编解码图片。也可以自定义图片解码插件。
  • 读取图片元数据。
  • 图像处理(最高支持每通道32位)。
  • 内置支持一些流行的格式。包括:BMP v5, GIF 89a/m, ICO, JPEG, PNG 1.2, TIFF 6.0, Windows Media Photo.
WIC绘制图片是在GPU上进行的,所以效率较高

这里写一个简单的读取图片的例子,别忘了添加头文件:

#include "wincodec.h"

以及wic的Lib
windowscodecs.lib;
下面的是d2d必须的
d2d1.lib;
dxgi.lib;
dxguid.lib;

显示图片的流程如下:
  1. 初始化IWICImagingFactory的对象。
  2. IWICImagingFactory创建解码器。
  3. 打开文件,并获取一帧。
  4. 转码。
  5. 初始化。
  6. 创建图片。
  7. 绘制图片。

pRenderTarget->BeginDraw();

	//clear screen
	pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

	IWICImagingFactory* pIWICFactory = NULL;
	IWICBitmapDecoder* pDecoder = NULL;
	IWICBitmapFrameDecode* pFrame = NULL;
	IWICFormatConverter* pConverter = NULL;
	ID2D1Bitmap* pBitmap = NULL;

	CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory) );

	//create a decoder
	hr = pIWICFactory->CreateDecoderFromFilename(
		TEXT("C:\\Users\\NET45.png"),
		NULL,
		GENERIC_READ,
		WICDecodeMetadataCacheOnLoad,
		&pDecoder
		);

	// Create the initial frame.
	hr = pDecoder->GetFrame(0, &pFrame);

	// Format convert the frame to 32bppPBGRA
	hr = pIWICFactory->CreateFormatConverter(&pConverter);

	//Initialize Converter
	hr = pConverter->Initialize(
		pFrame,                          // Input bitmap to convert
		GUID_WICPixelFormat32bppPBGRA,   // Destination pixel format
		WICBitmapDitherTypeNone,         // Specified dither pattern
		NULL,                            // Specify a particular palette 
		0.f,                             // Alpha threshold
		WICBitmapPaletteTypeCustom       // Palette translation type
		);

	// create Bitmap
	hr = pRenderTarget->CreateBitmapFromWicBitmap(
		pConverter,
		NULL,
		&pBitmap
		);

	//get bimap size
	D2D1_SIZE_F size = pBitmap->GetSize() ;
	D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(0.f, 0.f) ;

	//draw bitmap
	pRenderTarget->DrawBitmap(
		pBitmap,
		D2D1::RectF(
		upperLeftCorner.x,
		upperLeftCorner.y,
		upperLeftCorner.x + size.width,
		upperLeftCorner.y + size.height)
		) ;

	pRenderTarget->EndDraw();

关于WIC的详细介绍可以参考MSDN:
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值