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;
}
}
分享到:
相关推荐
本资源是基于Springmvc的图片上传案例,并对上传的图片添加了生成缩略图的功能。
jsp上传图片并生成缩略图example upload pictures and zoom it
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来...
java 上传图片时生成对应的缩略图,缩略图的大小可手动更改,代码中有详细注释
本文详细介绍在ASP.NET中上传图片并生成缩略图 在ASP.NET中上传图片并生成缩略图!
JSP上传图片并生成缩略图 JSP上传图片并生成缩略图
FLASH+PHP上传,返回值到当前页面,可以自定义传送值到PHP上传处理的页面,这样就可以定义接受图片...update.php逻辑处理,file_upload.php上传类,可定义源文件名或者随机命名和缩略图大小. 没分了,来赚点积分花花
Asp.net上传图片同时生成缩略图和水印图
html5图片上传本地生成缩略图预览
相关知识struts2,fileupload等,实现图片上传并生成缩略图。单文件上传,图片展示直接使用jQuery FancyBox,效果不错,值得初学者研究学习。
html5移动端图片上传本地生成缩略图预览,单图上传
从ueditor中把批量上传图片的flash分离出来,同时实现生成缩略图
使用jspsmartupload实现图片的批量上传,并保存到服务器的指定路径,自动生成缩略图,与图片的路径相同。(需要导入jspsmartupload.jar,在工程下的WebContent\WEB-INF\lib中 )
用JSP实现上传图片自动生成缩略图 很简单的 大家看看把
本示例代码核心都来自于互联网,具体出处不明,本示例已经过本人的修改及功能整合,...可生成缩略图,自由设定可要,可不要 本代码最大的好处就是上传图片,未保存到数据库时重传图片不会产生垃圾图片,重传时直接把原图删除
php 上传图片生成缩略图 在PHP中获取EXIF信息应用EXIF函数库中的函数。首先,应该确定EXIF函数是否已经加载,在php.ini文件中查看“extension=php_exif.dll”前是否有分号“;”,如果有则说明未加载,那么就要去掉...
asp.net上传图片 并生成缩略图
多图片上传-判断图片像素-生成缩略图 多图片上传-判断图片像素-生成缩略图