Esempio n. 1
0
        /// <summary>
        /// Method for drawing whole image from Intent data
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="data"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task <Bitmap> CreateBitmapImage(int reqWidth, int reqHeight, int width, int height, Intent data, Context context)
        {
            Bitmap image = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

            // Draw image into bitmap
            using (Canvas can = new Canvas(image))
            {
                can.DrawColor(Color.Black); // Set the color

                // Get the image from the stream scaled and resampled
                Bitmap bmp = Bitmap.CreateScaledBitmap(await BitmapResampler.DecodeBitmapFromStreamAsync(data.Data, reqWidth, reqHeight, context), width, height, true);
                can.DrawBitmap(bmp, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
                bmp.Recycle();
            }
            return(image);
        }
Esempio n. 2
0
        /// <summary>
        /// Method for drawing whole image from url asynchronously
        /// </summary>
        /// <param name="reqWidth"></param>
        /// <param name="reqHeight"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="url"></param>
        /// <param name="txtProgress"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task <Bitmap> CreateBitmapImage(int reqWidth, int reqHeight, int width, int height, string url, TextView txtProgress, Context context)
        {
            Bitmap image = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

            byte[] imageBytes = null;

            using (WebClient webClient = new WebClient())
            {
                try
                {
                    // Show download progress
                    webClient.DownloadProgressChanged += (s, e) =>
                    {
                        txtProgress.Text = e.ProgressPercentage.ToString() + "%";
                    };

                    // Download the image into a byte array
                    imageBytes = await webClient.DownloadDataTaskAsync(url);
                }
                catch
                {
                    return(null);
                }
            }

            if (imageBytes != null && imageBytes.Length > 0)
            {
                // Draw image into bitmap
                using (Canvas can = new Canvas(image))
                {
                    can.DrawColor(Color.Black); // Set the color

                    // Get the image from the byte array scaled and resampled
                    Bitmap bmp = Bitmap.CreateScaledBitmap(await BitmapResampler.DecodeBitmapFromStreamAsync(imageBytes, reqWidth, reqHeight, context), width, height, true);
                    can.DrawBitmap(bmp, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
                    bmp.Recycle();
                }
            }
            return(image);
        }