.NETの描画パフォーマンスを上げる件
.NETでDirect2Dを使用して描画する時に役立つ非常に良いサンプルを発見。
https://www.newtone.co.jp/producttc_dnjp00.html
→「Windows Forms で画像生成能力を上げるには(PDF)」
→ サンプルは次のURLからダウンロードできます。 のリンク
- wpf vs System.Drawing vs Direct2D(SlimDX) のサンプル
- 描画手段を抽象化した簡単なクラスライブラリが入っていて本当にすぐ理解できる
- 自作ライブラリのベースに使える
気が付いたこと
- 試した環境は Windows 10 64bit
- SlimDXをnugetから入れるとx86版しか入らないしうまく起動できない。公式HPからx86とx64を両方インストールしてGACに登録する必要があった。たぶん個別に設定いじる方法もあるけど今回はちゃっちゃとインストーラ走らせた方が良い。
- 上記のサンプルにはDirect2Dのポリゴン生成部分にバグがあり、すごいメモリリークする。下記のように、geomをDisposeさせるコードを追加する。
[CanvasDirect2D.cs オリジナル]
private PathGeometry GetPathGeometry(Point[] points)
{
PathGeometry result = null;
if (points.Length > 1)
{
result = new PathGeometry(g.Factory);
GeometrySink geom = result.Open(); //<----geomがDisposeされていない
geom.SetFillMode(FillMode.Winding);
geom.BeginFigure(points[0], FigureBegin.Filled);
for (int i = 1; i < points.Length; i++)
{
geom.AddLine(points[i]);
}
geom.EndFigure(FigureEnd.Closed);
geom.Close();
}
return result;
}
[CanvasDirect2D.cs 修正後]
private PathGeometry GetPathGeometry(Point[] points)
{
PathGeometry result = null;
if (points.Length > 1)
{
result = new PathGeometry(g.Factory);
using(GeometrySink geom = result.Open()) //usingを追加
{
geom.SetFillMode(FillMode.Winding);
geom.BeginFigure(points[0], FigureBegin.Filled);
for (int i = 1; i < points.Length; i++)
{
geom.AddLine(points[i]);
}
geom.EndFigure(FigureEnd.Closed);
geom.Close();
}
}
return result;
}