Exemplo n.º 1
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());
                }
            }
        }