0

I am having the issue of "Canvas Drawing too large bitmaps". After a quick search, I found the following thread, which promptly helped me know what is the issue.

The solution is to put the image in drawable-xxhdpi/ instead of simply drawable/. And here lies the issue: the image is not static, it is imported when I need it. As such, I do not chose where the image ends up stored. It store itself in drawable. Is there 1) A solution to chose which folder to use, or 2) a way to tell it not get the image if it's too heavy?

        var file = new SmbFile(path, auth);
            try
            {
                if (file.Exists())
                {
                    // Get readable stream.
                    var readStream = file.GetInputStream();

                    //Create reading buffer.
                    MemoryStream memStream = new MemoryStream();

                    //Get bytes.
                    ((Stream)readStream).CopyTo(memStream);

                    var stream1 = new MemoryStream(memStream.ToArray());

                    if (stream1.Length < 120188100)
                    {
                        //Save image
                        ProductImage = ImageSource.FromStream(() => stream1);

                        //Dispose readable stream.
                        readStream.Dispose();

                        InfoColSpan = 1;
                    }
                    else
                    {
                        Common.AlertError("Image trop lourde pour l'affichage");
                    }
                }
            }
Elgate
  • 113
  • 2
  • 11
  • At runtime you receive an image file? Then you cannot put it in drawable. You need to store it on the device first. After that you can try to make an image of the file. It will probably never be too heavy to store. – blackapps Dec 05 '22 at 08:51
  • I wonder why you use two memory streams. – blackapps Dec 05 '22 at 08:56
  • Isnt there a file.Length()? Or file.size()? – blackapps Dec 05 '22 at 08:57
  • @blackapps it works for most images. They show up without issue as soon as I put the stream in the in ProductImage. It's not too heavy to store, it's too heavy to display. As for the two memory stream mostly because I'm still not sure how to use streams. File does have a length. – Elgate Dec 05 '22 at 09:11
  • Ehhh.. then what is your question? – blackapps Dec 05 '22 at 09:21
  • `ProductImage = ImageSource.FromStream(() => stream1);` Try instead: `ProductImage = ImageSource.FromStream(() => file.GetInputStream());` – blackapps Dec 05 '22 at 09:24
  • @blackapps According to the linked thread, the "Canvas drawing too large bitmap" error comes from the image being too big while being stored in drawing/. If it were stored in drawing-xxhdpi/ instead, the image size would not be an issue. Thus my question was how to change the default folder so Images I get would store themselves in drawing-xxhdpi/ – Elgate Dec 05 '22 at 13:41
  • `the "Canvas drawing too large bitmap" error comes from the image being too big while being stored in drawing` ???? No you display that message when `if (stream1.Length < 120188100)` not is fullfilled. – blackapps Dec 05 '22 at 13:58
  • And i repeat: At runtime you can store nothing in anyone of those drawable folders. Those resource folders are read only. – blackapps Dec 05 '22 at 13:59
  • “It stores itself in drawable” - I agree with @blackapps, it shouldn’t go into any drawable folder. **Add to question the code that stores it.** If it’s dynamic, it should not be accessed as a drawable resource. Search for info about saving/loading image from file. – ToolmakerSteve Dec 05 '22 at 20:17

0 Answers0