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 的方法 ...
随机推荐
- [转帖]Run Grafana behind a reverse proxy
On this page Introduction Configure NGINX Configure HAProxy Configure IIS Configure Traefik Summary ...
- [转帖]Oracle数据库中ITL详解
首先说明这篇文章是转载的,原文地址:http://blog.sina.com.cn/s/blog_616b428f0100lwvq.html 1.什么是ITL ITL(Interested Trans ...
- [转帖]88. sys_kwr
88. sys_kwr ¶ 88.1. 插件sys_kwr简介 ¶ 插件sys_kwr是KingbaseES 的一个扩展插件.主要功能是通过周期性自动记录性能统计相关的快照,分析出KingbaseES ...
- [转帖]Linux 监测服务心跳、服务重启策略
文章目录 前言 背景 一.curl服务可用验证 二.服务探测脚本 三.配置系统定时任务 四.Linux特殊字符转义 总结 前言 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i. 提示:以下是 ...
- [转帖]nmon使用及监控数据分析
[使用] [监控数据分析] 参考链接:nmon监控数据分析 性能测试中,各个服务器资源占用统计分析是一个很重要的组成部分,通常我们使用nmon这个工具来进行监控以及监控结果输出. 一.在监控阶段使用类 ...
- js中toFixed 并不是你想的那样进行四舍五入
toFixed 的简单介绍 toFixed() 方法可把 Number 类型的数字通过四舍五入为指定小数位的字符串.(将数字类型转化为字符串类型) 也就是说toFixed只能够处理数字类型的. 字符串 ...
- Git - 关联远程仓库以及同时使用Lab和Hub
更新一下,感觉有更简单的方式 就比如你git config 的 全局的name和email是lab的 那就clone github上的项目然后设置局部的name和email就行了 ********** ...
- 在ECS中安装Docker在内部访问RDS数据库
Navicat连接阿里云RDS数据库入门 https://blog.csdn.net/fenxunkao0106/article/details/106594276 https://www.cnblo ...
- PostgreSQL中的B-TREE索引
分析了解pgsql中的索引 前言 索引 B-tree B-Tree和B+Tree的区别: pgsql中B-Tree 实现 如果该节点不是最右节点 如果该节点是最右节点 参考 分析了解pgsql中的索引 ...
- ElasticSearch7.3学习(一)----采用restful风格 基本的增删查改语句
1 .新建图书索引 首先建立图书索引 book 语法:put /index PUT /book 结果 2.新增图书 :新增文档 语法:PUT /index/type/id PUT /book/_doc ...