Articles in this section
Category / Section

How to show online images using bitmap value in SfRotator control

2 mins read

You can show the online images using bitmap value using the ImageView class. The following steps explain how to show the online images using bitmap value in SfRotator control.

Step 1: Add the necessary assemblies in the Android projects. Refer to this UG documentation to know more about the assemblies required for adding SfRotator control to your project.

Step 2: Add the SfRotator control in the main activity class as demonstrated in the following code sample.

SfRotator rotator; 
rotator = new SfRotator(context);

 

Step 3: Add the URL in the string array as demonstrated in the following code sample.

string[] images = { "https://cdn.syncfusion.com/content/images/downloads/ebook/ebook-cover/natural-language-processing-succinctly.png", "https://cdn.syncfusion.com/content/images/downloads/ebook/ebook-cover/Xamarin-Forms-Succinctly-update-1.png", "https://cdn.syncfusion.com/content/images/downloads/ebook/ebook-cover/azure-functions-succinctly.png"};         

 

Step 4: Add the images using the SfRotatorItem class as demonstrated in the following code sample. Using SfRotatorItem class can we can show a custom view in the rotator item. We used image view in the SfRotatorItem to show the converted bitmap value as image.

  collection = new List<SfRotatorItem>();
            for (int i = 0; i < 3; i++)
            {
                SfRotatorItem sfRotatorItem = new SfRotatorItem(context);
                LinearLayout linearLayout = new LinearLayout(context);
                imageView = new ImageView(context);
                linearLayout.AddView(imageView);
                sfRotatorItem.Content = linearLayout;
                collection.Add(sfRotatorItem);
            }
            // Assign the collection of custom view to Rotator's DataSource
            rotator.DataSource = collection;

 

Step 5: Convert the URL to bitmap value using the following code sample.

private Bitmap GetImageBitmapFromUrl(string url)
        {
            Bitmap imageBitmap = null;
 
            using (var webClient = new WebClient())
            {
                var imageBytes = webClient.DownloadData(url);
                if (imageBytes != null && imageBytes.Length > 0)
                {
                    imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                }
            }
            return imageBitmap;
        }

 

Step 6: Convert the bitmap value to image using the “SetImageBitmap” property in the imageview class. The SetImageBitmap will convert the bitmap value to the image. The converted image can be displayed in the rotator control by adding the image view to the rotator item.

//convert the bitmap to image
imageView.SetImageBitmap(bit);
 //Scaling the image 
imageView.LayoutParameters = new Android.Views.ViewGroup.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
 imageView.SetAdjustViewBounds(true);
 imageView.SetScaleType(ImageView.ScaleType.FitXy);

 

Output image:

 

Online images in SfRotator

 

Sample link:

You can download the sample from this link: Sample.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied