FreezeIfFreezable() public static method

public static FreezeIfFreezable ( System.Windows.Freezable freezable ) : System.Boolean
freezable System.Windows.Freezable
return System.Boolean
        GetPathGeometryFromPathSegments
        (
            Point oStartPoint,
            Boolean bPathFigureIsFilled,
            params PathSegment[] aoPathSegments
        )
        {
            Debug.Assert(aoPathSegments != null);

            PathFigure oPathFigure = new PathFigure();

            oPathFigure.StartPoint = oStartPoint;
            oPathFigure.IsFilled   = bPathFigureIsFilled;
            PathSegmentCollection oSegments = oPathFigure.Segments;

            foreach (PathSegment oPathSegment in aoPathSegments)
            {
                WpfGraphicsUtil.FreezeIfFreezable(oPathSegment);
                oSegments.Add(oPathSegment);
            }

            WpfGraphicsUtil.FreezeIfFreezable(oPathFigure);
            PathGeometry oPathGeometry = new PathGeometry();

            oPathGeometry.Figures.Add(oPathFigure);
            WpfGraphicsUtil.FreezeIfFreezable(oPathGeometry);

            return(oPathGeometry);
        }
示例#2
0
        ResizeImage
        (
            BitmapSource image,
            Int32 widthNew,
            Int32 heightNew
        )
        {
            Debug.Assert(image != null);
            Debug.Assert(widthNew > 0);
            Debug.Assert(heightNew > 0);
            AssertValid();

            // The following code was adapted from this post:
            //
            //   http://chironexsoftware.com/blog/?p=55

            TransformedBitmap resizedImage = new TransformedBitmap(image,

                                                                   new ScaleTransform(
                                                                       (Double)widthNew / (Double)image.PixelWidth,
                                                                       (Double)heightNew / (Double)image.PixelHeight,
                                                                       0, 0)
                                                                   );

            WpfGraphicsUtil.FreezeIfFreezable(resizedImage);

            return(resizedImage);
        }
示例#3
0
        GetImageSynchronousHttp
        (
            Uri uri
        )
        {
            Debug.Assert(uri != null);

            Debug.Assert(
                uri.Scheme == Uri.UriSchemeHttp
                ||
                uri.Scheme == Uri.UriSchemeHttps
                );

            AssertValid();

            String sTemporaryFilePath =
                GetTemporaryFilePathForDownloadedImage(uri);

            try
            {
                // Download the file using a simple synchronous technique.

                (new WebClient()).DownloadFile(uri, sTemporaryFilePath);

                BitmapImage oBitmapImage = new BitmapImage();
                oBitmapImage.BeginInit();
                oBitmapImage.CacheOption = BitmapCacheOption.OnLoad;

                // Some Twitter images have corrupted color profiles, which causes
                // BitmapImage.EndInit() to raise an ArgumentException with the
                // message "Value does not fall within the expected range."  Follow
                // the suggestion at the following page and ignore color profiles.
                //
                // http://www.hanselman.com/blog/
                // DealingWithImagesWithBadMetadataCorruptedColorProfilesInWPF.aspx

                oBitmapImage.CreateOptions =
                    BitmapCreateOptions.IgnoreColorProfile;

                // Synchronously load the bitmap with the contents of the
                // downloaded file.

                using (FileStream oFileStream =
                           new FileStream(sTemporaryFilePath, FileMode.Open))
                {
                    oBitmapImage.StreamSource = oFileStream;
                    oBitmapImage.EndInit();
                }

                WpfGraphicsUtil.FreezeIfFreezable(oBitmapImage);

                return(oBitmapImage);
            }
            finally
            {
                File.Delete(sTemporaryFilePath);
            }
        }
示例#4
0
        GetImageSynchronousIgnoreDpi
        (
            String uriString
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(uriString));
            AssertValid();

            BitmapSource oBitmapSource = GetImageSynchronous(uriString);

            if (oBitmapSource.DpiX != m_dScreenDpi)
            {
                // The DPI properties of BitmapSource are read-only.  To work
                // around this, copy the BitmapSource to a new BitmapSource,
                // changing the DPI in the process.  Wasteful, but there doesn't
                // seem to be a better way.

                // The formula for stride, which isn't documented in MSDN, was
                // taken from here:
                //
                // http://social.msdn.microsoft.com/Forums/en/windowswic/thread/
                // 8e2d2dbe-6819-488b-ac49-bf5235d87bc4

                Int32 iStride = ((oBitmapSource.PixelWidth *
                                  oBitmapSource.Format.BitsPerPixel) + 7) / 8;

                // Also, see this:
                //
                // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/
                // 56364b28-1277-4be8-8d45-78788cc4f2d7/

                Byte [] abtPixels = new Byte[oBitmapSource.PixelHeight * iStride];

                oBitmapSource.CopyPixels(abtPixels, iStride, 0);

                oBitmapSource = BitmapSource.Create(oBitmapSource.PixelWidth,
                                                    oBitmapSource.PixelHeight, m_dScreenDpi, m_dScreenDpi,
                                                    oBitmapSource.Format, oBitmapSource.Palette, abtPixels, iStride
                                                    );

                WpfGraphicsUtil.FreezeIfFreezable(oBitmapSource);
            }

            return(oBitmapSource);
        }
示例#5
0
        DrawingVisualToRenderTargetBitmap
        (
            DrawingVisual oDrawingVisual,
            Int32 iWidth,
            Int32 iHeight
        )
        {
            Debug.Assert(oDrawingVisual != null);
            Debug.Assert(iWidth > 0);
            Debug.Assert(iHeight > 0);
            AssertValid();

            RenderTargetBitmap oRenderTargetBitmap = new RenderTargetBitmap(
                iWidth, iHeight, 96.0, 96.0, PixelFormats.Pbgra32);

            oRenderTargetBitmap.Render(oDrawingVisual);
            WpfGraphicsUtil.FreezeIfFreezable(oRenderTargetBitmap);

            return(oRenderTargetBitmap);
        }
        GetQuadraticBezierCurve
        (
            Point startPoint,
            Point endPoint,
            Point controlPoint
        )
        {
            QuadraticBezierSegment oQuadraticBezierSegment =
                new QuadraticBezierSegment(controlPoint, endPoint, true);

            PathFigure oPathFigure = new PathFigure();

            oPathFigure.StartPoint = startPoint;
            oPathFigure.Segments.Add(oQuadraticBezierSegment);

            PathGeometry oQuadraticBezierCurve = new PathGeometry();

            oQuadraticBezierCurve.Figures.Add(oPathFigure);
            WpfGraphicsUtil.FreezeIfFreezable(oQuadraticBezierCurve);

            return(oQuadraticBezierCurve);
        }
        GetPathGeometryFromPoints
        (
            System.Windows.Point startPoint,
            params Point [] otherPoints
        )
        {
            Debug.Assert(otherPoints != null);

            Int32 iOtherPoints = otherPoints.Length;

            Debug.Assert(iOtherPoints > 0);

            PathFigure oPathFigure = new PathFigure();

            oPathFigure.StartPoint = startPoint;

            PathSegmentCollection oPathSegmentCollection =
                new PathSegmentCollection(iOtherPoints);

            for (Int32 i = 0; i < iOtherPoints; i++)
            {
                oPathSegmentCollection.Add(
                    new LineSegment(otherPoints[i], true));
            }

            oPathFigure.Segments = oPathSegmentCollection;
            oPathFigure.IsClosed = true;
            WpfGraphicsUtil.FreezeIfFreezable(oPathFigure);

            PathGeometry oPathGeometry = new PathGeometry();

            oPathGeometry.Figures.Add(oPathFigure);
            WpfGraphicsUtil.FreezeIfFreezable(oPathGeometry);

            return(oPathGeometry);
        }
示例#8
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);
        }