62,072
社区成员
发帖
与我相关
我的任务
分享
string imgExt, imgs = "", imgm = "";
System.Drawing.Image oriImg, newImg;
string imageSavePath = Server.MapPath("../UpImage/");
int imageThumbWidth = 0;//小缩略图
int imageThumbHeight = 0;
string imageNewName = DateTime.Now.ToString("yyyyMMddHHmmss");
try
{
if (image.Value != "")
{
HttpPostedFile PostedFile = image.PostedFile;
imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
if (imgExt != ".jpg" && imgExt != ".gif" && imgExt != ".png")
{
MessageBox.Show(this.Page, "上传的文件格式错误");
return;
}
else
{
imageNewName += imgExt;
oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
if (oriImg.Width < imageThumbWidth && oriImg.Height < imageThumbWidth)
{
imageThumbHeight = oriImg.Height;
imageThumbWidth = oriImg.Width;
}
else
{
if (oriImg.Width < oriImg.Height)
{
imageThumbHeight = imageThumbWidth;
imageThumbWidth = imageThumbHeight * oriImg.Width / oriImg.Height;
}
else if (oriImg.Width > oriImg.Height)
{
imageThumbHeight = imageThumbWidth * oriImg.Height / oriImg.Width;
}
else imageThumbHeight = imageThumbWidth;
}
newImg = oriImg.GetThumbnailImage(imageThumbWidth, imageThumbHeight, null, new System.IntPtr(0));
imgs = "o_" + imageNewName;
imgm = "t_" + imageNewName;
switch (imgExt)
{
case ".jpg":
oriImg.Save(imageSavePath + imgs, System.Drawing.Imaging.ImageFormat.Jpeg);
newImg.Save(imageSavePath + imgm, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
oriImg.Save(imageSavePath + imgs, System.Drawing.Imaging.ImageFormat.Gif);
newImg.Save(imageSavePath + imgm, System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
oriImg.Save(imageSavePath + imgs, System.Drawing.Imaging.ImageFormat.Png);
newImg.Save(imageSavePath + imgm, System.Drawing.Imaging.ImageFormat.Png);
break;
}
oriImg.Dispose();
newImg.Dispose();
}
}
public void CreateMinImageAndDel(string originalImagePath, string thumbnailPath, int width, int height)
{
Graphics draw = null;
string FileExt = "";
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));
int towidth = width;
int toheight = height;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Image bitmap2 = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight));
try
{
//以jpg格式保存缩略图
FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
//用新建立的image对象拷贝bitmap对象 让g对象可以释放资源
draw = Graphics.FromImage(bitmap2);
draw.DrawImage(bitmap, 0, 0);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
//保存调整在这里即可
bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
if (FileUpLogo.HasFile)
{
//取得上传文件的大小
int FileLen = FileUpLogo.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpLogo.PostedFile;
//创建数据流对象
System.IO.Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);
//将FileData 赋值给实体
brandModel.fld_logo = FileData;
}
HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
fileLength = upFile.ContentLength;//记录文件的长度
try
{
if(fileLength==0)//当文件长度为0的时候
{
txtMessage.Text = "请选择要上传的文件!";
}
else
{
byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
//插入数据库语句
SqlCommand comm = new SqlCommand(insertStr,conn);
comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
conn.Open();//打开数据库连接
comm.ExecuteNonQuery();//添加数据
conn.Close();//关闭数据库
txtMessage.Text = "你已经成功的上传了图片";
}
}
catch(Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
}
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from Tb where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
conn.Close();
}