0

In the following snippet of code I am trying to get the image directly from the network the application is supposed to work with. (Xamarin.forms)

if (!String.IsNullOrEmpty(CurrentProduct.Image))
            {

                SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8080");

                //Get Auth
                var auth = new NtlmPasswordAuthentication("CLD", "login", "password");

                string path = $@"smb://file/products/{CurrentProduct.Image}".Replace($"\\", $"/");

                //Get target's SmbFile.
                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");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.AlertError(ex, "Impossible de charger l'image");
                }

                
            }

Usually things work fine, but for a couple of days, we've received massive preview image for some products. I started getting the error that the drawing was too large.

Now a quick search allowed me to se the most common fix is to move the image in another folder (drawable-xxhdpi) which is not possible for me since I'm not choosing where I store the image here. The other proposed solution was to change the image size, but I don't know how to do it on an imagesource filetype. I tried limiting the size of the stream to avoid the issue, but to no avail.

Elgate
  • 113
  • 2
  • 11
  • if the image is too large then you need to resize it. There are numerous existing posts about resizing images in Xamarin – Jason Sep 01 '22 at 09:04
  • 1
    you have your byte[] as "stream1" in your code. and there are good examples around on how to resize. i personally use this method: https://social.msdn.microsoft.com/Forums/en-US/277a7a1b-821f-4da0-a073-cc73b4db73ea/how-to-resize-image-file-on-xamarinforms-writeablebitmap-package-cant-be-added?forum=xamarinforms – rrr Sep 01 '22 at 12:42
  • 1
    Maybe you can use glide library to load pictures, which is the solution I found: panel_IMG_back = (ImageView) findViewById(R.id.panel_IMG_back); Glide .with(this) .load(MyViewUtils.getImage(R.drawable.wallpaper) .into(panel_IMG_back); – Hongxin Sui-MSFT Sep 02 '22 at 02:02
  • I'm having the same issue for a 3:4 64MP image using a Samsung s21 device. Question is, does anyone know what the maximum size limitation is, when setting the image source for an Image ?? E.g. 256576512 sized byte[] threw the Java.Lang.RuntimeException "Canvas trying to draw too large bitmap" – chri3g91 Nov 09 '22 at 08:38

0 Answers0