您的位置:首页 > 其它

网上摸索,自己修改实现上传图片的功能

2016-10-14 09:10 585 查看
实现上传图片功能;
protected void Button1_Click(object sender, EventArgs e)
{
//判断上传控件中是否有值
if (FileUpimage.HasFile == false)
{
Response.Write("");
}
else
{
string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型
if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/png")
{
//获取文件信息
FileInfo file = new FileInfo(FileUpimage.PostedFile.FileName);
///随机数据
Guid guid = Guid.NewGuid();
string stamp = guid.ToString("N");
//生成随机数
Random aa = new Random();
string number = aa.Next(10000).ToString();
//原始图片保存路径
string path = "~/Fileupimage/" + stamp + ".gif";
//缩略图保存路径
string spath = "~/Fileupimage/" + number + ".gif";
try
{
//原始图片保存
FileUpimage.SaveAs(Server.MapPath(path));
//字体图片保存
Draw(Server.MapPath(path),Server.MapPath(spath));
//缩略图保存
//MakeThumbImage(Server.MapPath(path), Server.MapPath(spath), 200, 100);
//给隐藏的图片控件赋值并显示
Image1.Visible = true;
Image1.ImageUrl = spath;
Response.Write("");
}
catch
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}
///
///创建日期:2016-10-14
///创建人  :ss
///方法名称:MakeThumbImage
///内容摘要:生成缩略图,或者在图片上写字
///
/// 源图路径(物理路径)
/// 缩略图路径(物理路径)
/// 缩略图宽度
/// 缩略图高度
private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight)
{
System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath);
int tw = nWidth;
int th = nHeight;
///原始图片的宽度和高度
int sw = sImage.Width;
int sh = sImage.Height;
if (sw > tw)
{
sw = tw;
}
if (sh > th)
{
sh = th;
}
System.Drawing.Bitmap objPic, objNewPic;
objPic = new System.Drawing.Bitmap(sPath);
objNewPic = new System.Drawing.Bitmap(objPic, sw, sh);
objNewPic.Save(stPath);
sImage.Dispose();
objPic.Dispose();
objNewPic.Dispose();
}
public void Draw(string path,string stpath) {
System.Drawing.Image image = System.Drawing.Image.FromFile(path) ;// 具体这张图是从文件读取还是从picturebox什么的获取你来指定
using (Graphics g = Graphics.FromImage(image))
{
g.DrawString(TextBox1.Text, new Font("隶书", 23),
Brushes.Red, new PointF(0, 0));
g.Flush();
}
image.Save(stpath);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 上传