发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
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.Runtime.InteropServices.ComTypes;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
initPage();
}
protected void initPage()
{
////string url = Server.MapPath("upload") +"\\"+ "soufunquan.jpg";
string url = "d:/quan.jpg";
String src = url; //源图像文件的绝对路径
String dest = "d:/xxx.jpg"; //生成的缩略图图像文件的绝对路径
int thumbWidth = 624; //要生成的缩略图的宽度
int thumbHeight = 306;
System.Drawing.Image image = System.Drawing.Image.FromFile(url); //利用Image对象装载源图像
//接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
int srcWidth = image.Width;
int srcHeight = image.Height;
//int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
//从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
//设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//把原始图像绘制成上面所设置宽高的缩小图
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
SolidBrush drawBrush = new SolidBrush(Color.Red);
Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
int xPos = image.Height - (image.Height - 25);
int yPos = 3;
//设置好x,y坐标,可以写多个
gr.DrawString("zydzydyzydydydyyydydy", drawFont, drawBrush, xPos, yPos);
gr.DrawString("123123123123123123123", drawFont, drawBrush, xPos, yPos+20);
//保存图像,大功告成!
bmp.Save(dest);
//最后别忘了释放资源(译者PS:C#可以自动回收吧)
bmp.Dispose();
image.Dispose();
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。