关于C#图片上传限制大小
各位大虾,我在winfrm里上传图片到picturebox保存到数据库,上传的时候想固定图片大小,若超过该大小将自动按比例压缩图片处理,请问该如何实现呢?我要源代码哦,呵呵。 --------------------编程问答--------------------来看你怎么搞! --------------------编程问答-------------------- 在上传的时候用属性获取上传控件的文件的大小
然后判断 如果大于某个限定
就修改文件属性啊 个人意见 --------------------编程问答-------------------- 把Mode设为Zoom就行了啊 --------------------编程问答-------------------- //保存或修改按钮的事件
protected void btnSave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/images/Users");//得到物理路径
int userpicsize = fuUser.PostedFile.ContentLength;
if (0 < userpicsize && userpicsize < 40000)
{
if (Request.QueryString["id"] != null)//如果有传来参数,则执行更新操作
{
//根据上页面传来的参数去得到员工信息
int userId = Convert.ToInt32(Request.QueryString["id"]);
UserInfo userInfo = UserInfoManager.GetUserInfoById(userId);
userInfo.UserName = txtUserName.Text;
userInfo.PassWord = txtSurePwd.Text; //密码可修改
userInfo.Gender = Convert.ToInt32(rblGender.SelectedValue);
if (userInfo.UserPic != "noperson.jpg" || userInfo.UserPic.Trim().Length != 0)
{
//得到旧图片路径,并删除旧图片
string oldpath = path + "\\" + userInfo.UserPic;
File.Delete(oldpath);
}
if (fuUser.HasFile) //存放图片
{
userInfo.UserPic = userInfo.UserName + ".jpg"; //得到新图片名称
fuUser.PostedFile.SaveAs(path + "\\" + userInfo.UserPic); //保存图片
}
else
{
userInfo.UserPic = "noperson.jpg"; //暂无图片
}
int deId = Convert.ToInt32(ddlDepartInfo.SelectedValue);
userInfo.DepartId = DepartInfoManager.GetDepartInfoById(deId);
int roId = Convert.ToInt32(ddlUserRole.SelectedValue);
userInfo.Role = RoleInfoManager.GetRoleInfoById(roId);
int us = Convert.ToInt32(ddlUserState.SelectedValue);
userInfo.UserState = UserStateManager.GetUserStateById(us);
UserInfoManager.UpdateUserInfo(userInfo); //更新操作
Response.Redirect("UserInfo.aspx");
}
else //如果没有参数,则执行添加操作
{
//验证用户登录名的唯一性
if (Request.QueryString["id"] == null) //如果没参数,是添加操作
{
string checkname = txtUserName.Text.Trim();
UserInfo u = new UserInfo();
if (UserInfoManager.GetUserInfoByName(out u, checkname)) //添加操作时,验证用户名
{
Response.Write("<script>alert('用户名已存在!请重试')</script>");
return;
}
}
UserInfo userInfo = new UserInfo(); //创建用户信息并符值
userInfo.UserName = txtUserName.Text;
userInfo.PassWord = txtSurePwd.Text;
userInfo.Gender = Convert.ToInt32(rblGender.SelectedValue);
int deId = Convert.ToInt32(ddlDepartInfo.SelectedValue);
userInfo.DepartId = DepartInfoManager.GetDepartInfoById(deId);
int roId = Convert.ToInt32(ddlUserRole.SelectedValue);
userInfo.Role = RoleInfoManager.GetRoleInfoById(roId);
int us = Convert.ToInt32(ddlUserState.SelectedValue);
userInfo.UserState = UserStateManager.GetUserStateById(us);
if (fuUser.HasFile) //处理图片保存操作
{
//为图片命名
userInfo.UserPic = userInfo.UserName + ".jpg"; //此字段只存储图片名称
fuUser.PostedFile.SaveAs(path + "\\" + userInfo.UserPic); //保存图片
}
else
{
userInfo.UserPic = "noperson.jpg"; //暂无图片
}
//添加操作
int userId = UserInfoManager.AddUserInfo(userInfo);
Response.Redirect("UserInfo.aspx");
}
}
else
{
Response.Write("<script>alert('图片过大!应该小于(40kb)')</script>");
return;
}
} --------------------编程问答--------------------
别人要winform的,你写这么一大堆垃圾做什么? --------------------编程问答-------------------- 楼上大哥,你这么多代码给我没用啊。我要的是图片超过固定大小了如何自动按比例处理图片。 --------------------编程问答-------------------- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
image.GetThumbnailImage
OpenFileDialog ofd= new OpenFileDialog();
ofd.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif";
ofd.FilterIndex = 1;
ofd.FileName = "";
if (ofd.ShowDialog() == DialogResult.OK)
{
string path= ofd.FileName.ToString();
Bitmap bmPic = new Bitmap(path);
Point p= new Point(bmPic.Size);
if (p.X > mypicbox.Size.Width || p.Y > mypicbox.Size.Height)
{}
} --------------------编程问答--------------------
呵呵,楼上这位大哥判断了固定大小,我是想知道要是超过了大小怎样去按比例处理呢?不过还是谢谢你回答。 --------------------编程问答-------------------- 帮顶.... --------------------编程问答-------------------- 帮顶....````` --------------------编程问答-------------------- 这是基本形成缩略图的方法
private static void ThumbnailMake(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = 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), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
基于以上方法,形成你需要的方法
/// <summary>
/// 生成缩略图,根据maxSize,高宽自适应,不变形
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="maxSize">最大尺寸</param>
public static void MakByAuto(string originalImagePath, string thumbnailPath, int maxSize)
{
Image img = Image.FromFile(originalImagePath);
if (img.Height > img.Width)
{
ThumbnailMake(originalImagePath, thumbnailPath, maxSize, 0, "W");
}
else
{
ThumbnailMake(originalImagePath, thumbnailPath, 0, maxSize, "H");
}
if (img != null)
{
img.Dispose();
}
}
你可以在扩张方法再增加高宽判断可以扩张成如果小于一定宽,则不再处理。
方法缺点为:需要将图片上传到服务器上之后,才可以进行图片流处理。 --------------------编程问答-------------------- 问题解决了,呵呵。谢谢你们。
补充:.NET技术 , C#