showing.aspx.csusing System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Drawing.Imaging; using System.Drawing.Drawing2D;namespace rabbit { /// <summary> /// showimg 的摘要说明。 /// </summary> public class showimg : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 Response.ContentType="image/jpeg";
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
ImgPreview.Visible=false;
}
}
void GetThumbnailImage(int width,int height,string strInfo,int left,int right)
{
string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1);
string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1)+".jpg";
string strAdd=strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Response.Clear();
Bitmap output=new Bitmap(thumbnailImage);
Graphics g=Graphics.FromImage(output);
g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right);
output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/gif";
ImgPreview.Visible=true;
ImgPreview.ImageUrl=newfile;
}
bool ThumbnailCallback()
{
return true;
}
using System.Drawing.Imaging;namespace ifyr.util{
class ImageHandler {
static void Main(string[] args) {
Image sourceImage = Image.FromFile(@"e:\source.jpg");Compress(sourceImage, @"e:\target.jpg");
CreateThumb(sourceImage, 120, 160, @"e:\thumb.jpg");sourceImage.Dispose();
}///
/// 将图片转换成200k以下的图片
/// 即生成800*800以内像素,65%品质的JPEG格式图片
/// 请先判断图片大小,如果小于200k,则不需压缩,以免损失图片质量
/// 可以改写为输出到OutputStream
///
/// 原图
/// 压缩图片的保存文件名
public static void Compress(Image sourceImage, string filename) {
int targetWidth = 800;
int targetHeight = 800;if(sourceImage.Height <= 800 && sourceImage.Width <= 800) {
targetHeight = sourceImage.Height;
targetWidth = sourceImage.Width;
} else if(sourceImage.Height > sourceImage.Width) {
targetWidth = sourceImage.Width * 800 / sourceImage.Height;
} else {
targetHeight = sourceImage.Height * 800 / sourceImage.Width;
}// 缩放图片
Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(targetImage);
g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
g.Dispose();
// 设置输出格式
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65L);
ImageCodecInfo codeInfo = null;
ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo info in codeInfos) {
if(info.MimeType.Equals("image/jpeg")) {
codeInfo = info;
break;
}
}
if(codeInfo != null) {
targetImage.Save(filename, codeInfo, encParams);
}
targetImage.Dispose();
}///
/// 生成缩略图
/// 可以改写成输出到OutputSteam
///
/// 原图
/// 缩略图宽度
/// 缩略图高度
/// 缩略图的保存文件名
public static void CreateThumb(Image sourceImage, int thumbWidth, int thumbHeight, string filename) {
int tw = thumbWidth;
int th = thumbHeight;if(sourceImage.Height < th && sourceImage.Width < tw) {
th = sourceImage.Height;
tw = sourceImage.Width;
} else if(sourceImage.Height / th > sourceImage.Width / tw) {
tw = sourceImage.Width * th / sourceImage.Height;
} else {
th = sourceImage.Height * tw / sourceImage.Width;
}// 将图片缩放到一个空白背景的图片上,生成大小一致的缩略图
Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(thumbImage);
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(sourceImage, (thumbWidth - tw) / 2, (thumbHeight - th) / 2, tw, th);
g.Dispose();thumbImage.Save(filename, ImageFormat.Jpeg);
thumbImage.Dispose();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;namespace rabbit
{
/// <summary>
/// showimg 的摘要说明。
/// </summary>
public class showimg : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.ContentType="image/jpeg";
System.Drawing.Image img=System.Drawing.Image.FromFile("E:\\pictrue\\P1020075.JPG");
//img.RotateFlip(RotateFlipType.Rotate90FlipNone);
Bitmap bmp=new Bitmap(img,img.Width/20,img.Height/20);
bmp.Save(Response.OutputStream,ImageFormat.Jpeg);
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}showing.aspx<%@ Page language="c#" Codebehind="showimg.aspx.cs" AutoEventWireup="false" Inherits="rabbit.showimg" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>showimg</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
http://yefei520.cnblogs.com/archive/2006/01/05/311343.html