private static ImageSource GetImageSource()
 {
     BitmapSource source = ImageHelper.BitmapSourceFromBitmap(new Bitmap(Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Paket.VisualStudio.Resources.NuGet.ico"))));
     Int32Rect sourceRect = new Int32Rect(0, 0, 16, 16);
     ImageSource imageSource = new CroppedBitmap(source, sourceRect);
     imageSource.Freeze();
     return imageSource;
 }
        public Task<BitmapSource> TakePartialScreenshot(Int32Rect rpRect)
        {
            return TakeScreenshot(r =>
            {
                var rResult = new CroppedBitmap(r, rpRect);
                rResult.Freeze();

                return rResult;
            });
        }
        private static WriteableBitmap CropFrame(ref Rectangle srcRect, BitmapSource source)
        {
            var x = Math.Min(srcRect.X, source.PixelWidth - 1);
            var y = Math.Min(srcRect.Y, source.PixelHeight - 1);
            x = Math.Max(x, 0);
            y = Math.Max(y, 0);

            var right = srcRect.X + srcRect.Width;
            right = Math.Min(right, source.PixelWidth);
            right = Math.Max(right, 0);

            var bottom = srcRect.Y + srcRect.Height;
            bottom = Math.Min(bottom, source.PixelHeight);
            bottom = Math.Max(bottom, 0);

            var crop = new CroppedBitmap(source, new Int32Rect(x, y, right - x, bottom - y));
            crop.Freeze();

            var bmp = BitmapFactory.ConvertToPbgra32Format(crop);
            return bmp;
        }
        /// <summary>
        /// When implemented in a derived class, returns an object that is provided as the value of
        /// the target property for this markup extension.
        /// </summary>
        /// <param name="serviceProvider">
        /// A service provider helper that can provide services for the markup extension.
        /// </param>
        /// <returns>
        /// The object value to set on the property where the extension is applied.
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            // Setting BitmapImage.SourceRect has no effect. Need to use CroppedBitmap.
            if (WindowsHelper.IsInDesignMode)
            {
                // ----- Design time:
                // Design mode requires special code when used inside a WPF styles.
                var bitmapImage = Source as BitmapImage;
                if (bitmapImage == null)
                    return null;

                var croppedBitmap = new CroppedBitmap();
                croppedBitmap.BeginInit();
                croppedBitmap.Source = new BitmapImage(bitmapImage.UriSource);
                croppedBitmap.SourceRect = SourceRect;
                croppedBitmap.EndInit();
                croppedBitmap.Freeze();

                return croppedBitmap;
            }
            else
            {
                // ----- Run time:
                var bitmapSource = Source as BitmapSource;
                if (bitmapSource == null)
                    return null;

                // Freeze bitmap for performance.
                bitmapSource.Freeze();

                var croppedBitmap = new CroppedBitmap(bitmapSource, SourceRect);
                croppedBitmap.Freeze();

                return croppedBitmap;
            }
        }
        // expects the target object as the first parameter, and the resource key as the second
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values.Length < 2)
            {
                return null;
            }

            var element = values[0] as FrameworkElement;
            var resourceKey = values[1];

            if (element == null || resourceKey == null)
            {
                return null;
            }

            if (ResourceKeyFormat != null)
            {
                resourceKey = string.Format(ResourceKeyFormat, resourceKey);
            }

            if (ResourceKeyConverter != null)
            {
                resourceKey = ResourceKeyConverter.Convert(resourceKey, targetType, ConverterParameter, culture);
            }

            if (resourceKey == null) return null;

            var resource = element.TryFindResource(resourceKey);

            if (resource != null)
            {
                if (CropRect == null) return resource;
                else if (resource is BitmapSource)
                {
                    BitmapSource bitmap = resource as BitmapSource;
                    var result = new CroppedBitmap(bitmap, CropRect);
                    result.Freeze();
                    return result;
                }
            }

            string fileName = null;
            resourceKey = values[1];

            if (StringFormat != null)
            {
                fileName = string.Format(StringFormat, resourceKey);
            }

            if (ResourceKeyConverter != null)
            {
                fileName = ResourceKeyConverter.Convert(fileName, targetType, ConverterParameter, culture).ToString();
            }

            if (fileName != null)
            {

                fileName = string.Format("pack://siteoforigin:,,,/{0}", fileName);

                try
                {
                    object imageObj = (new ImageSourceConverter()).ConvertFromString(fileName);
                    var image = imageObj as ImageSource;
                    if (image != null)
                    {
                        if (CropRect == null) return image;
                        else if (image is BitmapSource)
                        {
                            BitmapSource bitmap = image as BitmapSource;
                            var result = new CroppedBitmap(bitmap, CropRect);
                            result.Freeze();
                            return result;
                        }
                    }
                }
                catch (NullReferenceException)
                {
                }
                catch (NotSupportedException)
                {
                }
                catch (ArgumentException)
                {
                    Trace.TraceWarning("Image not in expected size: {0}", fileName);
                }
            }

            Trace.TraceWarning("Resource not found: {0} or {1}\n", resourceKey, fileName);
            return null;
        }