New to Telerik UI for WinForms? Download free 30-day trial

How to Edit a Multi-page TIFF Image

Environment

Product Version Product Author
2022.1.222 RadImageEditor for WinForms Desislava Yordanova

Description

RadImageEditor supports Tiff images. However, support for multi-page Tiff images is not available at the moment. The following tutorial demonstrates a sample approach how to achieve it.

edit-multi-page-tiff-image 001

Solution

This solution creates a list of images out of the frames of the Tiff image and works with them. Every time you want to navigate to the next frame, you can save and update the initial image and then load a new bitmap with the correct frame and the updated image.


    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        Image tiffImage;
        Bitmap editedImage;
        int currentPage = 0;
        List<Image> images;
        string path = @"..\..\multipage_tif_example.tif";

        public RadForm1()
        {
            InitializeComponent();
            this.tiffImage = Image.FromFile(@"..\..\multipage_tif_example.tif");
            this.images = this.GetAllPages(tiffImage);

            this.tiffImage.SelectActiveFrame(FrameDimension.Page, this.currentPage);
            this.editedImage = new Bitmap(this.tiffImage);
            this.radImageEditor1.OpenImage(this.editedImage);
            this.radLabel1.Text = Convert.ToString(this.currentPage + 1);
        }

        private List<Image> GetAllPages(Image multiPageImage)
        {
            List<Image> images = new List<Image>();
            int count = multiPageImage.GetFrameCount(FrameDimension.Page);
            for (int i = 0; i < count; i++)
            {
                multiPageImage.SelectActiveFrame(FrameDimension.Page, i);
                MemoryStream byteStream = new MemoryStream();
                multiPageImage.Save(byteStream, ImageFormat.Tiff);
                images.Add(Image.FromStream(byteStream));
                byteStream.Dispose();
            }
            return images;
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            if (this.currentPage > 0)
            {
                MemoryStream byteStream = new MemoryStream();
                this.radImageEditor1.SaveImage(byteStream, ImageFormat.Tiff);
                this.images[this.currentPage] = Image.FromStream(byteStream);
                byteStream.Dispose();

                this.currentPage--;
                this.UpdateImage(this.currentPage);
            }
        }

        private void UpdateImage(int page)
        {
            this.editedImage = new Bitmap(this.images[page]);
            this.radImageEditor1.OpenImage(this.editedImage);
            this.radLabel1.Text = Convert.ToString(page + 1);

            this.radImageEditor1.Invalidate();
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            if (this.currentPage < this.images.Count - 1)
            {
                MemoryStream byteStream = new MemoryStream();
                this.radImageEditor1.SaveImage(byteStream, ImageFormat.Tiff);
                this.images[this.currentPage] = Image.FromStream(byteStream);
                byteStream.Dispose();

                this.currentPage++;
                this.UpdateImage(this.currentPage);
            }
        }
    }

A complete C# and VB.NET projects are available here.