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