Xamarin.forms Skiasharp从URL借出图像,并以Xamarin形式在画布上绘制
我正在寻找一种从URL加载图像并在Xamarin中使用Skiasharp在SKCanvas上绘制的方法Xamarin.forms Skiasharp从URL借出图像,并以Xamarin形式在画布上绘制,xamarin.forms,skiasharp,Xamarin.forms,Skiasharp,我正在寻找一种从URL加载图像并在Xamarin中使用Skiasharp在SKCanvas上绘制的方法 我找到了上面的链接,但不知何故,我的程序在使用该示例时崩溃了。在浏览了互联网之后,我终于让代码正常工作了。下面是我的代码,其中包含一些内容,例如首先检查URL中是否存在文件,以及调整图像大小以适应整个画布大小 HttpWebResponse response = null; var request = (HttpWebRequest) WebRequest.Create(url); requ
我找到了上面的链接,但不知何故,我的程序在使用该示例时崩溃了。在浏览了互联网之后,我终于让代码正常工作了。下面是我的代码,其中包含一些内容,例如首先检查URL中是否存在文件,以及调整图像大小以适应整个画布大小
HttpWebResponse response = null;
var request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "HEAD";
request.Timeout = 2000; // miliseconds
try
{
response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) //Make sure the URL is not empty and the image is there
{
// download the bytes
byte[] stream = null;
using(var webClient = new WebClient())
{
stream = webClient.DownloadData(url);
}
// decode the bitmap stream
resourceBitmap = SKBitmap.Decode(stream);
if (resourceBitmap != null)
{
var resizedBitmap = resourceBitmap.Resize(info, SKFilterQuality.High); //Resize to the canvas
canvas.DrawBitmap(resizedBitmap, 0, 0);
}
}
}
catch(Exception ex)
{
}
finally
{
// Don't forget to close your response.
if (response != null)
{
response.Close();
}
}
您可以使用
ffimageload
库,它支持将图像源
放到url
开箱即用。它使用SkiaSharp渲染图像。
或者,如果您感到有冒险精神,您可以始终在源代码中查看实现,并创建一个自定义的实现来满足您的需要