public static Bitmap DecodeBitmap(string path, int desiredSize)
 {
     var options = new BitmapFactory.Options { InJustDecodeBounds = true };
     BitmapFactory.DecodeFile(path, options);
     options = new BitmapFactory.Options { InSampleSize = desiredSize };
     return BitmapFactory.DecodeFile(path, options);
 }
		public static Bitmap DecodeSampleBitmapFromFile(string path, int reqWidth, int reqHeight)
		{

			try
			{
				//______________________________________________________________
				// First decode with inJustDecodeBounds=true to check dimensions
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.InJustDecodeBounds = true;
				BitmapFactory.DecodeFile(path, options);
				//BitmapFactory.DecodeStream(url.OpenConnection().InputStream, null, options);

				//______________________
				// Calculate inSampleSize
				options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);

				//____________________________________
				// Decode bitmap with inSampleSize set
				options.InJustDecodeBounds = false;
				return BitmapFactory.DecodeFile(path, options);
			}
			catch (System.Exception ex)
			{
				Log.Debug("DecodeBitmapFromFile: ", ex.Message);
				return null;
			}
			finally
			{
				//
			}
		}
示例#3
0
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            var largeImage = (CustomImage)Element;

            if ((Element.Width > 0 && Element.Height > 0 && !isDecoded) || (e.PropertyName == "ImageSource" && largeImage.ImageSource != null))
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.InJustDecodeBounds = true;

                //Get the resource id for the image
                if (largeImage.ImageSource != null)
                {
                    var field = typeof(Resource.Drawable).GetField(largeImage.ImageSource.Split('.').First());
                    var value = (int)field.GetRawConstantValue();

                    BitmapFactory.DecodeResource(Context.Resources, value, options);

                    var width = (int)Element.Width;
                    var height = (int)Element.Height;
                    options.InSampleSize = CalculateInSampleSize(options, width, height);

                    options.InJustDecodeBounds = false;
                    var bitmap = BitmapFactory.DecodeResource(Context.Resources, value, options);

                    Control.SetImageBitmap(bitmap);

                    isDecoded = true;
                }
            }

        }
		public SimulationView (Context context, SensorManager sensorManager, IWindowManager window)
			: base (context)
		{
			Bounds = new PointF ();

			// Get an accelorometer sensor
			sensor_manager = sensorManager;
			accel_sensor = sensor_manager.GetDefaultSensor (SensorType.Accelerometer);

			// Calculate screen size and dpi
			var metrics = new DisplayMetrics ();
			window.DefaultDisplay.GetMetrics (metrics);

			meters_to_pixels_x = metrics.Xdpi / 0.0254f;
			meters_to_pixels_y = metrics.Ydpi / 0.0254f;

			// Rescale the ball so it's about 0.5 cm on screen
			var ball = BitmapFactory.DecodeResource (Resources, Resource.Drawable.Ball);
			var dest_w = (int)(BALL_DIAMETER * meters_to_pixels_x + 0.5f);
			var dest_h = (int)(BALL_DIAMETER * meters_to_pixels_y + 0.5f);
			ball_bitmap = Bitmap.CreateScaledBitmap (ball, dest_w, dest_h, true);

			// Load the wood background texture
			var opts = new BitmapFactory.Options ();
			opts.InDither = true;
			opts.InPreferredConfig = Bitmap.Config.Rgb565;
			wood_bitmap = BitmapFactory.DecodeResource (Resources, Resource.Drawable.Wood, opts);
			wood_bitmap2 = BitmapFactory.DecodeResource (Resources, Resource.Drawable.Wood, opts);

			display = window.DefaultDisplay;
			particles = new ParticleSystem (this);
		}
示例#5
0
        public async Task<Bitmap> GetResizedBitmapAsync(string path, int width, int height)
        {
            if (_originalBitmap != null)
            {
                return _originalBitmap;
            }

            #region Get some some information about the bitmap so we can resize it
            BitmapFactory.Options options = new BitmapFactory.Options
                                            {
                                                InJustDecodeBounds = true
                                            };

            using (Stream stream = _context.Assets.Open(path))
            {
                await BitmapFactory.DecodeStreamAsync(stream);
            }
            await BitmapFactory.DecodeFileAsync(path, options);
            #endregion

            // Calculate the factor by which we should reduce the image by
            options.InSampleSize = CalculateInSampleSize(options, width, height);

            #region Go and load the image and resize it at the same time.
            options.InJustDecodeBounds = false;

            using (Stream inputSteam = _context.Assets.Open(path))
            {
                _originalBitmap = await BitmapFactory.DecodeStreamAsync(inputSteam);
            }
            #endregion

            return _originalBitmap;
        }
示例#6
0
        // Get a bitmap drawable from a local file that is scaled down
        // to fit the current window size
        public static BitmapDrawable GetScaledDrawable(Activity a, string path)
        {
            Display display = a.WindowManager.DefaultDisplay;
            float destWidth = display.Width;
            float destHeight = display.Height;

            // Read in the dimensions of the image on disk
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InJustDecodeBounds = true;
            BitmapFactory.DecodeFile(path, options);

            float srcWidth = options.OutWidth;
            float srcHeight = options.OutHeight;

            double inSampleSize = 1.0;
            if (srcHeight > destHeight || srcWidth > destWidth) {
                if (srcWidth > srcHeight) {
                    inSampleSize = Math.Round(srcHeight / destHeight);
                }
                else {
                    inSampleSize = Math.Round(srcWidth / destWidth);
                }
            }

            options = new BitmapFactory.Options();
            options.InSampleSize = (int)inSampleSize;

            Bitmap bitmap = BitmapFactory.DecodeFile(path, options);
            var bDrawable= new BitmapDrawable(a.Resources, bitmap);
            bitmap = null;

            return bDrawable;
        }
		public static Bitmap Scale (int scaleFactor, Stream iStream)
		{
			var bmOptions = new BitmapFactory.Options ();
			bmOptions.InJustDecodeBounds = false;
			bmOptions.InSampleSize = scaleFactor;
			return BitmapFactory.DecodeStream (iStream, null, bmOptions);
		}
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            var largeImage = (XLargeImage.SourceCode.Controls.XLargeImage)Element;

            if ((!(Element.Width > 0) || !(Element.Height > 0) || _isDecoded) &&
                (e.PropertyName != "ImageSource" || largeImage.ImageSource == null)) return;
            var options = new BitmapFactory.Options {InJustDecodeBounds = true};

            //Get the resource id for the image
            var field = typeof(Resource.Drawable).GetField(largeImage.ImageSource.Split('.').First());
            if(field == null) return;
            var value = (int)field.GetRawConstantValue();

            BitmapFactory.DecodeResource(Context.Resources, value, options);

            //The with and height of the elements (XLargeImage) will be used to decode the image
            var width = (int)Element.Width;
            var height = (int)Element.Height;
            options.InSampleSize = CalculateInSampleSize(options, width, height);

            options.InJustDecodeBounds = false;
            var bitmap = BitmapFactory.DecodeResource(Context.Resources, value, options);

            //Set the bitmap to the native control
            Control.SetImageBitmap(bitmap);

            _isDecoded = true;
        }
        public override void SetValue(object value)
        {
            var imageView = ImageView;
            if (imageView == null)
            {
                // weak reference is garbage collected - so just return
                return;
            }

            try
            {
                var assetStream = GetStream(value);
                if (assetStream == null)
                    return;

                var options = new BitmapFactory.Options {InPurgeable = true};
                var bitmap = BitmapFactory.DecodeStream(assetStream, null, options);
                var drawable = new BitmapDrawable(Resources.System, bitmap);
                imageView.SetImageDrawable(drawable);
            }
            catch (Exception ex)
            {
                MvxTrace.Error(ex.ToLongString());
                throw;
            }
        }
示例#10
0
		public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
		{
			// First we get the the dimensions of the file on disk
			BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
			BitmapFactory.DecodeFile(fileName, options);

			// Next we calculate the ratio that we need to resize the image by
			// in order to fit the requested dimensions.
			int outHeight = options.OutHeight;
			int outWidth = options.OutWidth;
			int inSampleSize = 1;

			if (outHeight > height || outWidth > width)
			{
				inSampleSize = outWidth > outHeight
					? outHeight / height
					: outWidth / width;
			}

			// Now we will load the image and have BitmapFactory resize it for us.
			options.InSampleSize = inSampleSize;
			options.InJustDecodeBounds = false;
			Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);

			return resizedBitmap;
		}
        public static AccidentMediaViewModel GetImageSize(string fileName)
        {

            var options = new BitmapFactory.Options
            {
                InJustDecodeBounds = true
            };

            File file = new File(fileName);
            long lengthMB = file.Length() / 1024;

            BitmapFactory.DecodeFile(fileName, options);
            int width = options.OutWidth;
            int height = options.OutHeight;
            string type = options.OutMimeType;

           
            file.Dispose();
            return new AccidentMediaViewModel
            {
                Height = height,
                MimeType = type,
                Filename = fileName,
                SizeKb = lengthMB,
                Width = width
            };

        }
示例#12
0
 public static Bitmap DecodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
 {
     BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
     BitmapFactory.DecodeResource(res, resId, options);
     options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
     options.InJustDecodeBounds = false;
     return BitmapFactory.DecodeResource(res, resId, options);
 }
示例#13
0
        private Bitmap img; // The object bitmap

        #endregion Fields

        #region Constructors

        public DragObject(Resources res, int drawableIntID)
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InJustDecodeBounds = true;
            img = BitmapFactory.DecodeResource(res, drawableIntID);
            id = count;
            count++;
        }
示例#14
0
 public static Bitmap DecodeFromByteArray(byte[] bytes, int reqWidth, int reqHeight)
 {
     BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
     BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length, options);
     options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
     options.InJustDecodeBounds = false;
     return BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length, options);
 }    
示例#15
0
 /// <summary>
 /// Helper method to get the sample size of the image for resampling.
 /// </summary>
 public static int ToSampleSize (this byte [] bytes)
 {
     var sampleSize = 0;
     BitmapFactory.Options options = new BitmapFactory.Options ();
     options.InJustDecodeBounds = true;
     BitmapFactory.DecodeByteArray (bytes, 0, bytes.Length, options);
     sampleSize = (int)Math.Ceiling ((double)Math.Max (options.OutWidth / Constants.MaxWidth, options.OutHeight / Constants.MaxHeight));
     return sampleSize;
 }
		public static int FindScaleFactor (int targetWidth, int targetHeight, Stream iStream)
		{
			var bmOptions = new BitmapFactory.Options ();
			bmOptions.InJustDecodeBounds = true;
			BitmapFactory.DecodeStream (iStream, null, bmOptions);
			int actualWidth = bmOptions.OutWidth;
			int actualHeight = bmOptions.OutHeight;
			return Math.Min (actualWidth / targetWidth, actualHeight / targetHeight);
		}
示例#17
0
		public Bitmap DecodeBitmapFromStream (Android.Net.Uri url)
		{			
			using (Stream stream = _context.ContentResolver.OpenInputStream ( url )) {
				BitmapFactory.Options options = new BitmapFactory.Options ();
				options.InJustDecodeBounds = false;
				Bitmap bitmap = BitmapFactory.DecodeStream (stream, null, options);
				return bitmap;
			}
		}
示例#18
0
 public DragObject(Resources res, int drawable, Point point)
 {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.InJustDecodeBounds = true;
     img = BitmapFactory.DecodeResource(res, drawable);
     id = count;
     count++;
     coordX = point.X;
     coordY = point.Y;
 }
示例#19
0
		public static Bitmap Base64StringtoBitmap (string base64)
		{
			byte[] decodedByte = Base64.Decode (base64, 0);
			var option = new BitmapFactory.Options();
			option.InPurgeable = true;
			option.InScaled = false;
			
			Bitmap image = BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length, option);
			return image;
		}
示例#20
0
		public async Task<Bitmap> downloadAsync (string uri)
		{
		 
			if (uri == null)
				return null;

			int index = uri.LastIndexOf ("/");
			string localFilename = uri.Substring (index + 1);
			webClient = new WebClient ();
			var url = new Uri (uri);
			byte[] bytes = null;


			try {

				bytes = await webClient.DownloadDataTaskAsync (url);
			} catch (TaskCanceledException Ex) {
				Log.Error ("downloadAsync:", Ex.Message);
				return null;
			} catch (Exception Ex) {
			
				Log.Error ("downloadAsync:", Ex.Message);

				return null;
			}

			string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);	

			string localPath = System.IO.Path.Combine (documentsPath, localFilename);
		

			//Sive the Image using writeAsync
			FileStream fs = new FileStream (localPath, FileMode.OpenOrCreate);
			await fs.WriteAsync (bytes, 0, bytes.Length);

			//Console.WriteLine("localPath:"+localPath);
			fs.Close ();


			BitmapFactory.Options options = new BitmapFactory.Options ();
			options.InJustDecodeBounds = true;
			await BitmapFactory.DecodeFileAsync (localPath, options);

			//	options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / imageview.Height : options.OutWidth / imageview.Width;
			options.InJustDecodeBounds = false;

			Bitmap bitmap = await BitmapFactory.DecodeFileAsync (localPath, options);



			return (bitmap);

		
		}
示例#21
0
        private static int GetSmallestDimensionOfImage(ContentResolver cr, Uri uri)
        {
            using (var inputStream = cr.OpenInputStream(uri))
            {
                var justSizeOptions = new BitmapFactory.Options();
                justSizeOptions.InJustDecodeBounds = true;

                BitmapFactory.DecodeStream(inputStream, new Rect(), justSizeOptions);

                return Math.Min(justSizeOptions.OutHeight, justSizeOptions.OutWidth);
            }
        }
示例#22
0
        //public static Task<Bitmap> DecodeBitmapAsync(string path,  int desiredWidth, int desiredHeight)
        // {
        //     return Task.Factory.StartNew(() => DecodeBitmap(path, desiredSize));
        // }

        public static Bitmap DecodeStream(Stream stream,int width, int height, int desiredWidth, int desiredHeight)
        {
            var sampleSize = 1;
            if (height > desiredHeight || width > desiredWidth)
            {
                var heightRatio = Math.Round(height / (float)desiredHeight);
                var widthRatio = Math.Round(width / (float)desiredWidth);
                sampleSize = Math.Min(heightRatio, widthRatio);
            }
            var options = new BitmapFactory.Options { InSampleSize = sampleSize };
            return BitmapFactory.DecodeStream(stream, null, options);
        }
 public static Bitmap loadingBitmapFromPath(string path, Context c, int reqheight, int reqwidth)
 {
     BitmapFactory.Options bmOptions = new BitmapFactory.Options ();
     bmOptions.InJustDecodeBounds = true;
     BitmapFactory.DecodeResource (c.Resources, Resource.Drawable.ic_store_default, bmOptions);
     bmOptions.InSampleSize = calculateInputSample (bmOptions, reqheight, reqwidth);
     bmOptions.InJustDecodeBounds = false;
     Bitmap bitmap = null;
     if (File.Exists (path))
         bitmap = BitmapFactory.DecodeFile (path, bmOptions);
     return bitmap;
 }
        public void DecodeImage(string imagePath)
        {
            // Get the dimensions of the bitmap
            var options = new BitmapFactory.Options { InJustDecodeBounds = true };
            BitmapFactory.DecodeFile(imagePath, options);

            options.InJustDecodeBounds = false;
            options.InSampleSize = 2;
            options.InPurgeable = true;

            _imageBitmap = BitmapFactory.DecodeFile(imagePath, options);
            ImageViewer.SetImageBitmap(_imageBitmap);
        }
        Bitmap MakeImage(string url)
        {
            Bitmap bm = null;
            try {
                BitmapFactory.Options options = new BitmapFactory.Options ();
                options.InSampleSize = 2;
                bm = BitmapFactory.DecodeFile (url, options);
            } catch (Exception ex) {

            }

            return bm;
        }
示例#26
0
        public Task<IBitmap> Load(Stream sourceStream, float? desiredWidth, float? desiredHeight)
        {
            if (desiredWidth == null) {
                return Task.Run(() => BitmapFactory.DecodeStream(sourceStream).FromNative());
            }

            var opts = new BitmapFactory.Options() {
                OutWidth = (int)desiredWidth.Value,
                OutHeight = (int)desiredHeight.Value,
            };
            var noPadding = new Rect(0, 0, 0, 0);
            return Task.Run(() => BitmapFactory.DecodeStream(sourceStream, noPadding, opts).FromNative());
        }
        private static Bitmap DecodeSampledBitmapFromFile(string path, int w, int h)
        {
            if (!File.Exists(path))
                return null;
            var options = new BitmapFactory.Options();
            options.InJustDecodeBounds = true;

            BitmapFactory.DecodeFile(path, options);
            options.InJustDecodeBounds = false;
            options.InSampleSize = CalculateInSampleSize(options, w, h);

            return BitmapFactory.DecodeFile(path, options);
        }
        protected override bool GetBitmap(object value, out Bitmap bitmap)
        {
            var assetStream = GetStream(value);
            if (assetStream == null)
            {
                bitmap = null;
                return false;
            }

            var options = new BitmapFactory.Options { InPurgeable = true };
            bitmap = BitmapFactory.DecodeStream(assetStream, null, options);
            return true;
        }
示例#29
0
        private void PictureTaken(object sender, PictureTakenArgs e)
        {
            RunOnUiThread(() =>
            {
                //Reducing the size to avoid OOM exceptions
                var options = new BitmapFactory.Options();
                options.InJustDecodeBounds = false;
                options.InSampleSize = 8;

                var bitmap = BitmapFactory.DecodeByteArray(e.Image, 0, e.Image.Length, options);
                _imagePreview.SetImageBitmap(bitmap);
                _previewLayout.Visibility = ViewStates.Visible;
            });
        }
        private static BitmapFactory.Options GetBitmapOptionsOfImage(string filename)
        {
            BitmapFactory.Options options = new BitmapFactory.Options {
                InJustDecodeBounds = true
            };

            // The result will be null because InJustDecodeBounds == true.
            Bitmap result = BitmapFactory.DecodeFile(filename, options);

            int imageHeight = options.OutHeight;
            int imageWidth = options.OutWidth;

            return options;
        }