正在尝试修改现有的ASPX C#以允许对TIFF图像进行缩放
本文关键字:TIFF 图像 缩放 修改 ASPX | 更新日期: 2023-09-27 18:26:48
我有一个页面,显示存储为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);
}
}
}
}
}
我已经尝试了许多不同的位图和drawimage操作。我最接近于获得"缩放"的方法是替换基于本文的gr.DrawImage:http://stackoverflow.com/questions/2319983/resizing-an-image-in-asp-net-without-losing-the-image-quality
替换后的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);
这导致文本图像变得更大,但现在只能看到图像的一部分。用于显示图像的区域保持静止。我要么需要使"显示区域"更大,要么以某种方式允许在显示的图像上滚动。欢迎提出建议。
我设法想出了一个并不完美的解决方案,但它允许用户获得他们试图查看的文档的更多细节。
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>
在代码隐藏页上,我将值存储在代码的"使用(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渲染为窗口的高度。因此,该代码不会将tiff渲染得更高,只会将其渲染得更宽。因此,尽管这很不雅,但它确实让用户"缩放"以更好地查看文档的细节。