6

を使用しViewboxて、WPFビューに動的にバインドする一連のアイコンを作成しています。

リソース名にバインドし、を使用してConverterリソース名をに変換していImageSourceます。

リソースがである場合の方法は知っていますが、?Pathで行う方法はViewbox

これは、リソースが、の場合、リソース名を:に変換する方法PathですImageSource


public class ResourceNameToImageSourceConverter : BaseValueConverter {
    protected override ImageSource Convert(string value, System.Globalization.CultureInfo culture) {
        var resource = new ResourceDictionary();
        resource.Source = new Uri("pack://application:,,,/MyAssembly;component/MyResourceFolder/ImageResources.xaml", UriKind.Absolute);
        var path = resource[value] as Path;
        if (path != null) {
            var geometry = path.Data;
            var geometryDrawing = new GeometryDrawing();
            geometryDrawing.Geometry = geometry;
            var drawingImage = new DrawingImage(geometryDrawing);

        geometryDrawing.Brush = path.Fill;
        geometryDrawing.Pen = new Pen();

        drawingImage.Freeze();
        return drawingImage;
    } else {
        return null;
    }
}

}

And this is what the Viewbox declaration looks like.

<Viewbox>
    <Viewbox>
      <Grid>
        <Path>
        ...
        </Path>
        <Path>
        ...
        </Path>
        <Path>
        ...
        </Path>
        <Rectangle>
        ...
        </Rectangle>
      </Grid>
    </Viewbox>
</Viewbox>
4

1 に答える 1

1

ビューボックスは視覚的な要素であるため、手動でビットマップに「レンダリング」する必要があります。このブログ投稿は、これがどのように行われるかを示していますが、関連するコードは次のとおりです。

private static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY) {
    if (target == null)
        return null;

    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                (int)(bounds.Height * dpiY / 96.0),
                                                dpiX,
                                                dpiY,
                                                PixelFormats.Pbgra32);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen()) {
        VisualBrush vb = new VisualBrush(target);
        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
    }

    rtb.Render(dv);
    return rtb;
}
于 2011-03-04T06:51:05.787 に答える