Beispiel #1
0
        public static BitmapSource GetBitmapSource(Stream Source, float overAllSize = 256.0f)
        {
            Svg.SvgDocument doc = Svg.SvgDocument.Open <Svg.SvgDocument>(Source);

            float w = doc.GetDimensions().Width;
            float h = doc.GetDimensions().Height;

            float ratioX = overAllSize / w;
            float ratioY = overAllSize / h;

            float ratio = ratioX < ratioY ? ratioX : ratioY;

            int newHeight = Convert.ToInt32(h * ratio);
            int newWidth  = Convert.ToInt32(w * ratio);

            if (doc.Width.Type != Svg.SvgUnitType.Percentage && doc.Height.Type != Svg.SvgUnitType.Percentage)
            {
                doc.Transforms.Add(new Svg.Transforms.SvgScale(ratio));
            }

            Bitmap HBITMAP = doc.Draw(newWidth, newHeight);

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(HBITMAP.GetHbitmap(), IntPtr.Zero,
                                                                                           System.Windows.Int32Rect.Empty,
                                                                                           BitmapSizeOptions.FromEmptyOptions());

            return(bs);
        }
        internal System.Drawing.Image GetGdiImage(IconCollectionInfo collectionInfo, IconFileInfo fileInfo)
        {
            string iconFileKey = GetIconFileKey(collectionInfo, fileInfo);

            System.Drawing.Image result = null;
            if (!m_gdiImages.TryGetValue(iconFileKey, out result))
            {
                // Try to load the icon from svg file
                var svgIconLink = TryFindSvgIcon(collectionInfo, fileInfo);
                if (svgIconLink != null)
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    using (Stream inStream = svgIconLink.OpenRead())
                    {
                        Svg.SvgDocument       svgDoc       = Svg.SvgDocument.Open <Svg.SvgDocument>(inStream);
                        System.Drawing.Bitmap targetBitmap = new System.Drawing.Bitmap(collectionInfo.IconSideWidthPixel, collectionInfo.IconSideWidthPixel);
                        using (CustomSvgRenderer svgRenderer = new CustomSvgRenderer(targetBitmap, Color.FromArgb(collectionInfo.IconForeColor)))
                        {
                            svgDoc.Overflow = Svg.SvgOverflow.Auto;

                            svgRenderer.SetBoundable(new GenericBoundable(0f, 0f, (float)targetBitmap.Width, (float)targetBitmap.Height));

                            SizeF dimensions = svgDoc.GetDimensions();
                            svgRenderer.ScaleTransform((float)targetBitmap.Width / dimensions.Width, (float)targetBitmap.Height / dimensions.Height, MatrixOrder.Append);

                            svgDoc.Draw(svgRenderer);
                        }
                        result = targetBitmap;
                    }

                    sw.Stop();
                }

                // Try to load the icon from png file
                if (result == null)
                {
                    var pngIconLink = TryFindPngIcon(collectionInfo, fileInfo);
                    if (pngIconLink != null)
                    {
                        using (Stream inStream = pngIconLink.OpenRead())
                        {
                            result = System.Drawing.Bitmap.FromStream(inStream);
                        }
                    }
                }

                m_gdiImages[iconFileKey] = result;
            }

            return(result);
        }