Example #1
0
        public object GetRealObject(StreamingContext context)
        {
            if (fileName == null && rawData == null)
            {
                return(null);
            }

            if (fileName != null)
            {
                if (asIcon)
                {
                    return(new Icon(fileName));
                }

                try
                {
                    return(Image.FromFile(fileName));
                }
                catch (Exception)
                {
                    if (!fileName.EndsWith(".ico", StringComparison.OrdinalIgnoreCase))
                    {
                        throw;
                    }

                    // special handling for icon files: as a Bitmap icons may throw an exception
                    using (var info = new ImageInfo(new Icon(fileName)))
                        return(info.GetCreateImage().Clone());
                }
            }

            MemoryStream ms = new MemoryStream(rawData);

            return(asIcon ? (object)new Icon(ms) : SerializationHelper.ReadImage(new BinaryReader(ms)));
        }
Example #2
0
        private byte[] SerializeImage(ImageInfo imageInfo)
        {
            using (var ms = new MemoryStream())
            {
                if (asIcon)
                {
                    (imageInfo.Icon ?? imageInfo.GetCreateIcon()).SaveAsIcon(ms);
                    return(ms.ToArray());
                }

                using (var bw = new BinaryWriter(ms))
                {
                    switch (imageInfo.Type)
                    {
                    case ImageInfoType.Pages:
                    case ImageInfoType.MultiRes:
                        // we must use an inner stream because image.Save (at least TIFF encoder) may overwrite
                        // the stream content before the original start position
                        using (var inner = new MemoryStream())
                        {
                            if (imageInfo.Type == ImageInfoType.Pages)
                            {
                                imageInfo.Frames.Select(f => f.Image).SaveAsMultipageTiff(inner);
                            }
                            else
                            {
                                (imageInfo.Icon ?? imageInfo.GetCreateIcon()).SaveAsIcon(inner);
                            }

                            bw.Write(true);     // AsImage
                            bw.Write((int)inner.Length);
                            inner.WriteTo(ms);
                        }

                        break;

                    case ImageInfoType.Animation:
                        SerializationHelper.WriteImage(bw, imageInfo.GetCreateImage());
                        break;

                    default:
                        SerializationHelper.WriteImage(bw, imageInfo.Image);
                        break;
                    }

                    return(ms.ToArray());
                }
            }
        }