关于asp.net c#图片缩略图尺寸太大的问题。
代码如下,出现的问题是缩略图反而比原图还要大,请问如何减少缩略图的文件大小呢?谢谢。protectedvoidbtn_upload_Click(objectsender,...
代码如下,出现的问题是缩略图反而比原图还要大,请问如何减少缩略图的文件大小呢?谢谢。
protected void btn_upload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
uploadimg_info.Text = "请选择文件!";
}
else
{
//string filePath = FileUpload1.PostedFile.FileName;
//string filename = filePath.Substring(filePath.LastIndexOf("\\")+1);
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + FileUpload1.PostedFile.FileName;
string fileType = filename.Substring(filename.LastIndexOf(".") + 1);
if (fileType == "jpg" || fileType == "JPG")
{
string serverPath = Server.MapPath("/img/s3/") + filename;
FileUpload1.PostedFile.SaveAs(serverPath);
System.Drawing.Image bigImg = System.Drawing.Image.FromFile(serverPath);
System.Drawing.Image thumImg = bigImg.GetThumbnailImage(350, 260, null, System.IntPtr.Zero);
thumImg.Save(Server.MapPath("/img/s2/") + filename);
System.Drawing.Image thumImg2 = bigImg.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
thumImg2.Save(Server.MapPath("/img/s1/") + filename);
bigImg.Dispose();
uploadimg_info.Text = "上传成功!";
if (imgSave.Value == "")
{
imgSave.Value = filename;
}
else
{
imgSave.Value += "," + filename;
}
rpt_filelist.DataSource = imgSave.Value.Split(',');
rpt_filelist.DataBind();
}
else
{
uploadimg_info.Text = "请选择jpg文件!";
}
}
}
请问有什么办法可以使生成的缩略图文件更小一些呢?
-----
调小了也是偏大,我调成150*150,尺寸居然还有70多k!太大了,不知是不是代码的问题? 展开
protected void btn_upload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
uploadimg_info.Text = "请选择文件!";
}
else
{
//string filePath = FileUpload1.PostedFile.FileName;
//string filename = filePath.Substring(filePath.LastIndexOf("\\")+1);
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + FileUpload1.PostedFile.FileName;
string fileType = filename.Substring(filename.LastIndexOf(".") + 1);
if (fileType == "jpg" || fileType == "JPG")
{
string serverPath = Server.MapPath("/img/s3/") + filename;
FileUpload1.PostedFile.SaveAs(serverPath);
System.Drawing.Image bigImg = System.Drawing.Image.FromFile(serverPath);
System.Drawing.Image thumImg = bigImg.GetThumbnailImage(350, 260, null, System.IntPtr.Zero);
thumImg.Save(Server.MapPath("/img/s2/") + filename);
System.Drawing.Image thumImg2 = bigImg.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
thumImg2.Save(Server.MapPath("/img/s1/") + filename);
bigImg.Dispose();
uploadimg_info.Text = "上传成功!";
if (imgSave.Value == "")
{
imgSave.Value = filename;
}
else
{
imgSave.Value += "," + filename;
}
rpt_filelist.DataSource = imgSave.Value.Split(',');
rpt_filelist.DataBind();
}
else
{
uploadimg_info.Text = "请选择jpg文件!";
}
}
}
请问有什么办法可以使生成的缩略图文件更小一些呢?
-----
调小了也是偏大,我调成150*150,尺寸居然还有70多k!太大了,不知是不是代码的问题? 展开
3个回答
展开全部
/// <summary>
/// 保存缩略图
/// </summary>
/// <param name="oldImage">源图</param>
/// <param name="image">目标图</param>
/// <param name="name">保存路径</param>
public static void DrawImage(System.Drawing.Image oldImage, System.Drawing.Image image,string name)
{
Graphics g = Graphics.FromImage(image);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.White);
g.DrawImage(oldImage,new Rectangle(0,0,image.Width,image.Height) ,new Rectangle(0,0,oldImage.Width,oldImage.Height),GraphicsUnit.Pixel);
image.Save(name,System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
}
先创建一张画板再进行绘图可以缩小图片所用内存,第二个参数image就是你上面通过代码
System.Drawing.Image thumImg = bigImg.GetThumbnailImage(350, 260, null, System.IntPtr.Zero);
thumImg.Save(Server.MapPath("/img/s2/") + filename);
System.Drawing.Image thumImg2 = bigImg.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
得到的两个image对象
/// 保存缩略图
/// </summary>
/// <param name="oldImage">源图</param>
/// <param name="image">目标图</param>
/// <param name="name">保存路径</param>
public static void DrawImage(System.Drawing.Image oldImage, System.Drawing.Image image,string name)
{
Graphics g = Graphics.FromImage(image);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.White);
g.DrawImage(oldImage,new Rectangle(0,0,image.Width,image.Height) ,new Rectangle(0,0,oldImage.Width,oldImage.Height),GraphicsUnit.Pixel);
image.Save(name,System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
}
先创建一张画板再进行绘图可以缩小图片所用内存,第二个参数image就是你上面通过代码
System.Drawing.Image thumImg = bigImg.GetThumbnailImage(350, 260, null, System.IntPtr.Zero);
thumImg.Save(Server.MapPath("/img/s2/") + filename);
System.Drawing.Image thumImg2 = bigImg.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
得到的两个image对象
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.Drawing.Image thumImg = bigImg.GetThumbnailImage(350, 260, null, System.IntPtr.Zero);
这里的350,260就是图片的宽度和高度,调小点就行了
这里的350,260就是图片的宽度和高度,调小点就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不懂, 学习下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询