当需要设置位图的混合模式时,应该使用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的使用的更多相关文章

  1. Direct2D教程V——位图(Bitmap)和位图笔刷(BitmapBrush)

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  2. 关于 Direct2D

    http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd370987(v=vs.85).aspx 本主题介绍 Direct2D,这是 Win ...

  3. UWP中的Direct2D

    介绍 DirectX一直是Windows平台中高性能图形的代名词,自Win7开始,微软又推出了Direct2D技术,包装于Direct3D,但专注于2D图形,并且准备取代GDI这样的传统2D图形技术. ...

  4. Direct2D教程(外篇)环境配置

    2014年世界杯首场淘汰赛马上开始了,闲着没事,整理以前的博客草稿打发时间,意外的发现这篇文章,本来是打算加入到Direct2D那个系列的,不知道为什么把它给遗漏了.环境配置,对于熟手来说,不是什么重 ...

  5. Direct2D开发:纹理混合

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...

  6. Direct2D开发:从资源加载位图

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D使用Windows图像处理组件 (WIC) 来加载位图.从文件加载位图的方法很简单,而且网上的教 ...

  7. Direct2D开发:绘制网格

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.引言 最近在使用Direct2D进行绘制工作中,需要实现使用Direct2D绘制网格的功能.在网上查了很多资料,终于实 ...

  8. 使用DirectWrite测量Direct2D文字大小

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 最近在使用Direct2D和DirectWrite写引擎,在引擎中需要实现文本标签控件.但是文本标签的尺寸最好不 ...

  9. SharpDX之Direct2D教程II——加载位图文件和保存位图文件

    本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...

  10. 在 WinForm 中使用 Direct2D

    在 C# 的 WinForm 应用中,界面的绘制使用的是 GDI+.不过在一些特别的应用中,可能需要用硬件加速来提高绘制的效率.下面就来介绍两种在 WinForm 应用中嵌入 Direct2D 的方法 ...

随机推荐

  1. [转帖]SQL SERVER DBCC命令详解

    https://developer.aliyun.com/article/867768   简介: SQL数据库开发 DBCC DROPCLEANBUFFERS:从缓冲池中删除所有缓存,清除缓冲区 在 ...

  2. [转帖]前端安全(同源策略、XSS攻击、CSRF攻击)

    https://juejin.cn/post/6844904158697357319 同源策略(Same-origin policy) 如果两个 URL 的协议.域名和端口都相同,我们就称这两个 UR ...

  3. 阿里的AIGC数据库工具: Chat2DB的学习与使用

    阿里的AIGC数据库工具: Chat2DB的学习与使用 背景 今天陪家中老人去完医院后, 继续回来学习时发现 阿里巴巴的 chat2DB已经发布的 2.0.1的版本. 想着下载下来试试. 主要也是备忘 ...

  4. [转帖]Linux—编写shell脚本操作数据库执行sql

    Linux-编写shell脚本操作数据库执行sql Hughman关注IP属地: 北京 0.0762020.03.20 09:02:13字数 295阅读 1,036 修改数据库数据   在升级应用时, ...

  5. [转帖] 拒绝蛮力,高效查看Linux日志文件!

    https://www.cnblogs.com/codelogs/p/16410363.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 日常分析问题时 ...

  6. Redis-dump Docker搭建的快速指南

    背景 最近学习redis想能够将dump文件进行导入处理. 看到比较好的办法都是使用ruby ,但是公司的网络太感人了. 想着比较简单的办法是通过docker方式来搭建. 这里简单记录一下搭建过程. ...

  7. Widows 关闭 Defender的方法

    Study From MS reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v ...

  8. 『Echarts』简介

    目录 一.前言 二.『Echarts』简介 1. 什么是『Echarts』 三.数据可视化 四.『Echarts』 1.『Echarts』的作用 2.『Echarts』能绘制哪些图表 3.『Echar ...

  9. pandas高效读取大文件的探索之路

    使用 pandas 进行数据分析时,第一步就是读取文件.在平时学习和练习的过程中,用到的数据量不会太大,所以读取文件的步骤往往会被我们忽视. 然而,在实际场景中,面对十万,百万级别的数据量是家常便饭, ...

  10. 【JVM】运行时内存分配

    程序计数器 用于标识线程执行到了字节码文件(class文件)的哪一行,当执行native方法时,值为undefined,各个线程私有 Java虚拟机栈 每个线程独有,每个方法执行时会创建一个栈帧,用于 ...