【问题标题】:Skiasharp loan image from URL and draw on Canvas in Xamarin Forms来自 URL 的 Skiasharp 贷款图像并在 Xamarin 表单中的 Canvas 上绘制
【发布时间】:2020-08-01 04:00:20
【问题描述】:

我正在寻找一种从 URL 加载图像并在 Xamarin 中使用 Skiasharp 在 SKCanvas 上绘制的方法。

https://forums.xamarin.com/discussion/97717/skiasharp-get-skbitmap-from-url

我找到了上面的链接,但不知何故我的程序在使用示例时崩溃了。

【问题讨论】:

    标签: xamarin.forms skiasharp


    【解决方案1】:

    在浏览了互联网之后,我终于使代码能够工作。下面是我的代码,其中包含更多内容,例如首先检查文件是否存在于 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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用FFImageLoading 库,它支持将Image Sourceurl 开箱即用。它使用 SkiaSharp 渲染图像。 或者,如果您喜欢冒险,您可以随时查看源代码中的实现并创建一个自定义的以满足您的需求here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-16
        • 2011-03-23
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多