2017-03-08 55 views
1

我想在我的xamarin窗体解决方案中使用skia图形库加载和渲染图像。当我尝试渲染图像(运行Android项目),我收到以下错误:Xamarin窗体(安卓项目)使用Skia图形库的错误渲染图像

Value cannot be null. Parameter name: codec 

这里是代码:

void OnPainting(object sender, SKPaintSurfaceEventArgs e) 
{ 

    var surface = e.Surface; 
    var canvas = surface.Canvas; 

    canvas.Clear(SKColors.White); 

    var filename = "test.jpg"; 

    using (var stream = new SKFileStream(filename)) 
    using (var bitmap = SKBitmap.Decode(stream)) // the error occurs on this line 
    using (var paint = new SKPaint()) 
    { 
     canvas.DrawBitmap(bitmap, SKRect.Create(200, 200), paint); 
    } 
} 

我找不到任何的示例代码在线xamarin。任何示例代码或链接将不胜感激。

在此先感谢

回答

3

Value cannot be null. Parameter name: codec

我认为这是可能的,你得到一个空物体的位置:using (var stream = new SKFileStream(filename))。我试图创建一个演示,它工作正常。

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:skiaviews="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms" 
      x:Class="FormsIssue6.Page1"> 
    <Grid> 
     <skiaviews:SKCanvasView x:Name="mycanvas" PaintSurface="OnPainting" /> 
    </Grid> 
</ContentPage> 

后面的代码:

private void OnPainting(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) 
{ 
    var surface = e.Surface; 
    var canvas = surface.Canvas; 

    var assembly = typeof(Page1).GetTypeInfo().Assembly; 
    var fileStream = assembly.GetManifestResourceStream("YOUR-FILE-FULL-NAME"); 
    // clear the canvas/fill with white 
    canvas.DrawColor(SKColors.White); 

    // decode the bitmap from the stream 
    using (var stream = new SKManagedStream(fileStream)) 
    using (var bitmap = SKBitmap.Decode(stream)) 
    using (var paint = new SKPaint()) 
    { 
     // create the image filter 
     using (var filter = SKImageFilter.CreateBlur(5, 5)) 
     { 
      paint.ImageFilter = filter; 

      // draw the bitmap through the filter 
      canvas.DrawBitmap(bitmap, SKRect.Create(640, 480), paint); 
     } 
    } 
} 

在上面的代码中的文件名应该是像“你的项目名称空间”的“文件名”,并将该文件被加入到。该文件的PCL和构建动作必须是“嵌入式资源”。有关使用文件的更多信息,请参阅Files

I cannot find any sample code online for xamarin. Any sample code or links would be much appreciated.

在GitHub上包本身有Xamarin.Forms代码示例,您可以参考FormsSample

+0

它正在工作,谢谢! – noobie