Convert SKSurface to SKBitmap and resize into a image of a specific size on different device sizes

扶醉桌前 提交于 2019-12-11 16:10:02

问题


I have a Xamarin forms view where based on touch events i can alter the image

 <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>

I need to generate a image of exactly 1060 x 550 based on what is drawn on the surface

 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()); 
     }

As far i can see If i could convert the SKImage which is returned from the surface.Snapshot() into a SKBitmap, I could use the Resize() function. How do i do this?

Thanks!


回答1:


You can take picture and save it as SKImage in device folder after that you get it as stream and resize it and re-save it again

        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);

You can ameliorate and optimize the code more as your needs.



来源:https://stackoverflow.com/questions/49846848/convert-sksurface-to-skbitmap-and-resize-into-a-image-of-a-specific-size-on-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!