Pack URI to image embedded in a resx file

前端 未结 4 1348
情深已故
情深已故 2020-12-10 02:27

How do I construct a pack URI to an image that is in a resource file?

I have an assembly called MyAssembly.Resources.dll, it has a folder called Ima

4条回答
  •  不知归路
    2020-12-10 02:47

    As Walt has correctly stated, what you get out of a resx file is a System.Drawing.Bitmap. So this needs to be converted to a System.Windows.Media.ImageSource or subtype.

    I'm not sure if this falls under time wasters for you because it does not employ an URI, but here is how I get images from resx files in another library. I use a simple proxy because the resx designer file only exposes an internal constructor (even if the class is public), then define a ValueConverter that will provide the ImageSource.

    enter image description here

    
    
        
            
            
        
    
        
    
    

    AssetsProxy:

    namespace MyAssembly.Resources
    {
        public class AssetsProxy : Images.Assets
        {
            public AssetsProxy() : base() { }
        }
    }
    

    Bitmap to ImageSource conversion:

    using System;
    using System.Drawing.Imaging;
    using System.Globalization;
    using System.IO;
    using System.Windows.Data;
    using System.Windows.Media.Imaging;
    
    namespace MyAssembly.Resources
    {
        /// 
        /// Converts a BitmapImage, as provided by a resx resource, into an ImageSource/BitmapImage
        /// 
        public class BitmapToImageSourceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                BitmapImage bitmapImage = null;
                if (value is System.Drawing.Image)
                {
                    bitmapImage = ((System.Drawing.Image)value).ToBitmapImage();
                }
                return bitmapImage;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
        public static class BitmapExtensions
        {
            /// 
            /// Converts the System.Drawing.Image to a System.Windows.Media.Imaging.BitmapImage
            /// 
            public static BitmapImage ToBitmapImage(this System.Drawing.Image bitmap)
            {
                BitmapImage bitmapImage = null;
                if (bitmap != null)
                {
                    using (MemoryStream memory = new MemoryStream())
                    {
                        bitmapImage = new BitmapImage();
                        bitmap.Save(memory, ImageFormat.Png);
                        memory.Position = 0;
                        bitmapImage.BeginInit();
                        bitmapImage.StreamSource = memory;
                        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                        bitmapImage.EndInit();
                    }
                }
                return bitmapImage;
            }
        }
    }
    

提交回复
热议问题