我需要非常快速地显示图像(大约60 FPS)。 Picturebox / Panel不能以更高的分辨率完成工作,所以我现在转向SlimDX,我希望这是正确的举措。
由于SlimDX使用Directx,而DirectX使用GPU,我应该能够非常快速地完成。根据我的理解,GPU使用图像比使用CPU快得多。
我这样做:
MessagePump.Run(form, () =>
{
device.BeginScene();
sprite.Begin(SlimDX.Direct3D9.SpriteFlags.None);
tx = SlimDX.Direct3D9.Texture.FromStream(device, (new MemoryStream(reader.ReadBytes(reader.ReadInt32()))), SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Pool.Managed);
sprite.Draw(tx, Color.Transparent);
sprite.End();
device.EndScene();
device.Present();
});
初始化一切:
var form = new RenderForm("Test");
form.Width = 1280;
form.Height = 720;
SlimDX.Direct3D9.PresentParameters presentParams = new SlimDX.Direct3D9.PresentParameters
{
BackBufferWidth = form.Width,
BackBufferHeight = form.Height,
DeviceWindowHandle = form.Handle,
PresentFlags = SlimDX.Direct3D9.PresentFlags.None,
BackBufferCount = 0,
PresentationInterval = SlimDX.Direct3D9.PresentInterval.Immediate,
SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard,
BackBufferFormat = SlimDX.Direct3D9.Format.A8R8G8B8,
Windowed = true,
};
device = new SlimDX.Direct3D9.Device(new SlimDX.Direct3D9.Direct3D(), 0, SlimDX.Direct3D9.DeviceType.Hardware, form.Handle, SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing, presentParams);
device.Viewport = new SlimDX.Direct3D9.Viewport(0, 0, form.Width, form.Height);
SlimDX.Direct3D9.Sprite sprite;
SlimDX.Direct3D9.Texture tx;
sprite = new SlimDX.Direct3D9.Sprite(device);
tx = new SlimDX.Direct3D9.Texture(device, form.Width, form.Height, 0, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.X8R8G8B8, SlimDX.Direct3D9.Pool.Managed);
这有两个问题:
答案 0 :(得分:0)
你需要缓存纹理,因为每个帧都很慢。 E.g。
公共类CacheObjects { static List cacheSCBrushes = new List();
public static SolidColorBrush GetColorBrush(Color4 Color, Direct2D.RenderTarget renderTGT)
{
// ERROR: Not supported in C#: OnErrorStatement
SolidColorBrush returnBrush = null;
bool found = false;
foreach (void br_loopVariable in cacheSCBrushes) {
br = br_loopVariable;
if (br.Color.Red == Color.Red) {
if (br.Color.Green == Color.Green) {
if (br.Color.Blue == Color.Blue) {
if (br.Color.Alpha == Color.Alpha) {
found = true;
returnBrush = br;
exit for;
}
}
}
}
}
if (!found) {
returnBrush = new SolidColorBrush(renderTGT, Color);
cacheSCBrushes.Add(returnBrush);
}
return returnBrush;
}
public static void ClearCache()
{
foreach (void Brush_loopVariable in cacheSCBrushes) {
Brush = Brush_loopVariable;
Brush.Dispose();
}
}
}
如果那是不可能的,[在一个单独的线程中],加载接下来的5个纹理......渲染很快,对象创建很慢。
public void LoadTexture () {
//Load the next 5 here
}