0

我有一个 Xamarin 表单视图,我可以根据触摸事件更改图像

 <StackLayout > 
        <skia:SKCanvasView x:Name="canvasView"
                           PaintSurface="ImageCanvas_PaintSurface"  
                           EnableTouchEvents="True" 
                           HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                           IgnorePixelScaling="True"// not sure if i need this??
                           Touch="OnTouch" />
        <Button Text="Generate Image" Clicked="CreateButton_Clicked"/>
    </StackLayout>

我需要根据在表面上绘制的内容生成正好 1060 x 550 的图像

 void ImageCanvas_PaintSurface(object sender, SKPaintSurfaceEventArgs args)
     {
            var surface = args.Surface;
            var canvas = surface.Canvas;

        /// paint bitmaps/text on canvas etc 


        // Save the image to local storage
        ViewModel.SaveImage(surface.Snapshot()); 
     }

据我所知,如果我可以将从 surface.Snapshot() 返回的 SKImage 转换为 SKBitmap,我可以使用 Resize() 函数。我该怎么做呢?

谢谢!

4

1 回答 1

1

您可以拍照并将其保存为设备文件夹中的 SKImage,然后将其作为流获取并调整大小并再次重新保存

        SKImage snapI = e.Surface.Snapshot();
        SKData pngImage = snapI.Encode();
        var x=  Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "FolderName");
        var fullpath = x+"PicName.png";
        File.WriteAllBytes(fullpath, pngImage.ToArray());
        SKBitmap bitmap = SKBitmap.Decode(fullpath);
        var dstInfo = new SKImageInfo(1060, 550);

        bitmap.Resize(dstInfo, SKBitmapResizeMethod.Hamming);

您可以根据需要更多地改进和优化代码。

于 2018-04-16T09:39:10.497 回答