c# - C#texture2d如何转换为cpu图像
问题描述
您如何将 Directx11 Texture2d 转换为我可以处理的 cpu 上的图像?
我试过搜索这个问题,谷歌提出了使用专有 API 的统一答案,或者答案反映到 System.Drawing.Texture2
解决方案
您需要创建一个暂存纹理,然后您可以通过 cpu 访问它。
为此,我假设您已经拥有一个现有的 SharpDX 纹理:
public static StagingTexture2d FromTexture(Texture2D texture)
{
if (texture == null)
throw new ArgumentNullException("texture");
//Get description, and swap a few flags around (make it readable, non bindable and staging usage)
Texture2DDescription description = texture.Description;
description.BindFlags = BindFlags.None;
description.CpuAccessFlags = CpuAccessFlags.Read;
description.Usage = ResourceUsage.Staging;
return new StagingTexture2d(texture.Device, description);
}
这个新纹理将允许读取操作。
接下来,您需要使用设备上下文将 GPU 纹理复制到暂存纹理中:
deviceContext.CopyResource(gpuTexture, stagingTexture);
完成此操作后,您可以映射暂存纹理以访问其在 CPU 上的内容:
DataStream dataStream;
DataBox dataBox = deviceContext.MapSubresource(stagingTexture,0, MapMode.Read, MapFlags.None, out dataStream);
//Use either datastream to read data, or dataBox.DataPointer
//generally it's good to make a copy of that data immediately and unmap asap
//Very important, unmap once you done
deviceContext.UnmapSubresource(stagingTexture, 0);
推荐阅读
- java - JavaFX FileChooser 本地化内部元素的任何解决方法?
- javascript - javascript在动态显示中性能缓慢:id无
- image - matlab,图像大小相同,但我不能使用 imabsdiff
- vba - 编译错误子或未定义函数(VBA)
- c++ - 提升精神语法问题
- json - Swift 4:使用 URLSession 发出请求并更新全局变量
- ruby-on-rails - 如何使用 rspec 测试 ActionCable 和 Devise?
- java - 搜索数组的问题
- composer-php - 我应该把 composer.json 文件放在哪里?
- javascript - 解析 afterFind,添加到响应的对象是客户端中的指针