WPFのRenderTargetBitmapの画像の幅と高さ
-
02-01-2020 - |
質問
下記のコードを使用しているため、DrawingVisual
を使用してImage
にレンダリングします。最後のRenderTargetBitmap
は後でImage
に追加され、画面に表示されます。
私の問題は、Canvas
とpixelWidth
の引数がpixelHeight
メソッドを望んでいます。私はそれに与えるべきですか?私はそれがそれを小さい数字を与えるならば、画像の一部はレンダリングされません。私はこれらを選ぶべき基礎について?以下のコードに1000を投稿しました。
public class ModelBeamSectionNamesInPlan : Image
{
private readonly VisualCollection _visuals;
public ModelBeamSectionNamesInPlan(BaseWorkspace space)
{
var typeface = Settings.BeamTextTypeface;
var cultureinfo = Settings.CultureInfo;
var flowdirection = Settings.FlowDirection;
var beamtextsize = Settings.BeamTextSize;
var beamtextcolor = Settings.InPlanBeamTextColor;
beamtextcolor.Freeze();
const double scale = 0.6;
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
foreach (var beam in Building.ModelBeamsInTheElevation)
{
var text = beam.Section.Id;
var ft = new FormattedText(text, cultureinfo, flowdirection,
typeface, beamtextsize, beamtextcolor,
null, TextFormattingMode.Display)
{
TextAlignment = TextAlignment.Center
};
// Draw Text
dc.DrawText(ft, space.FlipYAxis(x, y));
}
}
var bmp = new RenderTargetBitmap(1000, 1000, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
Source = bmp;
}
}
. 解決
描画VisualのContentBounds
プロパティを照会することができます。
CluperSevisual
の内容の境界ボックスを取得します
または
のDescendantBounds
プロパティ
全てのコンテンツバウンディングボックスの統合を取得します の内容を含まないがの子孫。 引数
これがうまくいくはずです:
var bounds = drawingVisual.DescendantBounds;
var bmp = new RenderTargetBitmap(
(int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height),
96, 96, PixelFormats.Pbgra32);
. 所属していません StackOverflow