GetThumbnail.aspx我是从网上拷到的,调用的时候提供的参数应该也是没有问题的,但是图片却不能正常输出,很奇怪.
请各位高人指点一下,不胜感激啊!!
以下为GetThumbnail.aspx文件:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="GetThumbnail.aspx.cs" Inherits="GetThumbnail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
以下为GetThumbnail.aspx.cs文件:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class GetThumbnail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Clear();
try
{
//需显示的图片的物理路径
string originalFileName = Request["fn"].ToString();
//需显示的图片的宽度
int thumbnailWidth = Convert.ToInt32(Request["tw"]);
//需显示的图片的高度
int thumbnailHeight = Convert.ToInt32(Request["th"]);
System.Drawing.Image img = System.Drawing.Image.FromFile(originalFileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
System.Drawing.Size newSize = this.GetNewSize(thumbnailWidth, thumbnailHeight, img.Width, img.Height);
System.Drawing.Bitmap outBmp = new Bitmap(thumbnailWidth, thumbnailHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp);
= System.Drawing. = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.Clear(System.Drawing.Color.White);
g.DrawImage(img, new Rectangle((thumbnailWidth - newSize.Width) / 2, (thumbnailHeight - newSize.Height) / 2, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
img.Dispose();
if (thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
Response.ContentType = "image/gif";
else
Response.ContentType = "image/jpeg";
System.Drawing.Imaging.EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[] { 100 };
System.Drawing.Imaging.EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int fwd = 0; fwd < arrayICI.Length; fwd++)
{
if (arrayICI[fwd].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[fwd];
break;
}
}
if (jpegICI != null)
{
outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(Response.OutputStream, thisFormat);
}
outBmp.Dispose();
}
catch (Exception err)
{
}
}
private System.Drawing.Size GetNewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
}
以下为调用该文件的代码:

<div>
<asp:DataList ID="pictureList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="800px">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"ProductName") %><br />
<img height="80" src='share/GetThumbnail.aspx?fn=D:\1.jpg&tw=80&th=80' width="80" alt="产品图片"/>
</ItemTemplate>
</asp:DataList></div>
相关图片如下:
只有本站会员才能查看附件,请 登录