[英]Width and Height of the Image in RenderTargetBitmap in WPF
Using the below code I'm drawing on DrawingVisual
then rendering it to an Image
using RenderTargetBitmap
. 使用下面的代码,我在
DrawingVisual
绘图,然后使用RenderTargetBitmap
将其渲染为Image
。 The final Image
is later added to a Canvas
and displayed on the screen. 最终的
Image
稍后会添加到Canvas
并显示在屏幕上。
My problem is with the pixelWidth
and pixelHeight
arguments the RenderTargetBitmap
method wants. 我的问题是
RenderTargetBitmap
方法想要的pixelWidth
和pixelHeight
参数。 What valued should I give to it? 我应该给予什么价值? I have noticed that if I give it lower numbers parts of the image is not rendered.
我注意到如果我给它较低的数字部分图像不会被渲染。 On what basis should I choose these?
我应该在什么基础上选择这些? I have given it 1000 in the code below.
我在下面的代码中给了它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;
}
}
You can query the DrawingVisual's ContentBounds
property, which 您可以查询DrawingVisual的
ContentBounds
属性
gets the bounding box for the contents of the ContainerVisual
获取ContainerVisual内容的边界框
or the DescendantBounds
property which 或者
DescendantBounds
属性
gets the union of all the content bounding boxes for all of the descendants of the ContainerVisual, but not including the contents of the ContainerVisual.
获取ContainerVisual的所有后代的所有内容边界框的并集,但不包括ContainerVisual的内容。
Something like this should work: 这样的事情应该有效:
var bounds = drawingVisual.DescendantBounds;
var bmp = new RenderTargetBitmap(
(int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height),
96, 96, PixelFormats.Pbgra32);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.