Пример #1
0
        public bool TryConvert(object? @from, Type toType, object?conversionHint, out object?result)
        {
            result = @from;

            if (@from is string path)
            {
                var ext = Path.GetExtension(path);
                if (ext?.Equals(".svg") == true)
                {
                    result = new SvgImageExtension(path).ProvideValue(null) as ImageSource;
                }
                else
                {
                    result = new BitmapImage(new Uri(path));
                }

                return(true);
            }

            return(false);
        }
Пример #2
0
        public static ImageSource GetSVGOrBitmapImageSource(string filepath)
        {
            string localpath = FileDataProvider.EnsureLocalFilePathDownloadTempDirectory(filepath);

            if (localpath == null && !File.Exists(localpath))
            {
                return(null);
            }

            ImageSource imageSource = null;

            string ext = Path.GetExtension(localpath);

            if (string.Equals(ext, DataProviderFactory.IMAGE_SVG_EXTENSION, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(ext, DataProviderFactory.IMAGE_SVGZ_EXTENSION, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    SvgImageExtension svgImageExt = new SvgImageExtension(localpath);
                    svgImageExt.TextAsGeometry = true;
                    svgImageExt.OptimizePath   = true;
                    svgImageExt.IncludeRuntime = true;

                    imageSource = (DrawingImage)svgImageExt.ProvideValue(null);
                }
                catch (Exception e1)
                {
                    return(null);
                }
            }
            else
            {
                try
                {
                    //new BitmapImage(new Uri(path, UriKind.Absolute));
                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource     = new Uri(localpath, UriKind.Absolute);
                    bitmap.CacheOption   = BitmapCacheOption.OnLoad;
                    bitmap.CreateOptions = BitmapCreateOptions.None;
                    bitmap.EndInit();

                    imageSource = bitmap;
                }
                catch (Exception e2)
                {
                    return(null);
                }



                /*
                 * stream = new FileStream(fullImagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                 *
                 *
                 * if (fullImagePath.EndsWith(DataProviderFactory.IMAGE_JPG_EXTENSION) || fullImagePath.EndsWith(DataProviderFactory.IMAGE_JPEG_EXTENSION))
                 * {
                 *  BitmapDecoder dec = new JpegBitmapDecoder(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                 *  image.Source = dec.Frames[0];
                 * }
                 * else if (fullImagePath.EndsWith(DataProviderFactory.IMAGE_PNG_EXTENSION))
                 * {
                 *  BitmapDecoder dec = new PngBitmapDecoder(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                 *  image.Source = dec.Frames[0];
                 * }
                 * else if (fullImagePath.EndsWith(DataProviderFactory.IMAGE_BMP_EXTENSION))
                 * {
                 *  BitmapDecoder dec = new BmpBitmapDecoder(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                 *  image.Source = dec.Frames[0];
                 * }
                 * else if (fullImagePath.EndsWith(DataProviderFactory.IMAGE_GIF_EXTENSION))
                 * {
                 *  BitmapDecoder dec = new GifBitmapDecoder(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                 *  image.Source = dec.Frames[0];
                 * }
                 */
            }

            return(imageSource);
        }