HttpPostedFile visualPhotoFile = VisualPhoto.PostedFile;
string photoType = visualPhotoFile.ContentType;
string extendFileName = visualPhotoFile.ContentType;
string strFileNameOnServer ="";
switch(photoType)
{
case("image/gif"):
extendFileName = "gif";
break;
case("image/pjpeg"):
extendFileName = "jpg";
break;
default:
string strPhotoTypeError = "<script language=javascript> alert('请选择jpg或者gif格式的照片');</script>";
this.RegisterStartupScript("s1",strPhotoTypeError);
return;
}
visualPhotoFile.SaveAs("Name");
string photoType = visualPhotoFile.ContentType;
string extendFileName = visualPhotoFile.ContentType;
string strFileNameOnServer ="";
switch(photoType)
{
case("image/gif"):
extendFileName = "gif";
break;
case("image/pjpeg"):
extendFileName = "jpg";
break;
default:
string strPhotoTypeError = "<script language=javascript> alert('请选择jpg或者gif格式的照片');</script>";
this.RegisterStartupScript("s1",strPhotoTypeError);
return;
}
visualPhotoFile.SaveAs("Name");
<%@ 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 newfile = path+".png";
string strAdd=strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(path);
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("宋体", 12),new SolidBrush(Color.Red),left,right);
//Color backColor = output.GetPixel(1, 1);
//output.MakeTransparent(backColor);
output.Save(newfile,System.Drawing.Imaging.ImageFormat.Png);
Response.ContentType = "image/Png";
ImgPreview.Visible=true;
ImgPreview.ImageUrl=newfile;
}bool ThumbnailCallback()
{
return true;
}string path;void Button_Click(object sender, EventArgs e)
{
int width,height,left,right;
string strAddInfo=txtAddInfo.Text;
width=Int32.Parse(txtWidth.Text);
height=Int32.Parse(txtHeight.Text);
left=Int32.Parse(txtLeft.Text);
right=Int32.Parse(txtRight.Text);
if(!(uploadFile.PostedFile.ContentLength>0))
{
lblErrInfo.Text="没有选择文件";
}
else
{
path = Server.MapPath("./Uploads/"+RndNum(6)+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);
}
}
} private string RndNum(int VcodeNum) //产生随机字符串
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
string[] VcArray = Vchar.Split(',') ;
string VNum = "" ;//由于字符串很短,就不用StringBuilder了
int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数 //采用一个简单的算法以保证生成随机数的不同
Random rand =new Random();
for ( int i = 1 ; i < VcodeNum+1 ; i++ ) {
if ( temp != -1) {
rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35) ;
if (temp != -1 && temp == t) {
return RndNum( VcodeNum );
}
temp = t ;
VNum += VcArray[t]; }
return VNum ;
}</script>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="post" enctype="multipart/form-data" runat="server">
<p>
<input id="uploadFile" type="file" runat="server" />
<asp:Label id="lblErrInfo" runat="server" forecolor="Red"></asp:Label>
</p>
<p>
width:<asp:TextBox id="txtWidth" runat="server" Width="40px">100</asp:TextBox>
height:<asp:TextBox id="txtHeight" runat="server" Width="40px">150</asp:TextBox>
</p>
<p>
添加信息:<asp:TextBox id="txtAddInfo" runat="server">AspxBoy.Com</asp:TextBox>
</p>
<p>
信息位置:left:<asp:TextBox id="txtLeft" runat="server" Width="40px">10</asp:TextBox>
right:<asp:TextBox id="txtRight" runat="server" Width="40px">135</asp:TextBox>
</p>
<p>
<input id="button" type="button" value="上传生成缩略图" onServerClick="Button_Click" runat="server" />
</p>
<p><asp:Image id="ImgPreview" runat="server"></asp:Image>
<asp:TextBox id="t1" runat="server"></asp:TextBox>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
希望对你有所帮助!