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

て烟熏妆下的殇ゞ 提交于 2019-12-06 09:23:44
Ben Walker

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);
                }

            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!