如何在asp.net中显示多个图像的tiff文件?

时间:2014-04-29 10:10:57

标签: c# asp.net .net tiff

我正在使用OCR项目,其中用户提交要转换的pdf或tiff文件,转换后的文本和原始文件将并排显示以进行比较和编辑。除了在控件中显示原始tiff之外,我已经完成了所有部分。

无论如何,我从tiff中提取了图像并添加到图像集中。我被困在显示部分。控件应按顺序显示图像,以便用户可以与处理过的文本进行比较。

这是我用于提取单个图像的方法:

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

我做了一些研究并遇到了几个观众(dll),但我想知道有没有简单的方法来使用.Net中的现有核心控件。

我感谢任何帮助或建议,向我展示继续前进的道路。

修改 为了清楚这个问题,上面的方法运行正常。 我的问题是如何在.Net控件中显示订单中的那些?

2 个答案:

答案 0 :(得分:1)

要创建TIFF图像,您可以使用TiffBitmapDecoder

步骤如下:

  • 通过TIFF文件
  • 的流创建TiffBitmapDecoder对象
  • 从解码器帧中获取单个图像

下面提供了一个示例,它将从TIFF中提取单个图像并在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];
}

在用户界面上,您可以放置​​一个带有两个按钮的pictureBox,以便在TIFF图像中前后移动

                +----------------------------+
                |                            |
                |                            |
                |                            |
                |     pictureBox             |
                |             control        |
                |                            |
                |                            |
                |                            |
                |                            |
                |                            |
                +----------------------------+
                       [ < ]    [ > ]

编辑:根据OP的要求,所有TIFF图像都以可滚动格式显示

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

有关详情,请参阅MSDN

答案 1 :(得分:0)

显示您只需要图片框的图片

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

之后,您将i ++或i--放在按钮中的按钮