using System;
using System.Configuration;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TipDa;
//http://dcdc723.iteye.com
//by dcdc723 指尖
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlHelper sqlhelper = new SqlHelper();
// FileUpload1.Attributes.Add("onchange", "return show()");
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;
string size = FileUpload1.PostedFile.ContentLength.ToString();//文件大小.
string type = FileUpload1.PostedFile.ContentType;//文件类型.
string type2 = name.Substring(name.LastIndexOf(".") + 1);//LastIndexOf()最后一个索引位置匹配.Substring()里面的+1是重载.
// name = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Millisecond+"." + type2;
Random rd = new Random();
string sj = (rd.Next() + 10).ToString();
name = DateTime.Now.ToString("yyyyMMddHHmmss") + sj + "." + type2;
string ipath = Server.MapPath("~/js") + "\\" + name;//取得根目录下面的路径.
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png" || type2 == "jpeg")
{
Response.Write(ipath);
// Response.End();
FileUpload1.SaveAs(ipath);//保存方法,参数是一个地址字符串.
Label1.Text = "~/js/" + name;
string picbig = "11";
bool flag=false;
FixPic2(ipath, ref picbig,ref flag);
if (flag)
{
Response.Write("<font color=red>"+picbig+"</font>"); //大图名字
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('上传成功!');</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件类型不正确!');</script>");
}
}
protected void FileUpload1_Disposed(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('改变');</script>");
}
private string FixPic2(string pic,ref string picbig,ref bool flag)
{
if (!System.IO.File.Exists(pic))
return null;
System.Drawing.Image image = System.Drawing.Image.FromFile(pic);
if (image != null)
{
if (image.Width > 220 || image.Height > 220)
{
System.Drawing.Bitmap final_image = null;
System.Drawing.Graphics graphic = null;
try
{
string pic1 = pic.Insert((pic.Length - 4),"_big");
picbig = pic1;
System.IO.File.Copy(pic, pic1, true);
int width = image.Width;
int height = image.Height;
int target_width = 220;
int target_height = 220;
int new_width, new_height;
float target_ratio = (float)target_width / (float)target_height;
float image_ratio = (float)width / (float)height;
if (target_ratio > image_ratio)
{
new_height = target_height;
new_width = (int)Math.Floor(image_ratio * (float)target_height);
}
else
{
new_height = (int)Math.Floor((float)target_width / image_ratio);
new_width = target_width;
}
new_width = new_width > target_width ? target_width : new_width;
new_height = new_height > target_height ? target_height : new_height;
final_image = new System.Drawing.Bitmap(target_width, target_height);
graphic = System.Drawing.Graphics.FromImage(final_image);
graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height));
int paste_x = (target_width - new_width) / 2;
int paste_y = (target_height - new_height) / 2;
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */
//graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height);
graphic.DrawImage(image, paste_x, paste_y, new_width, new_height);
image.Dispose();
//string newFileName = System.IO.Path.GetDirectoryName(Server.MapPath(pic)) + "/" + System.IO.Path.GetFileNameWithoutExtension(pic) + "_s" + System.IO.Path.GetExtension(pic);
Response.Write(pic);
final_image.Save(pic, System.Drawing.Imaging.ImageFormat.Jpeg);
flag = true;
// Response.Write("Fix OK--:" + pic + "");
Response.Flush();
//return System.IO.Path.GetDirectoryName(pic).Replace("\\", "/") + "/" + System.IO.Path.GetFileNameWithoutExtension(pic) + "_s" + System.IO.Path.GetExtension(pic);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
if (final_image != null) final_image.Dispose();
if (graphic != null) graphic.Dispose();
if (image != null) image.Dispose();
}
}
}
return null;
}
}