Question

I'm working with an OCR project where the user submits pdf or tiff file to be converted and converted text and original file will be displayed side by side to compare and edit. I've done every part except displaying the original tiff in a control.

Anyway, I've extracted images from the tiff and added to the image collection. I'm stuck with the displaying part. The control should be display the images in the order so that the user can compare with the processed text.

This is the method I used for extracting individual images:

 public static Collection<Image> GetAllPages(string file)
    {
        Collection<Image> images = new Collection<Image>();
        Bitmap bitmap = (Bitmap)Image.FromFile(file);
        int count = bitmap.GetFrameCount(FrameDimension.Page);
        for (int idx = 0; idx < count; idx++)
        {

            bitmap.SelectActiveFrame(FrameDimension.Page, idx);
            MemoryStream byteStream = new MemoryStream();
            bitmap.Save(byteStream, ImageFormat.Tiff);
            images.Add(Image.FromStream(byteStream));
        }


        return images;
    }

I've done some research and came across several viewers (dlls), but I want to know is there any simple way to do it with existing core controls in .Net.

I appreciate any help or suggestions to show me a path to continue.

Edit: Just to be clear about the question, the above method works fine. My Question is how to display those in an order in a .Net control?

Was it helpful?

Solution

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

OTHER TIPS

to display the image you just need a picturebox

coleccion = GetAllPages(string file);
yourpicturebox.image = coleccion[i];

After that for the buttons you put i++ or i-- in some buttons

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top