UWP的DrawingVisual等价物

时间:2015-11-27 22:14:14

标签: c# wpf uwp

我有一个人的位图图像,我想在人脸上画一个矩形。我有矩形的坐标(x,y,宽度,高度)。像这样:

enter image description here

如果我使用WPF,那么我可以使用DrawingVisualDrawingContext类来实现这一点。不幸的是,它们在UWP中不可用。是否有可用的等效API或NuGet包来实现相同的功能?

2 个答案:

答案 0 :(得分:1)

我正在使用'WriteableBitmapEx'Nuget Package解决了这个问题。 更多信息是http://kaki104.tistory.com/517

示例代码:

private async void BrowseButton_Click(object sender, RoutedEventArgs e)
{
    //FileOpePicker
    var openDlg = new FileOpenPicker();
    openDlg.FileTypeFilter.Add(".jpg");
    openDlg.FileTypeFilter.Add(".jpeg");
    openDlg.FileTypeFilter.Add(".png");
    //Open file selection window
    var result = await openDlg.PickSingleFileAsync();
    if (result == null || !result.IsAvailable) return;
    var file = await StorageFile.GetFileFromPathAsync(result.Path);
    var property = await file.Properties.GetImagePropertiesAsync();
    //Create bitmap from image size
    var writeableBmp = BitmapFactory.New((int)property.Width, (int)property.Height);
    using (writeableBmp.GetBitmapContext())
    {
        //Load bitmap from image file
        using (var fileStream = await file.OpenAsync(FileAccessMode.Read))
        {
            writeableBmp = await BitmapFactory.New(1, 1).FromStream(fileStream, BitmapPixelFormat.Bgra8);
        }
    }

    //find face that DetectAsync Face API
    using (var imageFileStream = await file.OpenStreamForReadAsync())
    {
        var faces = await faceServiceClient.DetectAsync(imageFileStream);
        if (faces == null) return;
        //display rect
        foreach (var face in faces)
        {
            writeableBmp.DrawRectangle(face.FaceRectangle.Left, face.FaceRectangle.Top,
            face.FaceRectangle.Left + face.FaceRectangle.Width,
            face.FaceRectangle.Top + face.FaceRectangle.Height, Colors.Red);
        }
    }
    FacePhoto.Source = writeableBmp;
}

答案 1 :(得分:0)

我提出的一个解决方案是在Grid中包含图像并在其上放置一个ItemsControl,它绑定到一组矩形和一个Canvas作为面板模板:

<Grid>
    <Image Source="{Binding Image}" />
    <ItemsControl ItemsSource="{Binding Rectangles}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Helper:BindingHelper.CanvasLeftBindingPath" Value="Left" />
                <Setter Property="Helper:BindingHelper.CanvasTopBindingPath" Value="Top" />
             </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Width="{Binding Width}"
                           Height="{Binding Height}"
                           Stroke="{Binding Color}"
                           StrokeThickness="6">
                </Rectangle>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

由于UWP不支持样式设置器中的Binding,因此我必须使用绑定助手。更多关于这一点可以在这里找到 UWP Binding in Style Setter not working