【问题标题】:How to display multiple image tiff file in asp.net?如何在 asp.net 中显示多个图像 tiff 文件?
【发布时间】:2014-04-29 10:10:57
【问题描述】:

我正在处理一个 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 控件中按顺序显示它们?

【问题讨论】:

  • 检查 Windows Image 组件的 TIFFBitmapDecoder 类。它可能会有所帮助
  • 您想在.aspx page 的位置显示图像吗?如果是,您需要创建一个http handler,它将返回返回图像。
  • 是的。在控件内部.. 不是
    .. 我必须并排放置两个控件。一种是显示原始图像,另一种是加载提取文本的编辑器。用户必须能够并排比较两者并编辑文本。因此,它必须按顺序显示图像,以便用户可以滚动浏览和比较。

标签: c# asp.net .net tiff


【解决方案1】:

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

步骤如下:

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

下面提供了一个示例,它将从 TIFF 中提取单个图像并将它们显示在图片框上

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

在 UI 上,您可以放置​​一个带有两个按钮的图片框,以便在 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

【讨论】:

  • 我已经用我提到的方法得到了单独的图像。我需要知道如何在控件中显示它们。
  • 您想显示任何一张图片或一张接一张(如动画)?
  • 没有。我知道我可以在单击按钮时加载单个图像。但这对最终用户来说是额外的努力。编辑器加载可滚动的整个文本,因此我需要控件还必须能够加载用户可以滚动浏览的所有图像。所以它看起来是一样的。
【解决方案2】:

要显示图像,您只需要一个图片框

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

在那之后你把 i++ 或 i-- 放在一些按钮中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多