Direct2D CreateBitmap的使用
当需要设置位图的混合模式时,应该使用ID2D1DeviceContext而不是ID2D1RenderTarget。
代码如下:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <comdef.h>
#include <d2d1.h>
#include <d2d1_1.h>
#include <d3d11.h> #pragma comment(lib,"D2d1.lib")
#pragma comment(lib,"D3D11.lib") #include <iostream>
#include <string> void printError(const std::string& msg, HRESULT err)
{
std::cerr << "[ERROR] " << msg << ": " << _com_error(err).ErrorMessage() << std::endl;
assert(false);
} int main(int argc, char* argv[])
{
ID2D1Factory1* mD2DFactory = nullptr;
ID3D11Device* mD3DDevice = nullptr;
ID3D11DeviceContext* mD3DDeviceContext = nullptr;
IDXGIDevice* mDXGIDevice = nullptr;
ID2D1Device* mD2DDevice = nullptr; HRESULT err; // Create factories first; they don't depend on anything.
err = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
__uuidof(ID2D1Factory1), // get a Direct2D 1.1 factory,
(void**)&mD2DFactory);
if (err != S_OK) {
printError("fatal: Could not create Direct2D factory!", err);
return 1;
} // Initialize DirectX 11.1
D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1 };
err = D3D11CreateDevice(NULL, // first adapter
D3D_DRIVER_TYPE_HARDWARE,
NULL, // software rasterizer DLL handle
D3D11_CREATE_DEVICE_SINGLETHREADED // better performance
| D3D11_CREATE_DEVICE_BGRA_SUPPORT, // required for Direct2D
featureLevels,
sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION, // docs say use this
&mD3DDevice,
NULL, // don't care what feature level we got
&mD3DDeviceContext);
if (err != S_OK) {
printError("fatal: could not create a Direct3D 11.1 device", err);
return 1;
} err = mD3DDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&mDXGIDevice);
if (err != S_OK) {
printError("fatal: Direct3D device is not IDXGIDevice", err);
return 1;
} // Initialize Direct2D
err = mD2DFactory->CreateDevice(mDXGIDevice, &mD2DDevice);
if (err != S_OK) {
printError("fatal: could not create Direct2D device", err);
return 1;
} //---------------- Create the bitmap --------------------
ID2D1DeviceContext* mDC = nullptr;
ID2D1Bitmap1* mBitmap = nullptr;
int width = 13, height = 13;
float dpi = 76.0f; err = mD2DDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &mDC);
if (err) {
printError("Could not create device context", err);
return 1;
} D2D1_BITMAP_PROPERTIES1 bitmapProps;
bitmapProps.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
bitmapProps.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
bitmapProps.dpiX = dpi;
bitmapProps.dpiY = dpi;
bitmapProps.bitmapOptions = D2D1_BITMAP_OPTIONS_CANNOT_DRAW | // can use for SetTarget()
D2D1_BITMAP_OPTIONS_CPU_READ;
bitmapProps.colorContext = nullptr; err = mDC->CreateBitmap({ UINT32(width), UINT32(height) },
nullptr, 0,
bitmapProps, &mBitmap);
if (err == S_OK) {
mDC->SetTarget(mBitmap);
}
else {
printError("Could not create bitmap (" + std::to_string(width) + "x" + std::to_string(height) + ")", err);
return 1;
} std::cout << "Success!" << std::endl;
return 0;
}
类似的例子:per-pixel transparent window using Windows Composition engine in C++
Direct2D CreateBitmap的使用的更多相关文章
- Direct2D教程V——位图(Bitmap)和位图笔刷(BitmapBrush)
目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...
- 关于 Direct2D
http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd370987(v=vs.85).aspx 本主题介绍 Direct2D,这是 Win ...
- UWP中的Direct2D
介绍 DirectX一直是Windows平台中高性能图形的代名词,自Win7开始,微软又推出了Direct2D技术,包装于Direct3D,但专注于2D图形,并且准备取代GDI这样的传统2D图形技术. ...
- Direct2D教程(外篇)环境配置
2014年世界杯首场淘汰赛马上开始了,闲着没事,整理以前的博客草稿打发时间,意外的发现这篇文章,本来是打算加入到Direct2D那个系列的,不知道为什么把它给遗漏了.环境配置,对于熟手来说,不是什么重 ...
- Direct2D开发:纹理混合
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...
- Direct2D开发:从资源加载位图
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D使用Windows图像处理组件 (WIC) 来加载位图.从文件加载位图的方法很简单,而且网上的教 ...
- Direct2D开发:绘制网格
转载请注明出处:http://www.cnblogs.com/Ray1024 一.引言 最近在使用Direct2D进行绘制工作中,需要实现使用Direct2D绘制网格的功能.在网上查了很多资料,终于实 ...
- 使用DirectWrite测量Direct2D文字大小
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 最近在使用Direct2D和DirectWrite写引擎,在引擎中需要实现文本标签控件.但是文本标签的尺寸最好不 ...
- SharpDX之Direct2D教程II——加载位图文件和保存位图文件
本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...
- 在 WinForm 中使用 Direct2D
在 C# 的 WinForm 应用中,界面的绘制使用的是 GDI+.不过在一些特别的应用中,可能需要用硬件加速来提高绘制的效率.下面就来介绍两种在 WinForm 应用中嵌入 Direct2D 的方法 ...
随机推荐
- Grafana监控Oracle数据库的表大小等信息
Grafana监控Oracle数据库的表大小等信息 方案 oracledb_exporter 以及 prometheus grafana 使用的SQL以及配置文件 [[metric]] context ...
- [转帖]jmeter编写测试脚本大全
目录 一.背景 二.按照功能划分 2.1 加密处理.验签处理 2.2 jmeter 使用beanshell 编写脚本 2.3 jmeter脚本报错大全 2.4 jmeter打印log 2.5 jmet ...
- [转帖]linux块I/O总体概括
直接先上重点,linux中IO栈的完全图如下: 系统中能够随机访问固定大小数据片的硬件设备称作块设备.固定大小的数据片称为块.常见的块设备就是硬盘了.不能随机访问的就是字符设备了,管理块设备比字符设备 ...
- [转帖]shell脚本之awk命令——按列求平均值、最大值、最小值
文章目录 写在前面 awk求平均值 awk求最大值 awk求最小值 awk求极值.均值的实际应用 写在前面 awk命令求极值和均值需要熟悉该命令的基本用法,如果你不熟悉该命令,请先阅读shell脚本之 ...
- [转帖]Linux性能优化(十五)——CPU绑定
一.孤立CPU 1.孤立CPU简介 针对CPU密集型的任务,CPU负载较高,推荐设置CPU Affinity,以提高任务执行效率,避免CPU进行上下文切换,提高CPU Cache命中率. 默认情况下, ...
- [转帖]@Autowired 和 @Resource 的区别
@Autowired 和 @Resource 的区别 默认注入方式不同 @Autowired 默认的注入方式为byType(根据类型进行匹配),也就是说会优先根据接口类型去匹配并注入 Bean (接口 ...
- Linux运行服务的几种方式
摘要 1. nohup & 2. screen 3. bg & disown 4. systemd 5. crontab @reboot 背景 最近一直在用linux 想着多总结一下. ...
- 我对vue3的理解
我对 reactive源码的理解 reactive 只能够代理对象 首先它判断传递过来的值是否是对象,如果是才会进行代理.变成响应式的. Proxy 并没有重写对象的属性,只做代理,在取值的时候回调用 ...
- diff算法是如何比较的,保证让你看的明明白白的!
更新dom节点,最小力度去跟新 index.html <body> <h1>你好啊!</h1> <button id="btn">该 ...
- Linux挂载新磁盘到根目录
1.添加磁盘到需要挂载的机器上2.lsblk查看硬盘挂载情况,sdb,sdc为我新挂载的磁盘 3.fdisk -l查看挂载之前的分区情况, 4.为新硬盘创建分区 fdisk /dev/sdb,终端会提 ...