【问题标题】:Save didnt work the drawing WinRT保存没有工作绘图 WinRT
【发布时间】:2014-03-07 16:42:28
【问题描述】:

应用程序运行良好,但是当我要保存时,就像边缘移动图片和绘图一样,我也移动了。我做错了什么或我需要做些什么来解决这个问题。我认为是几何问题。

Xaml 代码:

<Page
x:Class="DrawingWithMe.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DrawingWithMe"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Viewbox>
    <Grid x:Name="Grid1" Height="768" Width="1366">
        <Canvas x:Name="funnyCanvas" Background="White" Margin="162,10,254,42">
            <Rectangle x:Name="Rectangle1" Fill="#FFF4F4F5" Stroke="Black"></Rectangle>
            <Image x:Name="image" Source="Assets/Test.gif" Stretch="UniformToFill"/>
        </Canvas>
    </Grid>
</Viewbox>

<Page.BottomAppBar>
    <AppBar x:Name="AppBar" Padding="10,0,10,0">
        <Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Name="Save" Content="Save" VerticalAlignment="Top" Click="Save_Click_1" Grid.Column="1"></Button>
                <Button Name="Erase" Content="Erase All" VerticalAlignment="Top" Click="Erase_Click_1" Grid.Column="2"></Button>
                <Button x:Name="Copytoclipboard" Content="Copy To ClipBoard" VerticalAlignment="Top" Click="Copytoclipboard_Click_1"></Button>
                <Button x:Name="Pastefrom" Content="Paste From ClipBoard" VerticalAlignment="Top" Click="Pastefrom_Click_1"></Button>
                <Button x:Name="Recognizeword" Content="Recognize" VerticalAlignment="Top" Click="Recognizeword_Click_1"></Button>
            </StackPanel>
        </Grid>
    </AppBar>

</Page.BottomAppBar>

</Page>

C#代码:

  public async void TestingBlit()
    {

        var backgroundBmp = await BitmapFactory.New(1, 1).FromContent(new Uri(BaseUri,       @"///Assets/Test.gif"));

        //Image foreground
        WriteableBitmap foregroundBmp;
        using (InMemoryRandomAccessStream a = new InMemoryRandomAccessStream())
        {
            await _inkManager.SaveAsync(a);
            a.Seek(0);

            foregroundBmp = await new WriteableBitmap(1,1).FromStream(a);

        }

        // Combined
        backgroundBmp.Blit(new Rect(0, 0, foregroundBmp.PixelWidth, foregroundBmp.PixelHeight), foregroundBmp,new Rect(0, 0, foregroundBmp.PixelWidth, foregroundBmp.PixelHeight), WriteableBitmapExtensions.BlendMode.ColorKeying);

        // Save
        Windows.Storage.Pickers.FileSavePicker save = new Windows.Storage.Pickers.FileSavePicker();
        save.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
        save.DefaultFileExtension = ".gif";
        save.FileTypeChoices.Add("GIF", new string[] { ".gif" });
        StorageFile filesave = await save.PickSaveFileAsync();
        Guid encoderId = Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId;
         await WinRTXamlToolkit.Imaging.WriteableBitmapSaveExtensions.SaveToFile(backgroundBmp, filesave, encoderId);
        //List<InkStroke> tmp = _inkManager.GetStrokes().ToList();
        //tmp.RemoveAt(0);
        //RenderStroke(tmp.ElementAt(0), Colors.SkyBlue, 10, 1);
         SurfaceImageSource surfaceImageSource = new SurfaceImageSource((int)Rectangle1.ActualWidth, (int)Rectangle1.ActualHeight, true);    
        ImageBrush brush = new ImageBrush();
        brush.ImageSource = image.Source;

        Rectangle1.Fill = brush;

    }

    private void RenderStroke(InkStroke stroke, Color color, double width, double opacity = 1)
    {
        // Each stroke might have more than one segments
        var renderingStrokes = stroke.GetRenderingSegments();

        //
        // Set up the Path to insert the segments
        var path = new Windows.UI.Xaml.Shapes.Path();
        path.Data = new PathGeometry();
        ((PathGeometry)path.Data).Figures = new PathFigureCollection();

        var pathFigure = new PathFigure();
        pathFigure.StartPoint = renderingStrokes.First().Position;
        ((PathGeometry)path.Data).Figures.Add(pathFigure);

        //
        // Foreach segment, we add a BezierSegment
        foreach (var renderStroke in renderingStrokes)
        {
            pathFigure.Segments.Add(new BezierSegment()
            {
                Point1 = renderStroke.BezierControlPoint1,
                Point2 = renderStroke.BezierControlPoint2,
                Point3 = renderStroke.Position
            });
        }

        // Set the general options (i.e. Width and Color)
        path.StrokeThickness = width;
        path.Stroke = new SolidColorBrush(color);

        // Opacity is used for highlighter
        path.Opacity = opacity;

        funnyCanvas.Children.Add(path);
    }

    }
}

【问题讨论】:

  • 你在哪里捕捉笔画?您的目标是 8.0 还是 8.1?如果是后者-您是否考虑过使用RenderTargetBitmap
  • 不,我没有考虑过使用 RenderTargetBitmap... 但问题是库 System.Windows.Media.Imaging.RenderTargetBitmap Media 给我一个错误,我不能使用 RenderTargetBitmap。
  • 是的,但我不能使用它,因为我拥有的版本是 8.0
  • 哦,好吧,那么也许是 Direct2D? :) WinRTXamlToolkit.Composition 为 8.0 做了一些,虽然它不支持所有的东西,所以例如不是所有的 Stretch 模式都支持路径。

标签: c# windows-runtime winrt-xaml windows-applications


【解决方案1】:

您将内容放在Viewbox 中,这将拉伸它。您需要计算矩形的屏幕坐标。

试试这个。

var scalex = MyViewbox.GetScaleChildX();
var scaley = MyViewbox.GetScaleChildY();
SurfaceImageSource surfaceImageSource = new SurfaceImageSource((int)(scalex * Rectangle1.ActualWidth), (int)(scaley * Rectangle1.ActualHeight), true); 

public static double GetChildScaleX(this Viewbox viewbox)
{
    if (viewbox.Child == null)
        throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with no child.");

    var fe = viewbox.Child as FrameworkElement;

    if (fe == null)
        throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not a FrameworkElement.");

    if (fe.ActualWidth == 0)
        throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not laid out.");

    return viewbox.ActualWidth / fe.ActualWidth;
}   

GetChildScaleY 是一样的,但有高度(取自 here)。

(确保您命名为 Viewbox

<Viewbox x:Name="MyViewbox">

【讨论】:

  • ContainerVisual有参考吗?
  • 哎呀,很抱歉,一直在阅读 WPF 方法;)。这应该对你更好。如果您安装了WinRTXamlToolkit,则无需定义扩展方法。
  • 如果我在左下角画一条线,我所做的所有标记都有效,但我需要在左下角画一条线。
  • 啊,我想我明白你在说什么了。这可能是因为您的CanvasMargin 属性。你能张贴正在发生的事情和你打算发生的事情的图片吗?例如,在您的应用中画一个圆圈,然后在常规图像编辑器中画一个圆圈并发布两者。
  • 另外,我注意到您正在创建 SurfaceImageSource,但似乎没有在任何地方使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 2017-08-24
  • 2013-03-17
相关资源
最近更新 更多