To create a TIFF image, you can use TiffBitmapDecoder
The steps are:
- Create TiffBitmapDecoder object over the stream from TIFF file
- Get individual images from decoder frames
An example is provided below which will extract individual images from TIFF and show them on the pictureBox
List<Image> allTiffImages = null;
int currentImageIndex = 0;
private void btnLoadTiff_Click(object sender, EventArgs e)
{
images = new List<Image>();
// Open a Stream and decode a TIFF image
Stream imageStreamSource = new FileStream("filename.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
foreach(BitmapSource bmpS in decoder.Frames)
{
Image img = new Image();
img.Source = bmpS;
img.Stretch = Stretch.None;
img.Margin = new Thickness(10);
images.Add(img);
}
if(images.Count > 0)
pictureBox1.Image = images[0];
}
private void btnNextImage_Click(object sender, EventArgs e)
{
if(++currentImageIndex >= images.Count)
currentImageIndex = 0;
// 0 cycles the images,
// if you want to stop at last image,
// set currentImageIndex = images.Count - 1;
pictureBox1.Image = images[currentImageIndex];
}
private void btnPrevImage_Click(object sender, EventArgs e)
{
if(--currentImageIndex < 0)
currentImageIndex = images.Count - 1;
// images.Count - 1 cycles the images,
// if you want to stop at first image,
// set currentImageIndex = 0;
pictureBox1.Image = images[currentImageIndex];
}
On the UI, you can place a pictureBox with two buttons to move forward and backward in the TIFF images as
+----------------------------+
| |
| |
| |
| pictureBox |
| control |
| |
| |
| |
| |
| |
+----------------------------+
[ < ] [ > ]
Edit: as per OP's requirement, all images of TIFF to be shown in scrollable format
private void btnLoadTiff_Click(object sender, EventArgs e)
{
List<Image> images = ..... // this collection contains all the images in TIFF
// find the total width and height of all images in TIFF (this is because I will be showing images side by side
int maxWidth = 0;
int maxHeight = 0;
foreach(Image img in images)
{
maxWidth += img.Width;
if(maxHeight < img.Height)
maxHeight = img.Height;
}
// if any image has height less then the other image, there will be blank spaces.
// create new bitmap of the maxWidth and maxHeight (this bmp will have all the images drawn on itself
Bitmap bmp = new Bitmap(maxWidth, maxHeight);
Graphics g = Graphics.FromImage(bmp);
// stores the x location where next image should be drawn
int x = 0;
foreach(Image img in images)
{
Rectangle rectSrc = new Rectange(0, 0, img.Width, img.Height);
Rectangle rectDest = new Rectangle(x, 0, img.Width, img.Height);
g.DrawImage(bmp, rectDest, rectSrc, GraphicsUnit.Pixel);
x += img.Width;
}
// show the image in picturebox. The picturebox can have different stretch settings, or may be contained inside a panel with scrolling set.
pictureBox1.Image = bmp;
}
For more see MSDN