- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 SharpDX.DataStream 获取像素。我每秒执行此操作 25 次,这会导致大量内存使用。我想我缺少某种清理工作。
一旦我开始调用 GetColor 方法,内存使用量就开始上升。我尝试过Displosing the class,但没有成功。任何有更多 SharpDX 经验的人都可以指出我所缺少的内容。这很可能是一些简单的事情,比如释放资源,但我陷入了困境。
// Original code by Florian Schnell
// http://www.floschnell.de/computer-science/super-fast-screen-capture-with-windows-8.html
using System;
using System.IO;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using Color = System.Drawing.Color;
using Device = SharpDX.Direct3D11.Device;
using MapFlags = SharpDX.DXGI.MapFlags;
using Point = System.Drawing.Point;
using Resource = SharpDX.DXGI.Resource;
using ResultCode = SharpDX.DXGI.ResultCode;
namespace Artemis.Modules.Effects.AmbientLightning
{
internal class ScreenCapture : IDisposable
{
private readonly Device _device;
private readonly Factory1 _factory;
private readonly Texture2D _screenTexture;
private DataStream _dataStream;
private readonly OutputDuplication _duplicatedOutput;
private Resource _screenResource;
private Surface _screenSurface;
public ScreenCapture()
{
// Create device and factory
_device = new Device(DriverType.Hardware);
_factory = new Factory1();
// Creating CPU-accessible texture resource
var texdes = new Texture2DDescription
{
CpuAccessFlags = CpuAccessFlags.Read,
BindFlags = BindFlags.None,
Format = Format.B8G8R8A8_UNorm,
Height = _factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Bottom,
Width = _factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Right,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
ArraySize = 1,
SampleDescription =
{
Count = 1,
Quality = 0
},
Usage = ResourceUsage.Staging
};
_screenTexture = new Texture2D(_device, texdes);
// duplicate output stuff
var output = new Output1(_factory.Adapters1[0].Outputs[0].NativePointer);
_duplicatedOutput = output.DuplicateOutput(_device);
_screenResource = null;
_dataStream = null;
}
public void Dispose()
{
_duplicatedOutput.Dispose();
_screenResource.Dispose();
_dataStream.Dispose();
_factory.Dispose();
}
public DataStream Capture()
{
try
{
OutputDuplicateFrameInformation duplicateFrameInformation;
_duplicatedOutput.AcquireNextFrame(1000, out duplicateFrameInformation, out _screenResource);
}
catch (SharpDXException e)
{
if (e.ResultCode.Code == ResultCode.WaitTimeout.Result.Code ||
e.ResultCode.Code == ResultCode.AccessDenied.Result.Code ||
e.ResultCode.Code == ResultCode.AccessLost.Result.Code)
return null;
throw;
}
// copy resource into memory that can be accessed by the CPU
_device.ImmediateContext.CopyResource(_screenResource.QueryInterface<SharpDX.Direct3D11.Resource>(),
_screenTexture);
// cast from texture to surface, so we can access its bytes
_screenSurface = _screenTexture.QueryInterface<Surface>();
// map the resource to access it
_screenSurface.Map(MapFlags.Read, out _dataStream);
// seek within the stream and read one byte
_dataStream.Position = 4;
_dataStream.ReadByte();
// free resources
_dataStream.Close();
_screenSurface.Unmap();
_screenSurface.Dispose();
_screenResource.Dispose();
_duplicatedOutput.ReleaseFrame();
return _dataStream;
}
/// <summary>
/// Gets a specific pixel out of the data stream.
/// </summary>
/// <param name="surfaceDataStream"></param>
/// <param name="position">Given point on the screen.</param>
/// <returns></returns>
public Color GetColor(DataStream surfaceDataStream, Point position)
{
var data = new byte[4];
surfaceDataStream.Seek(
position.Y*_factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Right*4 + position.X*4,
SeekOrigin.Begin);
surfaceDataStream.Read(data, 0, 4);
return Color.FromArgb(255, data[2], data[1], data[0]);
}
}
}
最佳答案
不确定它是否能解决您的内存问题,但有一些改进:
_screenResource.QueryInterface<SharpDX.Direct3D11.Resource>()
_screenSurface = _screenTexture.QueryInterface<Surface>();
虽然您可以直接执行此操作 Direct3D11.DeviceContext.Map()
DeviceContext.Map()
的版本返回一个结构体(仅包含非托管内存指针)而不是 DataStream
Utilities.Read<ColorBGRA>(intptr)
使用正确的字节偏移量来提取数据(请注意 DataStream
还允许直接读取颜色,它不适合与 ReadByte
一起使用)position.Y*_factory.Adapters1[0].Outputs[0].Description.DesktopBounds.Right*4 + position.X*4
。使用 Map 方法返回的步幅 ( DataBox.RowPitch
) 来偏移到正确的行,而不是纹理的宽度(每行的字节数可以更改,并且可能不会与纹理宽度对齐)关于c# - 从 SharpDX 中的数据流读取导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990670/
我目前正在使用SharpDX SourceVoice在WP8应用程序中播放wav文件,该文件可以很好地播放单个文件。 但是,我找不到在没有在SourceVoice上注册事件并排队下一个文件的情况下循环
如何在构建时在 SharpDX 中编译顶点和像素着色器?您可以在“Direct3D Rendering Cookbook”中读到的方法是使用 HLSLCompiler.CompileFromFile。
我正在使用 SharpDX 2.5.0 并且我有一个游戏类,我尝试禁用固定时间步长,但是它似乎不起作用,我仍然得到 59-60 FPS。我只画了一个犹他茶壶,所以我很确定它一定能以更高的速度(比如 1
我使用 easyhook 和 SharpDX 从 DirectX 游戏中获取 fps 数据。有时它有效。但是,当我下次启动它时(可能只是几分钟后),它会抛出异常 System.IO.FileNotFo
我在弄清楚如何使用 SharpDX 设置动态 VertexBuffer 和 IndexBuffer 时遇到了很多问题。 我必须在用户按下屏幕的地方生成三角形。 我想我必须设置一个转换函数,将我的屏幕坐
我正在尝试在我的窗口大小更改后立即对后备缓冲区实现更新。所以我的对象不会被拉伸(stretch)。所以我试了一下 _renderForm.Resize += OnRenderFormOnResize;
我正在使用 SharpDX 编写 C# Direct2D 应用程序,但是我可以理解 C++ 中提供的答案/示例。 我想呈现文本并更改某些字符的宽度以使其看起来像图片: 字母B扩大到200%,字母D缩小
我正在尝试从 SharpDX.DataStream 获取像素。我每秒执行此操作 25 次,这会导致大量内存使用。我想我缺少某种清理工作。 一旦我开始调用 GetColor 方法,内存使用量就开始上升。
我正在尝试让“SharpDX.Direct3D11.DeviceContext.OutputMerger.Blendstate”工作。没有这个,我就有了不错的场景(带有纹理的多边形,用于太空射手)。我
我正在尝试让“SharpDX.Direct3D11.DeviceContext.OutputMerger.Blendstate”工作。没有这个,我就有了不错的场景(带有纹理的多边形,用于太空射手)。我
此代码每 100 毫秒运行一次。内存使用量不断增加,直到达到 1.5 GB,然后崩溃。 void takeScreenShot() { Surface s; s = CaptureSc
我们正在使用 SharpDX 开发 Windows 8 Metro 应用程序.现在我们必须在 Rectangle 中声明一组字符串.为此,我们尝试使用 SharpDX.DrawingSizeF 找出字
我是 sharpdx 的新手。 我目前的工作流程是用户可以使用网络摄像头或任何摄像头设备拍摄照片。现在我正在为用户开发新功能。用户可以在图像顶部绘制任何内容,例如 (mspaint drawing).
首先,如果这是问这个问题的错误地方,我深表歉意,我在其他地方找不到答案。 我已将 SharpDX 引用导入到我的 C# 项目中,但是,SharpDX.Windows 引用不存在。我试过从网站下载它并使
SharpDX 有一个 RenderLoop,它在渲染循环中运行给定的委托(delegate): RenderLoop.Run(m_RenderForm, () => { // Do stuf
我在互联网上四处寻找代码,试图找到允许我从常规字体(而不是 Sprite 字体)绘制文本的代码,但只找到了对我毫无帮助的片段。 有人有完整的源文件可以让我使用 SharpDX 和 D3D11/Dire
(这个问题是基于对 this 其他问题的进一步调查,但不是同一个问题,这是关于绘画问题的非常具体的问题。) 我正在尝试绘制一个重叠在目标窗口上的透明表面,问题是我不知道如何将其涂成透明,所以现在我的表
我正在将 SharpDX.WPF 项目用于 WPF 功能,与 SharpDX 附带的工具包相比,它似乎是一个易于理解的低开销库(具有相同的问题!) 首先:我使用以下方法为最新的 SharpDX 修复了
我已经使用SharpDX.XAudio2了几天,虽然事情在很大程度上是积极的(到处都是奇怪的软件怪癖),但以下问题却使我完全陷入困境: 我正在使用VS2015在C#.NET中工作。 我正在尝试同时播放
我在 Direct2D 下使用 SharpDx 开发 Metro 应用程序。我使用 deviceContext 画了一些图片。如何将图片保存为 png 文件? 最佳答案 解决方案: public
我是一名优秀的程序员,十分优秀!