【问题标题】:ILNumerics: Export part of a scene as image without affecting original sceneILNumerics:将场景的一部分导出为图像而不影响原始场景
【发布时间】:2015-11-27 20:04:51
【问题描述】:

我创建了一个包含 3 个 PlotCube 和一个颜色条的 ILNumerics 场景。

Screenshot of the ILPanel

我想添加一个以两种方式将场景导出为图像的方法,第一种是您在上面看到的屏幕截图。 第二个导出应该只显示中心立方体。

我尝试遵循 scene management 的 ILNumerics 指南。

我写了以下代码:

public void ExportAsImage(int resolutionWidth, int resolutionHeight, string path, bool includeSubCubes)
    {
        using (ILScope.Enter())
        {
            ILGDIDriver backgroundDriver = new ILGDIDriver(resolutionWidth, resolutionHeight, ilPanel1.Scene);

            if (includeSubCubes)
            {
                // code for standard export here
            }
            else
            {
                // setting left and top cube and color bar invisible and
                // adjusting main cube size is affecting the ilPanel.Scene
                backgroundDriver.Scene.First<ILColorbar>().Visible = false;
                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _leftCubeTag).Visible = false;
                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _topCubeTag).Visible = false;

                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _mainCubeTag).ScreenRect = new RectangleF(0, 0, 1, 1);
                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _mainCubeTag).DataScreenRect = new RectangleF.Empty;

                backgroundDriver.Scene.Configure();
                backgroundDriver.Render();

                // save image
                backgroundDriver.BackBuffer.Bitmap.Save(path,System.Drawing.Imaging.ImageFormat.Png);

                // revert changes done to cubes and color bar
                backgroundDriver.Scene.First<ILColorbar>().Visible = true;
                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _leftCubeTag).Visible = true;
                GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _topCubeTag).Visible = true;
                AdjustCubeSizes();
            }
        }
    }

注意:“GetElementByTag”是一个自己的实现,用于在 ILNumerics 场景中检索对象。

我最初预计新驱动程序基本上会创建我可以处理的场景的副本,但就像代码显示的那样,我必须在导出后恢复所有更改,或者显示的 ilPanel 仅以我导出的方式显示场景。

是否可以在不影响真实场景的情况下导出到图像?我只是遗漏了一些细节吗?

问候, 弗洛里安 S.

【问题讨论】:

    标签: image export scene ilnumerics


    【解决方案1】:

    弗洛里安,它确实复制了一份。但是您需要将有趣的部分添加到新场景中。魔法发生在 Add() 方法中:

    var scene4render = new ILScene(); 
    scene4render.Add(oldScene.First<ILPlotCube>(mytag)); 
    // ... configure scene4render here, it will be detached from the original scene
    // with the exception of shared buffers.
    
    // ... proceed with rendering
    

    为了还包括 + 渲染原始绘图立方体的交互状态更改(假设用户鼠标旋转),您可以使用类似的东西:

    scene4render.Add(panel.SceneSyncRoot.First<ILPlotCube>(mytag)); 
    

    另外,我想知道GetElementByTagILGroup.First&lt;T&gt;(tag, predicate)ILGroup.Find&lt;T&gt;(...) 做得更好吗?

    另请参阅:http://ilnumerics.net/scene-management.html

    【讨论】:

    • 谢谢。看来,这正是我所缺少的代码部分。我们使用“GetElementByTag”,因为一些测试表明通常的方法不区分大小写,并以字符串形式查找 包含 给定标签的标签,例如Find("He") 将返回一个带有标签“hello”的 ILNode
    • 是的。您可以考虑将 predicate 参数与匿名函数一起使用作为替代方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多