65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 多页 TIFF 查看器,带缩略图

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (15投票s)

2010年3月9日

CPOL

3分钟阅读

viewsIcon

128616

downloadIcon

6109

ASP.NET 多页 TIFF 查看器,带缩略图

ASPNET_TIFFViewer

引言

市面上有很多第三方 TIF 查看器,但为什么要花钱呢?使用 System.Drawing.Image 在 .NET 中已经内置了该功能(除非您需要支持 JPEG 压缩的 TIF 文件,而 GDI 不支持)。 节省时间和金钱,从这里开始吧。

背景

现在是时候回馈社区了。 我在开始构建应用程序之前,会搜索很多地方(为什么还要重新发明轮子),但没有找到完全符合我要求的。 我希望为某人节省一些时间和金钱。 本文为您提供了使用 ASP.NET 创建多页 TIF 查看器的基础知识和代码示例。 网页将在一侧显示缩略图,另一侧显示放大图像。 单击每个页面将使用一些 JavaScript 更新大图像的 src

Using the Code

该概念由 3 个主要部分组成

  1. 一个 ASP 页面,用于调用获取文件路径参数并完成配置缩略图和大图像的工作(下载中的 Default.aspx
  2. 一个 TIF 类,用于从 TIF 文件中提取页面
  3. 一个 ASP 页面,用于显示图像(下载中的 ViewImg.aspx

Default.aspx

此页面只有一个必需参数,即查询字符串中的 FILEFILE 是文件的绝对路径。 我还设置了一个分页机制,以便大型文件加载更快,并且占用更少的系统资源进行迭代。 还有一个可选的 STARTPAGE 可以传递到查询字符串,它指示从哪一页开始(用于分页)。 此分页器大小可以在代码隐藏中配置。 代码隐藏还设置了 JavaScript,以更改大图像的 src 属性,以及大图像的任何 TIF 旋转或缩放所需的属性。 aspx 页面包含一个占位符,该占位符将 asp:image 对象添加到缩略图的占位符(querystring 参数将传递给缩略图大小的 src)。 该图像的 src 指向另一个页面(稍后解释)。 同样的概念也适用于 BIG 图像。

//Determine Start/End Pages
int StartPg = 1;
if (Request.QueryString["StartPage"] != null)
    StartPg = System.Convert.ToInt16(Request.QueryString["StartPage"]);
int BigImgPg = StartPg;
int EndPg = StartPg + (PagerSize - 1);
if (EndPg > TotalTIFPgs)
    EndPg = TotalTIFPgs;
//Add/configure the thumbnails
while (StartPg <= EndPg)
{
    Label lbl = new Label();
    //Add break for new row of thumbnails
    if (StartPg % 4 == 0 && StartPg != 0) lbl.Text = "&nbsp;<br />";
    else lbl.Text = "&nbsp;";
    //Add new image and set source and click and mouseover events
    Image Img = new Image(); 
    Img.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), "Solid");
    Img.BorderWidth = Unit.Parse("1");
    Img.Attributes.Add("onClick", "ChangePg(" + StartPg.ToString() + ");");
    Img.Attributes.Add("onmouseover", "this.style.cursor='hand';");
    Img.ImageUrl = "ViewImg.aspx?FilePath=" + FilePath + "&Pg=" + (StartPg).ToString() + 
        "&Height=" + DefaultThumbHieght.ToString() + "&Width=" + DefaultThumbWidth;
    _plcImgsThumbs.Controls.Add(Img);
    _plcImgsThumbs.Controls.Add(lbl);
        StartPg++;
}

//Bind big img
Image BigImg = new Image();
BigImg.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), "Solid");
BigImg.BorderWidth = Unit.Parse("1");
BigImg.ID = "_imgBig";
BigImg.ImageUrl = "ViewImg.aspx?View=1&FilePath=" + FilePath + "&Pg=" + 
    BigImgPg.ToString() + "&Height=" + DefaultBigHieght.ToString() + "&Width=" +
    DefaultBigWidth.ToString();
_plcBigImg.Controls.Add(BigImg);

ViewImg.aspx

此页面接受几个参数(所有这些参数都由 default.aspx 提供和配置):FILEPATH(从 default.aspx 传递),HEIGHT(图像高度),WIDTH(要显示的图像宽度),PG(要显示的页面)和 ROTATE(在显示图像之前应用于图像的旋转)。 该页面仅使用 TIF 类获取图像对象,并将其保存到 JPG 格式的 outputstream 中,以便可以在浏览器中查看它。

protected void Page_Load(object sender, EventArgs e)
{
    System.Drawing.Image TheImg = new App.Files.TIF(
        Request.QueryString["FilePath"]).GetTiffImageThumb(
        System.Convert.ToInt16(Request.QueryString["Pg"]),
        System.Convert.ToInt16(Request.QueryString["Height"]),
        System.Convert.ToInt16(Request.QueryString["Width"]));
    if (TheImg != null)
    {
        switch (Request.QueryString["Rotate"])
        {
            case "90":
                TheImg.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                break;
            case "180":
                TheImg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                break;
            case "270":
                TheImg.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                break;
        }

        Response.ContentType = "image/jpeg";
        TheImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        TheImg.Dispose();
    }
}

TIF.cs

此类用于访问 TIF 文件的操作。 对于此项目,这里的主要方法是 GetTIFFImageThumb,您可以在其中提供页码(FrameDimension 中的活动帧)和返回的缩略图的大小,同时考虑页面的宽高比。 它打开文件并将相关页面加载到 memorystream 对象中,并从该流返回一个 system.drawing.image 对象,该对象以后可以使用(例如在 ViewImg.aspx 页面中)。 我在下载中还提供了一些其他方法和对象,这些方法和对象可能对其他人(以及我的大型项目)很有用。 对于您的文件和 system.drawing.image 对象,唯一的警告是,务必在完成后正确释放该对象; 否则,当您尝试稍后使用它时,您可能会遇到文件使用错误。

public Image GetTiffImageThumb(int PageNum, int ImgWidth, int ImgHeight)
{
if ((PageNum < 1) | (PageNum > this.PageCount))
{
     throw new InvalidOperationException("Page to be retrieved is outside the bounds" +
         "of the total TIF file pages. Please choose a page number that exists.");
}
MemoryStream ms = null;
Image SrcImg = null;
Image returnImage = null;
try
{
    SrcImg = Image.FromFile(this.m_FilePathName);
    ms = new MemoryStream();
    FrameDimension FrDim = new FrameDimension(SrcImg.FrameDimensionsList[0]);
    SrcImg.SelectActiveFrame(FrDim, PageNum-1);
    SrcImg.Save(ms, ImageFormat.Tiff);
    // Prevent using images internal thumbnail
    SrcImg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    SrcImg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    //Save Aspect Ratio
    if (SrcImg.Width <= ImgWidth) ImgWidth = SrcImg.Width;
    int NewHeight = SrcImg.Height * ImgWidth / SrcImg.Width;
    if (NewHeight > ImgHeight)
    {
        // Resize with height instead
        ImgWidth = SrcImg.Width * ImgHeight / SrcImg.Height;
        NewHeight = ImgHeight;
    }
//Return Image
returnImage = Image.FromStream(ms).GetThumbnailImage(ImgWidth, NewHeight, null,
    IntPtr.Zero);
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    SrcImg.Dispose();
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
return returnImage;
}

历史

  • 2010 年 3 月 9 日:初步发布
© . All rights reserved.