Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Bitmap display in SKIA SHARP

I downloaded a map tile (open street map) around my current location and displayed the image with DrawBitmap, but the display time is slow.

I am wondering if the download method is bad or the image display method is bad.

**↓ Download OpenStreetMap and save it on your device**
public async void DownLoadPic()
{

    try
    {

        var httpClient = new HttpClient();

        for (nY = BY; nY <= EY; nY++)
        {
            for (nX = BX; nX <= EX; nX++)
            {
                var url = "";
                string imgpath = "";

                imgpath = MapPath + "openstreetmap/" + Zoom + "/" + nX + "/" + nY + ".png";
                url = "http://c.tile.openstreetmap.org/" + Zoom + "/" + nX + "/" + nY + ".png";
                break;


                if ((System.IO.File.Exists(imgpath) == false) || (SKBitmap.Decode(imgpath) == null))
                {

                    try
                    {
                       spath = fileDL(url, MapPath + "openstreetmap/" + Zoom + "/" + nX + "/");

                        using (Stream stream = await httpClient.GetStreamAsync(url))
                        using (MemoryStream memStream = new MemoryStream())
                        {


                            if (dictTiles.Contains(imgpath))
                            {
                                img = (SKBitmap)dictTiles[imgpath];
                            }
                            else
                            {
                                await stream.CopyToAsync(memStream);
                                memStream.Seek(0, SeekOrigin.Begin);

                                if (dictTiles.Count > DictSize) dictTiles.RemoveAt(0);
                                dictTiles[imgpath] = SKBitmap.Decode(memStream);
                                img = (SKBitmap)dictTiles[imgpath];
                            }


                        }

                    }
                    catch (Exception)
                    {
                    }

                }
                else
                {

                    if (!dictTiles.Contains(imgpath))
                    {
                        try
                        {
                            if (File.Exists(imgpath) == true)
                            {
                                if (dictTiles.Count > DictSize) dictTiles.RemoveAt(0);
                                dictTiles[imgpath] = SKBitmap.Decode(imgpath);
                            }

                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    img = (SKBitmap)dictTiles[imgpath];

                }


                int iX = (nX) % tilecnt;
                int iY = (nY) % tilecnt;
                SKBitmap imgset = new SKBitmap(img.Width, img.Height);
                SKRect dest = new SKRect(0, 0, img.Width, img.Height);
                float x0 = iX * (img.Width / tilecnt);
                float y0 = iY * (img.Height / tilecnt);
                float x1 = x0 + (img.Width / tilecnt);
                float y1 = y0 + (img.Height / tilecnt);
                SKRect src = new SKRect(x0, y0, x1, y1);

                using (SKCanvas canvas = new SKCanvas(imgset))
                {
                    canvas.DrawBitmap(img, src, dest);
                    bitmaps.Add(imgset);
                }

            }
        }

    }
    catch (Exception)
    {
    }

}

[Obsolete]
private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
    try
    {
        SKImageInfo info = args.Info;
        SKSurface surface = args.Surface;
        SKCanvas canvas = surface.Canvas;

        H = Convert.ToInt32(canvasView.CanvasSize.Height);
        W = Convert.ToInt32(canvasView.CanvasSize.Width);

        //Pic();

        int imgsize = 256;
        int i = 0;
        for (int y = 0; y <= yy; y++)
        {
            for (int x = 0; x <= xx; x++)
            {
                canvas.DrawBitmap(bitmaps[i], imgsize * x - offX, imgsize * y - offY, null);

                i++;
            }
        }

    }
    catch
    {

    }
}

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I noticed that you were trying to load so many tiles in the for loop:

    for (nY = BY; nY <= EY; nY++)
            {
                for (nX = BX; nX <= EX; nX++)
    

    If you loaded a single tile, will this take so much time?
    We could also make a test to load images from the local storage.

Sign In or Register to comment.