uploadimage
Label
===========================---
using 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.IO;
namespace StudyCase.upload
{
///
/// Summary description for uploadimage.
///
public class uploadimage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Image ImgPreview;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;
protected System.Web.UI.WebControls.Label lblErrInfo;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// if (Request.QueryString["uploadId"] == null)
// Response.Redirect("uploadimage.aspx?uploadId=" + Guid.NewGuid().ToString());
if(!Page.IsPostBack)
{
ImgPreview.Visible=false;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
///
/// generate the thumb of image
///
/// the width of thumb
/// the height of thumb
/// the stamp string on the thumb
/// left point of postion of stamp
/// right point of postion of stamp
/// true for auto size/ false for fixed size
void GetThumbnailImage(int width,int height,string strInfo,int left,int right,bool switcher)
{
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));
if(switcher==false)
{
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;
}
else
{
int h=oldimage.Height;
int w=oldimage.Width;
int k=12;
w=w/k;
h=h/k;
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(w, h,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;
}
private void Button1_Click(object sender, System.EventArgs e)
{
int width,height,left,right;
string strAddInfo="for testing jacky";
bool switcher=true;
width=400;
height=300;
left=0;
right=0;
//uploadfile is a ID of HTMLfileupload control
if(!(uploadFile.PostedFile.ContentLength>0))
{
lblErrInfo.Text="please select a file firstly!";
}
else
{
if(uploadFile.PostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
lblErrInfo.Text="Only image is allowed";
return;
}
//limited the size of uploading file
if(uploadFile.PostedFile.ContentLength<1024)
{
this.lblErrInfo.Text="file max size is 1024 ";
}
string path = Server.MapPath("./Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1));
if(File.Exists(path))
{
lblErrInfo.Text="已经有同名文件";
}
else
{
uploadFile.PostedFile.SaveAs(path);
GetThumbnailImage(width,height,strAddInfo,left,right,switcher);
}
}
}
}
}
转载于:https://www.cnblogs.com/bloodycool/archive/2006/09/11/501202.html