System.ArgumentOutOfRangeException:** 'Index was out of range. The Error is : Index was outside the bounds of the array

Ahmed Wassim BEN SALEM (ENISo) 241 Reputation points
2021-04-20T12:22:41.213+00:00

Hello I am trying to pass a List of bytes arrays to method as parameter ,
this is the declaration : List<byte[]> listes= new List<byte[]>();

this is how i declared the method to call : MethodDeclare(List<byte[]> listes)
and this is how i called the methods :
Methodcall(listes)

But i get that exception , anyone know what is the reason please , any help ?

Developer technologies | .NET | Xamarin
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2021-04-21T06:25:46.733+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I used your code, I got the same exception as yours.

    I read the Image from URL, use Async/await method to read it, if you get exception, because you do not get all the byte[] completely, then you execute Bitmap bitmap1= BitmapFactory.DecodeByteArray(imageBytes1, 0, imageBytes1.Length); bitmap1 is empty, List<byte[]> images is empty as well, so you need to wait the image read completely, then this exception will disappear.

    I use var result = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result; to wait the Async/await method execute completely.

       List<byte[]> images;  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
         
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
                   LoadApplication(new App());  
         
                   images = new List<byte[]>();  
                   AddData();  
         
                   MotionDetected(images);  
         
               }  
         
               public async  void AddData()  
               {  
                   
                   var result = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result;  
                   var result2 = Task.Run(async () => await Download("https://news.microsoft.com/wp-content/uploads/2014/12/452292672.jpg")).Result;  
                    
         
                   images.Add(result);  
                   images.Add(result2);  
                   
               }  
               public async Task<byte[]> Download(string url)  
               {  
                   using (HttpClient client = new HttpClient())  
                   {  
                       byte[] fileArray = await client.GetByteArrayAsync(url);  
                       return fileArray;  
                   }  
         
               }  
         
         
               public bool MotionDetected(List<byte[]> images)  
               {  
                   byte[] imageBytes1 = images[0];  
                   byte[] imageBytes2 = images[0];  
         
                  Bitmap bitmap1= BitmapFactory.DecodeByteArray(imageBytes1, 0, imageBytes1.Length);  
                  Bitmap bitmap2 = BitmapFactory.DecodeByteArray(imageBytes2, 0, imageBytes2.Length);  
         
         
                   int heightFirstImage=bitmap1.Height;  
                   int widthFirstImage = bitmap1.Width;  
         
                   int heightSecondImage = bitmap2.Height;  
                   int widthSecondImage = bitmap2.Width;  
         
                   //ImageProcessing  
                   AssetManager assets = this.Assets;  
         
                    
         
                   return true;  
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.