将SKSurface转换为SKBitmap,并在不同设备尺寸上将其调整为特定大小的图像 [英] Convert SKSurface to SKBitmap and resize into a image of a specific size on different device sizes

查看:702
本文介绍了将SKSurface转换为SKBitmap,并在不同设备尺寸上将其调整为特定大小的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我需要根据在表面上绘制的图像生成准确的1060 x 550的图像

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

到目前为止,我可以看到,如果我可以将从surface.Snapshot()返回的SKImage转换为SKBitmap,则可以使用Resize()函数.我该怎么做?

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?

谢谢!

推荐答案

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

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.

这篇关于将SKSurface转换为SKBitmap,并在不同设备尺寸上将其调整为特定大小的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆