Utility methods for drawing with WPF.
All methods are static.
示例#1
0
        GetImageSynchronous
        (
            String uriString
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(uriString));
            AssertValid();

            const Int32 ErrorImageWidthAndHeight = 52;
            Uri         oUri;

            if (Uri.TryCreate(uriString, UriKind.Absolute, out oUri))
            {
                if (oUri.Scheme == Uri.UriSchemeHttp ||
                    oUri.Scheme == Uri.UriSchemeHttps)
                {
                    try
                    {
                        return(GetImageSynchronousHttp(oUri));
                    }
                    catch (WebException)
                    {
                        // (These empty catch blocks cause an error image to be
                        // returned.)
                    }
                    catch (ArgumentException)
                    {
                        /*
                         * Attempting to download a possibly corrupt image from this
                         * Twitter URL:
                         *
                         * http://a1.twimg.com/profile_images/1112245377/
                         *  Photo_on_2010-08-27_at_18.51_normal.jpg
                         *
                         * raised the following exception:
                         *
                         * [ArgumentException]: An invalid character was found in text
                         *  content.
                         * at System.Windows.Media.Imaging.BitmapFrameDecode.
                         *  get_ColorContexts()
                         * at System.Windows.Media.Imaging.BitmapImage.
                         *  FinalizeCreation()
                         * at System.Windows.Media.Imaging.BitmapImage.EndInit()
                         */
                    }
                    catch (FileFormatException)
                    {
                        /*
                         * A user attempted to download a possibly corrupt image from
                         * Twitter on 5/6/2011.  The exact URL wasn't determined.
                         *
                         * The following exception was raised:
                         *
                         * [FileFormatException]: The image format is unrecognized.
                         * [COMException]: Exception from HRESULT: 0x88982F07 at
                         * System.Windows.Media.Imaging.BitmapDecoder.
                         * SetupDecoderFromUriOrStream(Uri uri, Stream stream,
                         * BitmapCacheOption cacheOption, Guid& clsId, Boolean&
                         * isOriginalWritable, Stream& uriStream,
                         * UnmanagedMemoryStream& unmanagedMemoryStream,
                         * SafeFileHandle& safeFilehandle)
                         */
                    }
                    catch (IOException)
                    {
                        /*
                         * Attempting to download a possibly corrupt image from this
                         * Twitter URL:
                         *
                         * http://a2.twimg.com/profile_images/1126755034/
                         * 8FjbVD_normal.gif
                         *
                         * raised the following exception:
                         *
                         * [IOException]: Cannot read from the stream.
                         * [COMException]: Exception from HRESULT: 0x88982F72
                         * at System.Windows.Media.Imaging.BitmapDecoder.
                         * SetupDecoderFromUriOrStream(Uri uri, Stream stream,
                         * BitmapCacheOption cacheOption, Guid& clsId, Boolean&
                         * isOriginalWritable, Stream& uriStream,
                         * UnmanagedMemoryStream& unmanagedMemoryStream,
                         * SafeFileHandle& safeFilehandle)
                         * ...
                         */
                    }
                    catch (NotSupportedException)
                    {
                        /*
                         * Attempting to download a possibly corrupt image from a
                         * Twitter URL (didn't record the URL) raised the following
                         * exception:
                         *
                         * [NotSupportedException]: No imaging component suitable
                         * to complete this operation was found.
                         */
                    }
                }
                else if (oUri.Scheme == Uri.UriSchemeFile)
                {
                    try
                    {
                        // This is a synchronous operation.

                        BitmapImage oBitmapImage = new BitmapImage(oUri);

                        WpfGraphicsUtil.FreezeIfFreezable(oBitmapImage);

                        return(oBitmapImage);
                    }
                    catch (IOException)
                    {
                    }
                    catch (ArgumentException)
                    {
                        // This occurs when the URI contains an invalid character,
                        // such as "<".
                    }
                    catch (WebException)
                    {
                        // This occurs when a non-existent drive letter is used.
                    }
                    catch (NotSupportedException)
                    {
                        // Invalid image file.
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // The URI is actually a folder, for example.
                    }
                }
            }

            if (m_oCachedErrorImage == null)
            {
                m_oCachedErrorImage = CreateErrorImage(ErrorImageWidthAndHeight);
            }

            return(m_oCachedErrorImage);
        }