Esempio n. 1
0
        /// <summary>
        /// Reads an image list
        /// </summary>
        /// <param name="imageData">Serialized image list</param>
        /// <returns></returns>
        public static ImageListOptions ReadImageData(byte[] imageData)
        {
            var imageList = new ImageList();
            var info      = new SerializationInfo(typeof(ImageListStreamer), new FormatterConverter());

            info.AddValue("Data", imageData);
            var ctor     = typeof(ImageListStreamer).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
            var streamer = (ImageListStreamer)ctor.Invoke(new object[] { info, new StreamingContext(StreamingContextStates.All) });

            imageList.ImageStream = streamer;

            var opts = new ImageListOptions();

            opts.ColorDepth       = imageList.ColorDepth;
            opts.ImageSize        = imageList.ImageSize;
            opts.TransparentColor = imageList.TransparentColor;

            for (int i = 0; i < imageList.Images.Count; i++)
            {
                var bitmap = imageList.Images[i];
                var stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Bmp);
                opts.ImageSources.Add(ImageResourceUtilities.CreateImageSource(stream.ToArray()));
            }

            return(opts);
        }
Esempio n. 2
0
        /// <summary>
        /// Serialize an image list
        /// </summary>
        /// <param name="opts">Options</param>
        /// <returns></returns>
        public static ResourceElement Serialize(ImageListOptions opts)
        {
            var imgList = new ImageList();

            imgList.ColorDepth       = opts.ColorDepth;
            imgList.ImageSize        = opts.ImageSize;
            imgList.TransparentColor = opts.TransparentColor;

            foreach (var imageSource in opts.ImageSources)
            {
                var bitmapSource = imageSource as BitmapSource;
                if (bitmapSource == null)
                {
                    throw new InvalidOperationException("Only BitmapSources can be used");
                }
                var encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                var outStream = new MemoryStream();
                encoder.Save(outStream);
                outStream.Position = 0;
                var wfBmp = new System.Drawing.Bitmap(outStream);
                imgList.Images.Add(wfBmp);
            }

            var obj = imgList.ImageStream;

            return(new ResourceElement {
                Name = opts.Name,
                ResourceData = new BinaryResourceData(new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes), SerializationUtilities.Serialize(obj)),
            });
        }
Esempio n. 3
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="other">Other instance</param>
		public ImageListOptions(ImageListOptions other) {
			this.Name = other.Name ?? string.Empty;
			this.ColorDepth = other.ColorDepth;
			this.ImageSize = other.ImageSize;
			this.TransparentColor = other.TransparentColor;
			this.ImageSources.AddRange(other.ImageSources);
		}
Esempio n. 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="other">Other instance</param>
 public ImageListOptions(ImageListOptions other)
 {
     this.Name             = other.Name ?? string.Empty;
     this.ColorDepth       = other.ColorDepth;
     this.ImageSize        = other.ImageSize;
     this.TransparentColor = other.TransparentColor;
     this.ImageSources.AddRange(other.ImageSources);
 }
        public static ImageListOptions ReadImageData(byte[] imageData)
        {
            var imageList = new ImageList();
            var info = new SerializationInfo(typeof(ImageListStreamer), new FormatterConverter());
            info.AddValue("Data", imageData);
            var ctor = typeof(ImageListStreamer).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
            var streamer = (ImageListStreamer)ctor.Invoke(new object[] { info, new StreamingContext(StreamingContextStates.All) });
            imageList.ImageStream = streamer;

            var opts = new ImageListOptions();
            opts.ColorDepth = imageList.ColorDepth;
            opts.ImageSize = imageList.ImageSize;
            opts.TransparentColor = imageList.TransparentColor;

            for (int i = 0; i < imageList.Images.Count; i++) {
                var bitmap = imageList.Images[i];
                var stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Bmp);
                opts.ImageSources.Add(ImageResourceUtils.CreateImageSource(stream.ToArray()));
            }

            return opts;
        }
        public static ResourceElement Serialize(ImageListOptions opts)
        {
            var imgList = new ImageList();
            imgList.ColorDepth = opts.ColorDepth;
            imgList.ImageSize = opts.ImageSize;
            imgList.TransparentColor = opts.TransparentColor;

            foreach (var imageSource in opts.ImageSources) {
                var bitmapSource = imageSource as BitmapSource;
                if (bitmapSource == null)
                    throw new InvalidOperationException("Only BitmapSources can be used");
                var encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                var outStream = new MemoryStream();
                encoder.Save(outStream);
                outStream.Position = 0;
                var wfBmp = new System.Drawing.Bitmap(outStream);
                imgList.Images.Add(wfBmp);
            }

            var obj = imgList.ImageStream;
            return new ResourceElement {
                Name = opts.Name,
                ResourceData = new BinaryResourceData(new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes), SerializationUtils.Serialize(obj)),
            };
        }
 void InitializeImageData(byte[] imageData)
 {
     this.imageListOptions = SerializedImageListStreamerUtils.ReadImageData(imageData);
     this.imageData = imageData;
 }