[英]How to display multiple image tiff file in asp.net?
我正在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控件中按順序顯示這些內容?
要創建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];
}
在UI上,您可以放置一個帶兩個按鈕的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。
顯示圖像,您只需要一個圖片框
coleccion = GetAllPages(string file);
yourpicturebox.image = coleccion[i];
之后,對於按鈕,您將i ++或i--放入某些按鈕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.