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 的方法 ...
随机推荐
- [转帖]Always-on Profiling for Production Systems
https://0x.tools/ 0x.tools (GitHub) is a set of open-source utilities for analyzing application perf ...
- [转帖]prometheus和node_exporter中的磁盘监控
https://www.ipcpu.com/2021/04/prometheus-node_exporter/ prometheus和node_exporter中的磁盘监控.md 对于磁盘问题,我们主 ...
- [转帖]已整理-shell内置字符串常用操作
https://www.cnblogs.com/reachos/p/16803672.html bash 里面内置了一些常用的字符串操作: 1.字符串截取 a="abc" ${a: ...
- [转帖]TiDB-unsafe recover(三台tikv宕机两台)
一.背景 名称 数量 tikv 3 副本 3 1.故障: 因为某些原因,两台tikv不可连接,出现region不能访问的故障 2.几条理论: 2.1.多副本原则 存在一半以上的副本则集群访问不受影响( ...
- [转帖]JMeter 5.5 使用详解(持续更新中......)
https://blog.csdn.net/qq_45138120/article/details/124056704?ops_request_misc=%257B%2522request%255Fi ...
- 【转帖】nginx变量使用方法详解-6
https://www.diewufeiyang.com/post/580.html Nginx 内建变量用在"子请求"的上下文中时,其行为也会变得有些微妙. 前面在 (三) 中我 ...
- [转帖]Linux平台shell脚本输入密码,不显示明文
需求:shell脚本中输入密码,要求不显示明文,需要将其转换为"*"星号,或者不显示 实现方案:有两种实现方案,一是通过stty命令来实现,二是直接使用read来实现 方案一:使用 ...
- [转帖]Linux AWK工作原理
https://www.cnblogs.com/yeyuzhuanjia/p/13967513.html 本篇文章我们主要为大家介绍 AWK 是如何工作的. AWK 工作流程可分为三个部分:1.读输入 ...
- [转帖]021系统状态检测命令sosreport
https://www.cnblogs.com/anyoneofus/p/16467677.html sosreport命令用于收集系统配置及架构信息并输出诊断文档.
- true=='true'这个等式成立吗?
在localStorage存入里面的数据是字符串,如果你存入了一个值是Boolean类型的, 那你你取出来就是一个字符串 'true' 或者 'false' 假设取出来的值是 'true' 在你进行i ...