尝试修改现有的 ASPX C# 以允许缩放 TIFF 图像

Attempting to modify existing ASPX C# to allow zoom on TIFF image

我有一个页面显示存储为 Tiff 图像的文档。有时显示的图像不够清晰,用户无法确定他们拥有正确的文档。他们需要缩放功能。

原始代码如下所示:

public void GetPage(int page)
    {
        var c = Request.Cookies["TiffViewer"];
        string key = c["Key"];
        if (Cache[key] != null)
        {
            using (MemoryStream ms = new MemoryStream((byte[])Cache[key]))
            {
                using (Bitmap img = new Bitmap(ms))
                {
                    img.SelectActiveFrame(FrameDimension.Page, page);
                    using (Bitmap b = new Bitmap(img, new Size(img.Width / 2, img.Height / 2)))
                    {
                        using (Graphics gr = Graphics.FromImage(b))
                        {
                            var gray_matrix = new float[][] { 
                                new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, 
                                new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, 
                                new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, 
                                new float[] { 0,      0,      0,      1, 0 }, 
                                new float[] { 0,      0,      0,      0, 1 }};

                            using (var ia = new ImageAttributes())
                            {
                                ia.SetColorMatrix(new ColorMatrix(gray_matrix));
                                ia.SetThreshold(0.8f, ColorAdjustType.Default);
                                gr.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ia);
                            }
                        }
                        b.Save(Response.OutputStream, ImageFormat.Jpeg);
                    }
                }
            }
        }
    }

我尝试了多种不同的位图和绘制图像操作。我最接近获得 'zoom' 的方法是根据这篇文章替换 gr.DrawImage:

替换后的 gr.DrawImage 语句如下所示:

 gr.DrawImage(b, new Rectangle(0, 0, b.Width*2, b.Height*2), new Rectangle(0, 0, b.Width, b.Height), GraphicsUnit.Pixel);

这会导致文本图像变大,但现在只有部分图像可见。图像显示区域保持静止。我要么需要使 'display area' 变大,要么以某种方式允许在显示的图像上滚动。欢迎提出建议。

我想出了一个不完美的解决方案,但允许用户获得他们试图查看的文档的更多详细信息。

aspx 页面添加了两个控件:

<div class="Controls">
    <asp:Label ID="info" runat="server" Text=""></asp:Label>
    <asp:Button ID="Return" runat="server" Text="Return" onclick="Return_Click" />
    <asp:Button ID="Download" runat="server" Text="Download" onclick="Download_Click" />
    <asp:Button ID="Stamp" runat="server" Text="Stamp" onclick="Stamp_Click" />
    <asp:Button ID="Print" runat="server" Text="Print" onclick="Print_Click" />
    <asp:Button ID="Zoom_In" runat="server" Text="-" onclick="ZoomIn_Click" />
    <asp:Button ID="Zoom_Out" runat="server" Text="+" onclick="ZoomOut_Click" />
</div>

同样在 aspx 页面上,我修改了呈现 tiff 文件的脚本以添加大小更改百分比:

<script type="text/javascript">
    var html;
    var cookie = c("TiffViewer");
    var percent = '<%= Request.QueryString["ChangeSize"]%>';
    var ImHeight = cookie["ImHeight"];
    var ImWidth = cookie["ImWidth"];
    for (var i = 1; i < +cookie["PageCount"] + 1; i++) {
        $("#Pages").append("<a href='#page/" + (i - 1) + "'>" + i + "</a><br />");
        if (ImHeight == 0 || ImWidth == 0) {
            html = "<div class='slide' id='slide' overflow='scroll'><br /><b>Page {0}</b><br /><img id='p{1}' {2} /></div>";
            $("#frames").append(String.format(html, i, i - 1, (i == (1) || i == (+cookie["PageCount"]) ? "src='Viewer.aspx?p=" + (i - 1) + "&" + "ChangeSize=" + percent + "'" : "")));
        }
        else {
            var adjHeight = ImHeight / 2;
            var adjWidth = ImWidth / 2;
            html = "<div class='slide' id='slide' height='" + adjHeight.toString() + "'><br /><b>Page {0}</b><br /><img height='" + adjHeight.toString() + "' width='" + adjWidth.toString() + "' id='p{1}' {2} /></div>";
            $("#frames").append(String.format(html, i, i - 1, (i == (1) || i == (+cookie["PageCount"]) ? "src='Viewer.aspx?p=" + (i - 1) + "&" + "ChangeSize=" + percent +  "'" : "")));
        }
    }
</script>

在代码隐藏页面上,我将值存储在代码 "using (var ia = new ImageAttributes())" 部分的 cookie 中:

    using (var ia = new ImageAttributes())
{
    ia.SetColorMatrix(new ColorMatrix(gray_matrix));
    ia.SetThreshold(0.8f, ColorAdjustType.Default);
    //gr.DrawImage(b, new Rectangle(0, 0, (int)(b.Width * (1 + (percent / 100))), (int)(b.Height * (1 + (percent / 100)))), 0, 0, (int)(b.Width * (1 + (percent / 100))), (int)(b.Height * (1 + (percent / 100))), GraphicsUnit.Pixel, ia);
    gr.DrawImage(b, new Rectangle(0, 0, (int)(b.Width * (1 + (percent / 100))), (int)(b.Height * (1 + (percent / 100)))), new Rectangle(0, 0, (int)(b.Width * (1 + (percent / 100))), (int)(b.Height * (1 + (percent / 100)))), GraphicsUnit.Pixel);
    //gr.DrawImage(b, new Rectangle(0, 0, (int)(b.Width * (1 + (percent / 100))), (int)(b.Height * (1 + (percent / 100)))), new Rectangle(0, 0, b.Width, b.Height), GraphicsUnit.Pixel);
    string myHeight;
    string myWidth;
    ImHeight = (int)(b.Height * (1 + (percent / 100)));
    ImWidth = (int)(b.Width * (1 + (percent / 100)));
    myHeight = ImHeight.ToString(); ;
    myWidth = ImWidth.ToString(); ;
    Response.Cookies["TiffViewer"]["ReturnURL"] = c["ReturnURL"];
    Response.Cookies["TiffViewer"]["Key"] = c["Key"];
    Response.Cookies["TiffViewer"]["ImHeight"] = myHeight;
    Response.Cookies["TiffViewer"]["ImWidth"] = myWidth;
    Response.Cookies["TiffViewer"]["PageCount"] = c["PageCount"];

}

我为这两个新控件添加了以下点击程序:

    protected void ZoomIn_Click(object sender, EventArgs e)
{
    var c = Request.Cookies["TiffViewer"];
    string key = c["Key"];
    if (Cache[key] != null)
    {
        float chg = -10.0f;
        float percent = 0.0f;
        float.TryParse(Request.QueryString["ChangeSize"], out percent);
        percent += chg;
        int ImHeight = 0;
        int.TryParse(c["ImHeight"], out ImHeight);
        int ImWidth = 0;
        int.TryParse(c["ImWidth"], out ImWidth);
        string myHeight = "0";
        string myWidth = "0";
        if (ImHeight > 0 && ImWidth > 0)
        {
            ImHeight = (int)(ImHeight * (1 + (percent / 100)));
            myHeight = ImHeight.ToString();
            ImWidth = (int)(ImWidth * (1 + (percent / 100)));
            myWidth = ImWidth.ToString();
        }
        Response.Cookies["TiffViewer"]["ReturnURL"] = c["ReturnURL"];
        Response.Cookies["TiffViewer"]["Key"] = c["Key"];
        Response.Cookies["TiffViewer"]["ImHeight"] = myHeight;
        Response.Cookies["TiffViewer"]["ImWidth"] = myWidth;
        Response.Cookies["TiffViewer"]["PageCount"] = c["PageCount"];
        Response.Redirect("~/Viewer/Viewer.aspx?ChangeSize=" + percent.ToString());
    }
}

protected void ZoomOut_Click(object sender, EventArgs e)
{
    var c = Request.Cookies["TiffViewer"];
    string key = c["Key"];
    if (Cache[key] != null)
    {
        float chg = 10.0f;
        float percent = 0.0f;
        float.TryParse(Request.QueryString["ChangeSize"], out percent);
        percent += chg;
        int ImHeight = 0;
        int.TryParse(c["ImHeight"], out ImHeight);
        int ImWidth = 0;
        int.TryParse(c["ImWidth"], out ImWidth);
        string myHeight = "0";
        string myWidth = "0";
        if (ImHeight > 0 && ImWidth > 0)
        {
            ImHeight = (int)(ImHeight * (1 + (percent / 100)));
            myHeight = ImHeight.ToString();
            ImWidth = (int)(ImWidth * (1 + (percent / 100)));
            myWidth = ImWidth.ToString();
        }
        Response.Cookies["TiffViewer"]["ReturnURL"] = c["ReturnURL"];
        Response.Cookies["TiffViewer"]["Key"] = c["Key"];
        Response.Cookies["TiffViewer"]["ImHeight"] = myHeight;
        Response.Cookies["TiffViewer"]["ImWidth"] = myWidth;
        Response.Cookies["TiffViewer"]["PageCount"] = c["PageCount"];
        Response.Redirect("~/Viewer/Viewer.aspx?ChangeSize=" + percent.ToString());
    }
}

为什么这段代码不完美?部分原因是此代码的其他部分旨在将 tiff 渲染为 window 的高度。所以 tiff 永远不会变得更高,只有更宽的代码。因此,尽管这并不优雅,但它确实让用户 "zoom" 可以更好地查看文档的详细信息。