示例#1
0
        private Image GetImageFromPage(PdfImageViewer viewer, int pageIndex)
        {
            string[]  names  = viewer.GetImageNames(pageIndex);
            ArrayList images = new ArrayList();

            for (int i = 0; i < names.Length; i++)
            {
                try
                {
                    images.Add(viewer.GetImage(pageIndex, names[i]));
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("Image '{0}': {1}", names[i], e.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (images.Count == 0)
            {
                return(null);
            }
            if (images.Count == 1)
            {
                return((Image)images[0]);
            }

            // merge images
            int padding = 5;
            int heigth  = 0;
            int width   = 0;
            int n       = images.Count;

            for (int i = 0; i < n; i++)
            {
                Image current = (Image)images[i];
                if (width < current.Width)
                {
                    width = current.Width;
                }
                heigth += current.Height;
            }
            width  += 3;
            heigth += (n + 1) * padding;
            Bitmap   result = new Bitmap(width, heigth, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g      = Graphics.FromImage(result);

            g.FillRectangle(Brushes.White, new Rectangle(0, 0, width, heigth));
            int dy = 1;

            for (int i = 0; i < n; i++)
            {
                Image current = (Image)images[i];
                g.DrawImageUnscaled(current, new Point(1, dy));
                dy += current.Height + padding;
                current.Dispose();
            }
            g.Dispose();

            return(result);
        }
        /// <summary>
        /// Returns a page images as single image.
        /// </summary>
        private static BitmapSource GetPdfPageImage(PdfImageViewer viewer, int pageIndex)
        {
            string[] imageNames = viewer.GetImageNames(pageIndex);

            // collect page images
            List <BitmapSource> images = new List <BitmapSource>();

            for (int i = 0; i < imageNames.Length; i++)
            {
                try
                {
                    images.Add(viewer.GetImage(pageIndex, imageNames[i]));
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("Image '{0}': {1}", imageNames[i], e.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            if (images.Count == 0)
            {
                return(null);
            }
            if (images.Count == 1)
            {
                return(images[0]);
            }

            // draw images on page
            int padding = 5;
            int height  = 0;
            int width   = 0;
            int n       = images.Count;

            for (int i = 0; i < n; i++)
            {
                BitmapSource current = images[i];
                if (width < current.PixelWidth)
                {
                    width = current.PixelWidth;
                }
                height += current.PixelHeight;
            }
            width  += 3;
            height += (n + 1) * padding;
            DrawingVisual  drawingVisual  = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, width, height));
            height = 0;
            for (int i = 0; i < images.Count; i++)
            {
                BitmapSource bitmap = images[i];
                if (bitmap.PixelWidth > width)
                {
                    width = bitmap.PixelWidth;
                }
                drawingContext.DrawImage(bitmap, new Rect(0, height, bitmap.PixelWidth, bitmap.PixelHeight));
                height += bitmap.PixelHeight + padding;
            }
            drawingContext.Close();

            // returns combined images
            RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

            bmp.Render(drawingVisual);
            return(bmp);
        }