【问题标题】:How to detect if cropped/clipped WPF FrameworkElement/Canvas has any content?如何检测裁剪/裁剪的 WPF FrameworkElement/Canvas 是否有任何内容?
【发布时间】:2016-03-15 01:32:52
【问题描述】:

我有一个大画布,我正试图对其进行分页打印。我可以将画布分成页面大小的部分,可以裁剪(剪辑)内容,并将每个页面传递给打印方法。不过我想跳过空白页,并且需要一些方法来检测内容是否为空白。

目前,我通过使用原始 Canvas 的 VisualBrush 将 Canvas 转换为 Rectangle 来“展平” Canvas:

public Rectangle createRectanglefromUI(UIElement ui)
{
    VisualBrush myVisualBrush = new VisualBrush();
    myVisualBrush.Visual = ui;

    Rectangle myRectangle = new Rectangle();
    double w = ((FrameworkElement)ui).ActualWidth;
    double h = ((FrameworkElement)ui).ActualHeight;
    double ratio = w / h;

    myRectangle.Width = 150;
    myRectangle.Height = 150 / ratio;
    myRectangle.StrokeThickness = 0;
    myVisualBrush.Stretch = Stretch.Uniform;
    myRectangle.Margin = new Thickness(0, 0, 0, 0);
    myRectangle.Fill = myVisualBrush;

    return (myRectangle);
}

然后我剪切生成的 Rectangle 以将其裁剪为大小:

private Canvas cropUIElement(double desiredWidth, double desiredHeight, int leftCoordinate, int topCoordinate, FrameworkElement uiELement, int scaleFactor = 1)
{
    try
    {
        // Create a clip for masking undesired content from the element
        Rect clipRectangle = new Rect(leftCoordinate * desiredWidth, topCoordinate * desiredHeight, desiredWidth, desiredHeight);
        RectangleGeometry clipGeometry = new RectangleGeometry(clipRectangle);

        ScaleTransform clipScale = new ScaleTransform(scaleFactor, scaleFactor, 0, 0);

        Rectangle flattenedUIElement = createRectanglefromUI(uiELement);

        flattenedUIElement.Clip = clipGeometry;
        flattenedUIElement.LayoutTransform = clipScale;
        flattenedUIElement.ClipToBounds = true;

        Canvas outputElement = new Canvas();

        Canvas.SetLeft(
            flattenedUIElement, -1 * scaleFactor * (leftCoordinate * desiredWidth));
        Canvas.SetTop(
             flattenedUIElement, -1 * scaleFactor * (topCoordinate * desiredHeight));

        // Configuring width and height determines the final element size.
        outputElement.Width = scaleFactor * desiredWidth;
        outputElement.Height = scaleFactor * desiredHeight;

        outputElement.Children.Add(flattenedUIElement);
            // Default behavior is ClipToBounds = false. True creates the cropping effect.
        outputElement.ClipToBounds = true;

        return outputElement;
    }
    catch (SystemException)
    {
        return null;
    }
}

我想也许我可以使用 XamlWriter 来分析生成的剪辑 Canvas 并检测是否存在任何内容,但不知道该怎么做。在表示目标页面大小的原始画布上绘制一个矩形也可能是合理的,并检查是否有任何其他元素出现在其边界/碰撞内。我不知道。 获得返回值或 null 之类的就可以了,我只需要通过整个路径找出我的裁剪部分没有内容。

【问题讨论】:

    标签: wpf canvas clip frameworkelement


    【解决方案1】:

    它的效率很低,但我最终将裁剪区域(由cropUIElement 产生)与 Canvas 的所有子项进行比较,检查 .IntersectsWith(child) 和 .Contains(child)。我最终多次迭代所有项目,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2016-07-26
      • 2014-03-04
      • 2013-10-06
      • 1970-01-01
      • 2014-05-30
      相关资源
      最近更新 更多