using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.UI;
namespace My532ad.Func
{
/// <summary>
/// myUpimg 的摘要说明。
/// </summary>
public class myUpimg:Page
{
public string[] myupimgfile()
{
string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];
string imgPath, imgType;
string mPath;
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg")
{
string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
string originalFilename = mPath + "\\" + newname;
//生成的高质量图片名称
string strGoodFile = mPath + "\\" + newname;
//生成的低质量图片名称
//string strBadFile = mPath + "\\" + newname;
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
Image image = Image.FromFile(originalFilename);
//取得图片大小
Size size = new Size(image.Width/iScale, image.Height/iScale);
//新建一个bmp图片
Image bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//水印字 14:131,37; 10:90,34; 8:74,12
int fontsize = 10;
int mygx = 10;
int mygy = 20;
if (image.Width > 160)
{
fontsize = 14;
mygx = (image.Width - 160)/2;
}
if (image.Width > 121 && image.Width < 161)
{
fontsize = 10;
mygx = (image.Width - 121)/2;
}
if (image.Width < 122)
{
fontsize = 8;
if (image.Width > 74)
{
mygx = (image.Width - 74)/2;
}
else
{
mygx = 1;
}
}
if (image.Height > 37)
{
mygy = image.Height - 60;
}
else
{
mygy = 10;
}
Font f = new Font("楷体", fontsize);
Brush b = new SolidBrush(Color.FromArgb(150, 1, 1, 1));
Brush b1 = new SolidBrush(Color.FromArgb(150, 250, 250, 250));
g.DrawString("www.532ad.com", f, b, mygx, mygy + 20);
g.DrawString("www.532ad.com", f, b1, mygx, mygy + 21);
//Font f = new Font("宋体", fontsize);
//Brush b = new SolidBrush(Color.FromArgb( 150, 50, 50, 50 ));
//string addText ="青岛分类广告网 http://www.532ad.com";
//Font f1 = new Font("Impact", 14);
//Brush b1 = new SolidBrush(Color.FromArgb( 250, 50, 50, 50 ));
//string addText1 ="http://www.532ad.com";
image.Dispose();
g.Dispose();
//g.DrawString(addText, f, b, 10, 10);
if (imgType == "jpg" || imgType == "jpeg")
bitmap.Save(strGoodFile, ImageFormat.Jpeg);
if (imgType == "gif")
bitmap.Save(strGoodFile, ImageFormat.Gif);
//取得原图像的普通缩略图
//System.Drawing.Image img = image.GetThumbnailImage(image.Width/iScale, image.Height/iScale, null, IntPtr.Zero);
//保存普通缩略图
//if (imgType == "jpg" || imgType == "jpeg")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//if (imgType == "gif")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Gif);
//img.Dispose();
}
if (imgType == "gif")
{
string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
}
}
if (mynum == 0)
{
myfilename[mynum] = newfilename;
}
else
{
myfilename[mynum] = newfilename;
}
}
return myfilename;
}
public string[] myupimgfile1()
{
string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];
string imgPath, imgType;
string mPath;
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
}
}
myfilename[mynum] = newfilename;
}
return myfilename;
}
public string[] myupimgfile2()
{
string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];
string imgPath, imgType;
string mPath;
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("clubimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
#region 存图
string originalFilename = mPath + "\\" + newname;
//生成的高质量图片名称
string strGoodFile = mPath + "\\" + newname;
//生成的低质量图片名称
//string strBadFile = mPath + "\\" + newname;
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
Image image = Image.FromFile(originalFilename);
//取得图片大小
//Size size = new Size(image.Width/iScale, image.Height/iScale);
int hi =0;
int wi =0;
if(image.Width>300)
{
wi=300;
}
else
{
}
if(image.Height>300)
{
hi=300;
}
else
{
}
Size size = new Size(wi,hi);
//新建一个bmp图片
Image bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//水印字 14:131,37; 10:90,34; 8:74,12
int fontsize = 10;
int mygx = 10;
int mygy = 20;
if (image.Width > 160)
{
fontsize = 14;
mygx = (image.Width - 160)/2;
}
if (image.Width > 121 && image.Width < 161)
{
fontsize = 10;
mygx = (image.Width - 121)/2;
}
if (image.Width < 122)
{
fontsize = 8;
if (image.Width > 74)
{
mygx = (image.Width - 74)/2;
}
else
{
mygx = 1;
}
}
if (image.Height > 37)
{
mygy = image.Height - 60;
}
else
{
mygy = 10;
}
Font f = new Font("楷体", fontsize);
Brush b = new SolidBrush(Color.FromArgb(150, 1, 1, 1));
Brush b1 = new SolidBrush(Color.FromArgb(150, 250, 250, 250));
g.DrawString("www.532ad.com", f, b, mygx, mygy + 20);
g.DrawString("www.532ad.com", f, b1, mygx, mygy + 21);
//Font f = new Font("宋体", fontsize);
//Brush b = new SolidBrush(Color.FromArgb( 150, 50, 50, 50 ));
//string addText ="青岛分类广告网 http://www.532ad.com";
//Font f1 = new Font("Impact", 14);
//Brush b1 = new SolidBrush(Color.FromArgb( 250, 50, 50, 50 ));
//string addText1 ="http://www.532ad.com";
image.Dispose();
g.Dispose();
//g.DrawString(addText, f, b, 10, 10);
if (imgType == "jpg" || imgType == "jpeg")
bitmap.Save(strGoodFile, ImageFormat.Jpeg);
if (imgType == "gif")
bitmap.Save(strGoodFile, ImageFormat.Gif);
newfilename = newname;
//取得原图像的普通缩略图
//System.Drawing.Image img = image.GetThumbnailImage(image.Width/iScale, image.Height/iScale, null, IntPtr.Zero);
//保存普通缩略图
//if (imgType == "jpg" || imgType == "jpeg")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//if (imgType == "gif")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Gif);
//img.Dispose();
//}
/*if (imgType == "gif")
{
/*string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;*/
/*newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
}*/
#endregion
}
}
myfilename[mynum] = newfilename;
}
return myfilename;
}
public string[] myupimgfile3()
{
string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];
string imgPath, imgType;
string mPath;
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../adimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
}
}
myfilename[mynum] = newfilename;
}
return myfilename;
}
public bool deleteimg(int empid, string senderid)
{
bool deletebool = true;
try
{
DataBase db = new DataBase();
string[] myimgname = new string[3];
db.Open();
db.comm = new SqlCommand("select id,senderid,imgname,imgname1,imgname2 from info where senderid = '" + senderid + "'and id = " + empid, db.conn);
db.dr = db.comm.ExecuteReader();
if (db.dr.Read())
{
myimgname[0] = db.dr["imgname"].ToString();
myimgname[1] = db.dr["imgname1"].ToString();
myimgname[2] = db.dr["imgname2"].ToString();
}
db.dr.Close();
db.comm.Dispose();
db.conn.Close();
for (int i = 0; i < 3; i++)
{
if (myimgname[i] != "" && File.Exists(Server.MapPath("../infoimg/" + myimgname[i])) == true)
{
File.Delete(Server.MapPath("../infoimg/" + myimgname[i]));
}
}
}
catch
{
deletebool = false;
}
return deletebool;
}
public bool deleteimg(int empid)
{
bool deletebool = true;
try
{
DataBase db = new DataBase();
string[] myimgname = new string[1];
db.Open();
db.comm = new SqlCommand("select * from movement where id = " + empid, db.conn);
db.dr = db.comm.ExecuteReader();
if (db.dr.Read())
{
myimgname[0] = db.dr["imgname"].ToString();
}
db.dr.Close();
db.comm.Dispose();
db.conn.Close();
for (int i = 0; i < 1; i++)
{
if (myimgname[i] != "" && File.Exists(Server.MapPath("../infoimg/" + myimgname[i])) == true)
{
File.Delete(Server.MapPath("../infoimg/" + myimgname[i]));
}
}
}
catch
{
deletebool = false;
}
return deletebool;
}
}
}
风格的
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "/upfile/" + fileName + extension;
UploadFile.PostedFile.SaveAs(path);
// UploadFile.Dispose();
//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 32);
Brush b = new SolidBrush(Color.Red);
string addText = "ty158.com";
g.DrawString(addText, f, b, 10, 10);
image.Save("c://gugug.jpg");
image.Dispose();
g.Dispose();
分享到:
相关推荐
本文实例讲述了C#实现HTTP上传文件的方法。分享给大家供大家参考。具体实现方法如下: 发送文件代码如下: 代码如下: /// /// Http上传文件 /// public static string HttpUploadFile(string url, string ...
Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式...
上传文件大全sql语句 将上传是时间作为上传文件的名字将上传是时间作为上传文件的名字
JSP上传文件(有进度条)可上传大文件 网上有很多文章都是jspsmart,由于jspsmart直接在内存中操作数据,如果上传大文件或多台计算机上传的话会出错,还有一些方法传得比较慢。 为了方便上传文件,本人写了一个传输...
uniapp实现上传文件功能,uniapp实现上传文件功能uniapp实现上传文件功能uniapp实现上传文件功能uniapp实现上传文件功能uniapp实现上传文件功能
C# WebClient 上传文件 WebClient上传文件至服务器
dubbo上传文件+oss上传文件服务
C#通过winform和http上传文件C#通过winform和http上传文件C#通过winform和http上传文件C#通过winform和http上传文件C#通过winform和http上传文件
java上传文件到服务器,将本地的文件上传到服务器上去
基于openresty+nginx+lua实现文件服务器(包括获取文件及上传文件)
flash上传文件flash上传文件flash上传文件flash上传文件
批量上传文件带进度条批量上传文件带进度条批量上传文件带进度条批量上传文件带进度条
git上传文件文件类型限制,如屏蔽一些依赖文件及生成文件上传
用WinForm做的,可以上传文件到服务器。
java上传文件的两个工具jar包,缺一不可,亲测有用,为什么字数还不够。。。。
1.稍微修改可以实现自动上传文件 2.上传文件不受大小限制 3。文件上传过程中可以显示当前上传进度,当前平均上传速度、上传文件已花费时长 上传文件大小和总的文件大小百分比 4。保证文件的稳定传输
linux脚本sftp上传文件
Android端上传文件的jar包
主要介绍了java实现上传文件类型检测过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
ExtJS验证文件上传类型,详细讲述ExtJS如何验证文件上传文件的类型!