Remove page breaks in multi-page tif to make one l

2019-05-23 10:40发布

I have some tif files with multiple pages that I would like to convert to a single long page. i.e. a file including two pages that are each 8.5x11 would be converted to a resulting file of size 8.5x22. Is there any way to remove the page breaks?

I am not asking how to convert multiple files into a single file.

1条回答
乱世女痞
2楼-- · 2019-05-23 11:36

I have solved this. A good chunk of the following code comes from Scott Hanselman on this page.

This C# function takes a filename for the source image and a save location for its tiff output:

public static void RemovePageBreaks(string fileInput, string fileOutput)
        {
            using (Image image = Image.FromFile(fileInput))
            using (MemoryStream m = new MemoryStream())
            {
                int width = image.Width;
                int height = 0;
                int pageCount = image.GetFrameCount(FrameDimension.Page);
                height = image.Height * pageCount;
                int pasteFrom = 0;
                using (Bitmap compositeImage = new Bitmap(width, height))
                using (Graphics compositeGraphics = Graphics.FromImage(compositeImage))
                {
                    compositeGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                    for (int page = 0; page < pageCount; page++)
                    {

                        image.SelectActiveFrame(FrameDimension.Page, page);
                        image.Save(m, image.RawFormat);
                        Rectangle rect = new Rectangle(0, pasteFrom, image.Width, image.Height);
                        compositeGraphics.DrawImageUnscaledAndClipped(image, rect);
                        pasteFrom += image.Height;
                    }
                    compositeImage.Save(fileOutput, System.Drawing.Imaging.ImageFormat.Tiff);
                }

            }
        }
查看更多
登录 后发表回答