将SKSurface转换为SKBitmap,并在不同的设备大小上调整为特定大小的图像

时间:2018-04-15 21:12:41

标签: xamarin xamarin.forms skiasharp

我有一个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()函数。我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分: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);

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