//开始生成缩略图 System.Drawing.Image oriImg, newImg; int thumbs_MaxHeight = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["thumbsHeight"]); int thumbs_MaxWidth = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["thumbsWidth"]); int ImageHeight = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["ImageHeight"]); int ImageWidth = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["ImageWidth"]);
[C#] public Image GetThumbnailImage( int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData );当所请求的缩略图大小约为 120×120 时,GetThumbnailImage 工作正常。如果从一个有嵌入式缩略图的 Image 对象中请求一个较大的缩略图像(比如 300×300),则在缩略图像的质量会有显著的降低。通过调用 DrawImage 缩放主图像(而非嵌入式缩略图),则效果可能较好。
思想: 你把商船的图片文件名称传递给一个缩放文件zoom.aspx,显示缩放图的那个image控件连接到zoom.aspx文件上!便可以按照你的意愿去显示! 以下是用VB.net作的,你参考一下: ===============default.aspx================== <asp:Image id="Image2" style="Z-INDEX: 103; LEFT: 424px; POSITION: absolute; TOP: 64px" runat="server" ImageUrl="http://localhost/ImgProject/Zoom/zoom.aspx"></asp:Image> <asp:Image id="Image1" style="Z-INDEX: 104; LEFT: 56px; POSITION: absolute; TOP: 72px" runat="server" ImageUrl="/ImgProject/Zoom/51.jpg"></asp:Image> <asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 424px; POSITION: absolute; TOP: 32px" runat="server">缩略图像</asp:Label> <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 32px" runat="server">原始图像</asp:Label> ===============default.aspx.vb=============== Public Class _default Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Image2 As System.Web.UI.WebControls.Image Protected WithEvents Image1 As System.Web.UI.WebControls.Image '注意: 以下占位符声明是 Web 窗体设计器所必需的。 '不要删除或移动它。 Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 End SubEnd Class===============zoom.aspx.vb================== Imports System.Drawing Imports System.Drawing.Imaging Imports System.Drawing.Drawing2D Imports System.IOPublic Class zoom Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub '注意: 以下占位符声明是 Web 窗体设计器所必需的。 '不要删除或移动它。 Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 Response.Clear() Dim src As String = "51.jpg" If src = "" Then Call sendError() Else If File.Exists(Server.MapPath(src)) Then Call sendFile() Else Call sendError() End If End If Response.End() End Sub Function newthumbSize(ByVal currentwidth, ByVal currentheight) Dim tempMultiplier As Double '缩小50% tempMultiplier = 0.5 Dim newSize As New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier)) Return newSize End Function Sub sendFile() Dim src As String = "51.jpg" '装载图像 Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(src)) '获取图像格式 Dim thisFormat = g.RawFormat '确定图像的新尺寸 Dim thumbSize As New Size thumbSize = newthumbSize(g.Width, g.Height) '创建缩略图 Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height) '根据图像的原始类型决定输出类型 If thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) Then Response.ContentType = "image/gif" Else Response.ContentType = "image/jpeg" End If '输出图像 imgOutput.Save(Response.OutputStream, thisFormat) 'output to the user g.Dispose() imgOutput.Dispose() End Sub '如果出错则调用此方法产生一个出错信息的图像 Sub sendError() Dim imgOutput As New Bitmap(120, 120, PixelFormat.Format24bppRgb) Dim g As Graphics = Graphics.FromImage(imgOutput) g.Clear(Color.Yellow) g.DrawString("ERROR!", New Font("verdana", 14, FontStyle.Bold), SystemBrushes.WindowText, New PointF(2, 2)) Response.ContentType = "image/gif" imgOutput.Save(Response.OutputStream, ImageFormat.Gif) g.Dispose() imgOutput.Dispose() End SubEnd Class
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=45E7E33C-F149-450E-B5D5-832958C20538
GUID = System.Guid.NewGuid().ToString();
string sFilename = GUID + "_" + Session.SessionID + "_" + strDt + System.IO.Path.GetExtension(mFile.FileName);
string origPath = strUploadPath + "orig/";
uplTheFile.PostedFile.SaveAs(Server.MapPath(origPath + sFilename));
//开始生成缩略图
System.Drawing.Image oriImg, newImg;
int thumbs_MaxHeight = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["thumbsHeight"]);
int thumbs_MaxWidth = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["thumbsWidth"]); int ImageHeight = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["ImageHeight"]);
int ImageWidth = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["ImageWidth"]);
oriImg = System.Drawing.Image.FromStream(mFile.InputStream);
//生成标准大图
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(origPath + sFilename));
System.Drawing.Size size = new Size(ImageWidth , ImageHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//newImg = oriImg.GetThumbnailImage(ImageWidth,ImageHeight,null,new System.IntPtr(0));
switch(fileExt)
{
case ".jpg":
bitmap.Save(Server.MapPath(strUploadPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
bitmap.Save(Server.MapPath(strUploadPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
bitmap.Save(Server.MapPath(strUploadPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Png);
break;
default:
bitmap.Save(Server.MapPath(strUploadPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
//标准大图生成完成
image.Dispose();
g.Dispose();
GC.Collect();
//生成缩略图
newImg = oriImg.GetThumbnailImage(thumbs_MaxWidth,thumbs_MaxHeight,null,new System.IntPtr(0));
string strUploadThumbsPath = strUploadPath + "thumbs/";
switch(fileExt)
{
case ".jpg":
newImg.Save(Server.MapPath(strUploadThumbsPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
newImg.Save(Server.MapPath(strUploadThumbsPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
newImg.Save(Server.MapPath(strUploadThumbsPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Png);
break;
default:
newImg.Save(Server.MapPath(strUploadThumbsPath + sFilename) ,System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
//缩略图生成完成
这样得到的图不会失真?
.net 框架/使用.net框架编程 /绘制和编辑图像/使用GDI托管类 /使用图像,位图和图元文件/缩放时使用插值模式控制图像质量
public Image GetThumbnailImage(
int thumbWidth,
int thumbHeight,
Image.GetThumbnailImageAbort callback,
IntPtr callbackData
);当所请求的缩略图大小约为 120×120 时,GetThumbnailImage 工作正常。如果从一个有嵌入式缩略图的 Image 对象中请求一个较大的缩略图像(比如 300×300),则在缩略图像的质量会有显著的降低。通过调用 DrawImage 缩放主图像(而非嵌入式缩略图),则效果可能较好。
你把商船的图片文件名称传递给一个缩放文件zoom.aspx,显示缩放图的那个image控件连接到zoom.aspx文件上!便可以按照你的意愿去显示!
以下是用VB.net作的,你参考一下:
===============default.aspx==================
<asp:Image id="Image2" style="Z-INDEX: 103; LEFT: 424px; POSITION: absolute; TOP: 64px" runat="server"
ImageUrl="http://localhost/ImgProject/Zoom/zoom.aspx"></asp:Image>
<asp:Image id="Image1" style="Z-INDEX: 104; LEFT: 56px; POSITION: absolute; TOP: 72px" runat="server"
ImageUrl="/ImgProject/Zoom/51.jpg"></asp:Image>
<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 424px; POSITION: absolute; TOP: 32px" runat="server">缩略图像</asp:Label>
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 32px" runat="server">原始图像</asp:Label>
===============default.aspx.vb===============
Public Class _default
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Image2 As System.Web.UI.WebControls.Image
Protected WithEvents Image1 As System.Web.UI.WebControls.Image '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End SubEnd Class===============zoom.aspx.vb==================
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.IOPublic Class zoom
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Response.Clear() Dim src As String = "51.jpg" If src = "" Then
Call sendError()
Else
If File.Exists(Server.MapPath(src)) Then
Call sendFile()
Else
Call sendError()
End If
End If Response.End() End Sub Function newthumbSize(ByVal currentwidth, ByVal currentheight) Dim tempMultiplier As Double '缩小50%
tempMultiplier = 0.5 Dim newSize As New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier)) Return newSize End Function Sub sendFile()
Dim src As String = "51.jpg" '装载图像
Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(src)) '获取图像格式
Dim thisFormat = g.RawFormat '确定图像的新尺寸
Dim thumbSize As New Size
thumbSize = newthumbSize(g.Width, g.Height) '创建缩略图
Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height) '根据图像的原始类型决定输出类型
If thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) Then
Response.ContentType = "image/gif"
Else
Response.ContentType = "image/jpeg"
End If '输出图像
imgOutput.Save(Response.OutputStream, thisFormat) 'output to the user g.Dispose()
imgOutput.Dispose() End Sub '如果出错则调用此方法产生一个出错信息的图像
Sub sendError() Dim imgOutput As New Bitmap(120, 120, PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(imgOutput)
g.Clear(Color.Yellow)
g.DrawString("ERROR!", New Font("verdana", 14, FontStyle.Bold), SystemBrushes.WindowText, New PointF(2, 2)) Response.ContentType = "image/gif" imgOutput.Save(Response.OutputStream, ImageFormat.Gif) g.Dispose()
imgOutput.Dispose()
End SubEnd Class