Exemplo n.º 1
0
        private static void WriteMeta(BinaryWriter bw, ImageInfoBase imageInfo)
        {
            bw.Write(imageInfo.HorizontalRes);
            bw.Write(imageInfo.VerticalRes);
            bw.Write(imageInfo.Size.Width);
            bw.Write(imageInfo.Size.Height);
            bw.Write((int)imageInfo.PixelFormat);
            bw.Write(imageInfo.RawFormat.ToByteArray());

            bw.Write(imageInfo.Palette.Length);
            foreach (Color color in imageInfo.Palette)
            {
                bw.Write(color.ToArgb());
            }
        }
Exemplo n.º 2
0
        private static void ReadMeta(BinaryReader br, ImageInfoBase imageInfo)
        {
            imageInfo.HorizontalRes = br.ReadSingle();
            imageInfo.VerticalRes   = br.ReadSingle();
            imageInfo.Size          = new Size(br.ReadInt32(), br.ReadInt32());
            imageInfo.PixelFormat   = (PixelFormat)br.ReadInt32();
            imageInfo.RawFormat     = new Guid(br.ReadBytes(16));

            var palette = new Color[br.ReadInt32()];

            imageInfo.Palette = palette;
            for (int i = 0; i < palette.Length; i++)
            {
                palette[i] = Color.FromArgb(br.ReadInt32());
            }
        }
Exemplo n.º 3
0
        public static Drawing CreateDrawing(
            WordprocessingDocument package,
            Stream imageStream,
            ImagePartType imagePartType,
            int percentage, long maxWidthInEmus)
        {
            var imagePart = package.MainDocumentPart.AddImagePart(imagePartType);

#if NET35 || NET45
            var img = new BitmapImage();

            using (imageStream)
            {
                img.BeginInit();
                img.StreamSource = imageStream;
                img.EndInit();

                imageStream.Seek(0, SeekOrigin.Begin);
                imagePart.FeedData(imageStream);
                // imagePart will also dispose the stream.
            }

            var widthPx    = img.PixelWidth;
            var heightPx   = img.PixelHeight;
            var horzRezDpi = (int)img.DpiX;
            var vertRezDpi = (int)img.DpiY;
#else
            ImageInfoBase imageInfo = null;

            using (imageStream)
            {
                var type = GetImageInfoType(imagePartType);
                imageInfo = ImageInfo.GetInfo(type, imageStream);
                if (imageInfo == null)
                {
                    throw new ArgumentException("Unsupported image format.");
                }

                imageStream.Seek(0, SeekOrigin.Begin);
                imagePart.FeedData(imageStream);
                // imagePart will also dispose the stream.
            }

            var widthPx    = imageInfo.Width;
            var heightPx   = imageInfo.Height;
            var horzRezDpi = imageInfo.DpiH;
            var vertRezDpi = imageInfo.DpiV;
#endif
            const int emusPerInch = 914400;
            var       widthEmus   = (long)widthPx * emusPerInch / horzRezDpi;
            var       heightEmus  = (long)heightPx * emusPerInch / vertRezDpi;

            if (widthEmus > maxWidthInEmus)
            {
                var ratio = heightEmus * 1.0m / widthEmus;
                widthEmus  = maxWidthInEmus;
                heightEmus = (long)(widthEmus * ratio);
            }

            if (percentage != 100)
            {
                widthEmus  = widthEmus * percentage / 100;
                heightEmus = heightEmus * percentage / 100;
            }

            var drawing = GetDrawing(package.MainDocumentPart.GetIdOfPart(imagePart), widthEmus, heightEmus);
            return(drawing);
        }