コード例 #1
0
        GetImageSourceCompletedEventArgs CreateBitmapFromStream(Stream stream)
        {
            GetImageSourceCompletedEventArgs eventArgs;

            try
            {
                // Create a BitmapImage from memory stream.
                BitmapImage bitmapImage = new BitmapImage();
#if !SILVERLIGHT
                bitmapImage.BeginInit();
                bitmapImage.StreamSource  = stream;
                bitmapImage.CacheOption   = BitmapCacheOption.OnLoad;
                bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
#else
                bitmapImage.SetSource(stream);
#endif
                eventArgs = new GetImageSourceCompletedEventArgs(bitmapImage, this);
            }
            catch (InvalidOperationException exception)
            {
                eventArgs = new GetImageSourceCompletedEventArgs(exception, false, this);
            }
            catch (Exception exception)
            {
                eventArgs = new GetImageSourceCompletedEventArgs(exception, false, this);
            }

            return(eventArgs);
        }
コード例 #2
0
        void wc_RequestCompleted(object sender, RequestCompletedEventArgs e)
        {
            WebClientHelper wc = sender as WebClientHelper;

            ContentType = wc.ContentType;

            dispatcher.BeginInvoke(new Action(() =>
            {
                GetImageSourceCompletedEventArgs eventArgs;

                if (e.Error != null)
                {
                    eventArgs = new GetImageSourceCompletedEventArgs(e.Error, false, this);
                }
                else
                {
                    try
                    {
                        eventArgs = CreateBitmapFromStream(e.Response);
                    }
                    finally
                    {
                        e.Response.Close();
                    }
                }

                GetImageSourceAsyncCallback callback = (GetImageSourceAsyncCallback)e.UserState;
                callback(this, eventArgs);
            }));
        }
        void wc_RequestCompleted(object sender, RequestCompletedEventArgs e)
        {
            WebClientHelper wc = sender as WebClientHelper;
            ContentType = wc.ContentType;

             dispatcher.BeginInvoke(new Action(()=>
                {
                    GetImageSourceCompletedEventArgs eventArgs;

                    if (e.Error != null)
                    {
                        eventArgs = new GetImageSourceCompletedEventArgs(e.Error, false, this);
                    }
                    else
                    {
                        try
                        {
                            eventArgs = CreateBitmapFromStream(e.Response);
                        }
                        finally
                        {
                            e.Response.Close();
                        }
                    }

                    GetImageSourceAsyncCallback callback = (GetImageSourceAsyncCallback)e.UserState;
                    callback(this, eventArgs);

                }));
        }
        GetImageSourceCompletedEventArgs CreateBitmapFromStream(Stream stream)
        {
            GetImageSourceCompletedEventArgs eventArgs;
            try
            {
                // Create a BitmapImage from memory stream.
                BitmapImage bitmapImage = new BitmapImage();
            #if !SILVERLIGHT
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = stream;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            #else
                bitmapImage.SetSource(stream);
            #endif
                eventArgs = new GetImageSourceCompletedEventArgs(bitmapImage, this);
            }
            catch (InvalidOperationException exception)
            {
                eventArgs = new GetImageSourceCompletedEventArgs(exception, false, this);
            }
            catch (Exception exception)
            {
                eventArgs = new GetImageSourceCompletedEventArgs(exception, false, this);
            }

            return eventArgs;
        }