Direct2D 第5篇 绘制图像

原文:Direct2D 第5篇 绘制图像

我加载的图像是一张透明底PNG图像,背景使用渐变的绿色画刷

#include <windows.h>
#include <d2d1.h>
#include <d2d1helper.h>
#include <dwrite.h>
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "d2d1.lib")

#include <wincodec.h>

HINSTANCE g_hinst;
HWND g_hwnd;

ID2D1Factory * g_factory;
ID2D1HwndRenderTarget * g_render_target;
ID2D1SolidColorBrush  * g_brush;

IDWriteFactory * g_write_factory;
IDWriteTextFormat * g_text_format;

ID2D1GradientStopCollection * g_gradient_stop_collection;
ID2D1LinearGradientBrush  * g_linear_gradient_brush;
ID2D1RadialGradientBrush * g_radial_gradient_brush;

IWICImagingFactory * g_image_factory = NULL;
ID2D1Bitmap * g_bitmap = NULL;

bool AppInit()
{
	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

	D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory);

	RECT rc;
	GetClientRect(g_hwnd, &rc);

	g_factory->CreateHwndRenderTarget(
		D2D1::RenderTargetProperties(),
		D2D1::HwndRenderTargetProperties(g_hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)	),
		&g_render_target);

	g_render_target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::WhiteSmoke), &g_brush);

	// Init Font
	DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,__uuidof(g_write_factory),reinterpret_cast<IUnknown **>(&g_write_factory));
	g_write_factory->CreateTextFormat(L"Arial", NULL, DWRITE_FONT_WEIGHT_NORMAL,
		DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 40, L"",&g_text_format);

	// Create Gradient Stops
	D2D1_GRADIENT_STOP gradient_stops[3];
	gradient_stops[0].color = D2D1::ColorF(D2D1::ColorF::LightGreen);
	gradient_stops[0].position = 0.0f;
	gradient_stops[1].color = D2D1::ColorF(D2D1::ColorF::DarkGreen);
	gradient_stops[1].position = 0.5f;
	gradient_stops[2].color = D2D1::ColorF(D2D1::ColorF::Green);
	gradient_stops[2].position = 1.0f;

	// Create Interface
	g_render_target->CreateGradientStopCollection(gradient_stops, 3, &g_gradient_stop_collection);

	// Create Linear Gradient Brush
	g_render_target->CreateLinearGradientBrush(
	D2D1::LinearGradientBrushProperties(D2D1::Point2F(0,0), D2D1::Point2F(600,600)),
		g_gradient_stop_collection, &g_linear_gradient_brush);

	g_render_target->CreateRadialGradientBrush(
		D2D1::RadialGradientBrushProperties(D2D1::Point2F(500,500),D2D1::Point2F(),1000.0f,1000.0f),
		g_gradient_stop_collection,&g_radial_gradient_brush);

	// Initialize Image Factory
	CoCreateInstance(CLSID_WICImagingFactory,NULL,CLSCTX_INPROC_SERVER,__uuidof(g_image_factory),(LPVOID*)&g_image_factory);

	IWICBitmapDecoder *bitmapdecoder = NULL;
	g_image_factory->CreateDecoderFromFilename(L"x.png",NULL,GENERIC_READ,WICDecodeMetadataCacheOnDemand,&bitmapdecoder);//

	IWICBitmapFrameDecode  *pframe = NULL;
	bitmapdecoder->GetFrame(0,&pframe);

	IWICFormatConverter * fmtcovter = NULL;
	g_image_factory->CreateFormatConverter(&fmtcovter);
	fmtcovter->Initialize(pframe,GUID_WICPixelFormat32bppPBGRA,WICBitmapDitherTypeNone,NULL,0.0f,WICBitmapPaletteTypeCustom);
	g_render_target->CreateBitmapFromWicBitmap(fmtcovter, NULL, &g_bitmap);

	fmtcovter->Release();
	pframe->Release();
	bitmapdecoder->Release();

	return true;
}

void OnSize(LPARAM lparam)
{
	if(g_render_target)
		g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam)));
}

void OnPaint()
{
	if(!g_render_target)
		return;

	g_render_target->BeginDraw();

	// Clear Background
	g_render_target->Clear(D2D1::ColorF(0.63, 0.84, 0.00)); 

	// Draw Ellipse
    D2D1_SIZE_F size = g_render_target->GetSize();
	D2D1_RECT_F r = {0, 0, size.width, size.height};
	g_render_target->FillRectangle(&r, g_linear_gradient_brush);
	//g_render_target->FillRectangle(&ellipse, g_linear_gradient_brush); 

	// Draw Image
	D2D1_RECT_F imgr = {0, 0, size.width, size.height};
	g_render_target->DrawBitmap(g_bitmap, imgr);

	// Draw Text
	//const wchar_t * text = L"Direct2D Draw Image";
	//g_render_target->DrawText(text, wcslen(text),
	//	g_text_format,
	//	D2D1::RectF(100, 190, size.width, size.height),
	//	g_brush);

	g_render_target->EndDraw();
}

void OnDestroy()
{
	g_bitmap->Release();
	g_image_factory->Release();

	g_linear_gradient_brush->Release();
	g_radial_gradient_brush->Release();
	g_gradient_stop_collection->Release();
	g_text_format->Release();
	g_write_factory->Release();
	g_brush->Release();
	g_render_target->Release();
	g_factory->Release();
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_PAINT:
		OnPaint();
		break;

	case WM_SIZE:
		OnSize(lparam);
		break;

	case WM_DESTROY:
		OnDestroy();
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hwnd, msg, wparam, lparam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	MSG msg;  

	memset(&wc,0,sizeof(wc));
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.lpfnWndProc = WndProc;
	wc.hInstance = hinst;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	g_hinst = hinst;

	g_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Direct2D Demo",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		640,
		480,
		NULL, NULL, hinst, NULL);

	if(g_hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	if(!AppInit())
	{
		MessageBox(NULL, "Application Initialisation Failed !","Error",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	while(GetMessage(&msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

原文地址:https://www.cnblogs.com/lonelyxmas/p/10823171.html

时间: 2024-10-09 20:16:19

Direct2D 第5篇 绘制图像的相关文章

Direct2D 第3篇 绘制文字

原文:Direct2D 第3篇 绘制文字 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> #include <dwrite.h> #pragma comment(lib, "dwrite.lib") #pragma comment(lib, "d2d1.lib") HINSTANCE g_hinst; HWND g_hwnd; ID2D1Fa

Direct2D 第2篇 绘制椭圆

原文:Direct2D 第2篇 绘制椭圆 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> #include <dwrite.h> #pragma comment(lib, "dwrite.lib") #pragma comment(lib, "d2d1.lib") HINSTANCE g_hinst; HWND g_hwnd; ID2D1Fa

Direct2D 第6篇 绘制多种风格的线条

原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢",不过Direct2D的绘制线条代码,要比GDI/GDI+要繁锁一些. 1.首先,初始化Direct2D(可以参考?http://blog.csdn.net/ubuntu_ai/article/details/50365536 ) 2.创建线条的风格实例 ?ID2D1StrokeStyle,以下函数Create

安卓突击:绘制图像

两种方法, 1,canvas.drawBitmap(); 2,drawable.draw(canvas); Bitmap.Config.ARGB_8888:代表的是RGB每个占8个字节,透明度通道占8个字节. 首先是使用InputStream is= context.getResources().openRawResource(R.drawable.panda);用来获取资源 然后是使用BitmapFactory来将is进行转换成Bitmap对象. BitmapFactory.Options o

5. Quartz2D 绘制图像

#pragma mark 绘制图像 -(void)drawImage:(CGContextRef)context{ UIImage *image = [UIImage imageNamed:@"1.png"]; //提示:绘制之后,就无法改变位置,也没有办法监听手势 //在指定点绘制图像 [image drawAtPoint:CGPointMake(50, 50)]; //会在指定的矩形中拉伸绘图 [image drawInRect:CGRectMake(0, 0, 320, 460)

java swing开发的图像生成器demo实例源代码下载,实现绘制图像,截屏功能。

一个类似于画画的javase程序 绘制图形 原文:java swing开发的图像生成器demo实例源代码下载,实现绘制图像,截屏功能. java源代码下载地址:http://www.zuidaima.com/share/1550463330028544.htm 获取屏幕 打开调色板

Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作

%测试数据 'ex1data1.txt', 第一列为 population of City in 10,000s, 第二列为 Profit in $10,000s 1 6.1101,17.592 2 5.5277,9.1302 3 8.5186,13.662 4 7.0032,11.854 5 5.8598,6.8233 6 8.3829,11.886 7 7.4764,4.3483 8 8.5781,12 9 6.4862,6.5987 10 5.0546,3.8166 11 5.7107,3

使用PHP GD库绘制图像,不显示的问题

1. 使用PHP中的GD库绘制图像,之后浏览器无法显示,GD库安装,配置也没什么错误,提示图像因本身有错无法显示,于是就在header() 前面使用ob_clean();然后使用浏览器就能正常的浏览了 1 <?php 2 $height = 300; 3 $width = 300; 4 $im = imagecreatetruecolor($width, $height); 5 $white = imagecolorallocate ($im, 255, 255, 255); 6 $blue =

Matlab绘制图像及图像的处理

一 绘制函数图像 matlab平面绘制函数图像有多个函数,plot,ezplot等. 1.1 plot函数 查看matlab的帮助文件可知plot函数的调用格式有 PLOT Linear plot. PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,    then the vector is plotted versus the rows or columns of the matrix,    whichever