Exemplo n.º 1
0
        private void _AddWeakCacheCallback(object sender, GetImageSourceCompletedEventArgs e)
        {
            if (e.Error != null || e.Cancelled)
            {
                return;
            }

            var bs = (BitmapSource)e.ImageSource;

            if (_isSquarish)
            {
                if (e.ImageSource.Height == 0)
                {
                    return;
                }

                double aspectRatio = e.ImageSource.Width / e.ImageSource.Height;
                // Allow for some flexibility.  This is really a special case for newsfeed filter icons and I can't really predict how Facebook is going to break things in the future.
                if (aspectRatio > 1.8 && aspectRatio < 2.5)
                {
                    // Take the right half of the image.
                    bs = new CroppedBitmap(bs, new Int32Rect((int)(.5 * bs.Width), 0, (int)(bs.Width * .5), (int)bs.Height));
                }
                else
                {
                    // If we didn't need to do cropping then we won't in the future, so just short circuit this next time.
                    _isSquarish = false;
                }
            }

            var userState = (_ImageCallbackState)e.UserState;

            switch (userState.RequestedSize)
            {
            case FacebookImageDimensions.Big:
                _bigWR = new WeakReference(bs);
                break;

            case FacebookImageDimensions.Normal:
                _normalWR = new WeakReference(bs);
                break;

            case FacebookImageDimensions.Small:
                _smallWR = new WeakReference(bs);
                break;

            case FacebookImageDimensions.Square:
                _squareWR = new WeakReference(bs);
                break;
            }

            if (userState.Callback != null)
            {
                userState.Callback(this, new GetImageSourceCompletedEventArgs(bs, this));
            }
        }
Exemplo n.º 2
0
        private void _ProcessNextLocalRequest(object unused)
        {
            while (_asyncPhotoPool != null)
            {
                // Retrieve the next data request for processing.
                GetImageSourceAsyncCallback callback = null;
                object   userState = null;
                object   sender    = null;
                SmallUri ssUri     = default(SmallUri);

                lock (_localLock)
                {
                    while (_activePhotoRequests.Count > 0)
                    {
                        _DataRequest nextDataRequest = _activePhotoRequests.Pop();
                        Assert.IsTrue(nextDataRequest.Callback is GetImageSourceAsyncCallback);
                        if (!nextDataRequest.Canceled)
                        {
                            sender    = nextDataRequest.Sender;
                            callback  = (GetImageSourceAsyncCallback)nextDataRequest.Callback;
                            userState = nextDataRequest.UserState;
                            ssUri     = nextDataRequest.SmallUri;
                            break;
                        }
                    }

                    if (ssUri == default(SmallUri))
                    {
                        Assert.AreEqual(0, _activePhotoRequests.Count);
                        return;
                    }
                }

                Assert.IsNotNull(callback);
                GetImageSourceCompletedEventArgs callbackArgs = null;
                string localCachePath = null;
                try
                {
                    localCachePath = _GetCacheLocation(ssUri);
                    if (localCachePath == null)
                    {
                        callbackArgs = new GetImageSourceCompletedEventArgs(new FileNotFoundException(), false, userState);
                    }
                    else
                    {
                        using (Stream imageStream = File.Open(localCachePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            callbackArgs = new GetImageSourceCompletedEventArgs(_GetBitmapSourceFromStream(imageStream), userState);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!string.IsNullOrEmpty(localCachePath))
                    {
                        // If the cache is bad try removing the file so we can fetch it correctly next time.
                        try
                        {
                            File.Delete(localCachePath);
                        }
                        catch { }
                    }
                    callbackArgs = new GetImageSourceCompletedEventArgs(e, false, userState);
                }

                _callbackDispatcher.BeginInvoke(callback, sender, callbackArgs);
            }
        }