我正在尝试使用Xamarin Forms解决方案中的Skia Graphics库加载和渲染图像。当我尝试渲染图像(运行Android项目)时,我会收到以下错误:
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的任何示例代码。任何示例代码或链接都将不胜感激。
预先感谢
值不能为null。参数名称:编解码
我认为您有可能在此处获得一个空对象: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中,并且该文件的构建操作必须为"嵌入式资源"。有关使用文件的更多信息,您可以参考文件。
我找不到Xamarin的任何示例代码。任何示例代码或链接都将不胜感激。
GitHub上的软件包本身具有Xamarin.forms的代码示例,您可以参考表单样本。