博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net上传图片压缩
阅读量:6453 次
发布时间:2019-06-23

本文共 2771 字,大约阅读时间需要 9 分钟。

转自csdn论坛,正确已经测试,非常好用。只不过引用的时候有点问题,图片没有释放,见下面红字部分。
我抄一份代码给你,清清月儿的.  
using
System;
using
System.IO;
using
System.Drawing;
using
System.Drawing.Imaging;
public
class
ImageThumbnail
{
   
public
Image ResourceImage;
   
private
int
ImageWidth;
   
private
int
ImageHeight;
   
public
string
ErrorMessage;
   
public
ImageThumbnail(
string
ImageFileName)
    {
        ResourceImage
=
Image.FromFile(ImageFileName);
        ErrorMessage
=
""
;
    }
   
public
bool
ThumbnailCallback()
    {
       
return
false
;
    }
   
//
方法1,按大小
   
public
bool
ReducedImage(
int
Width,
int
Height,
string
targetFilePath)
    {
       
try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb
=
new
Image.GetThumbnailImageAbort(ThumbnailCallback);
            ReducedImage
=
ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
           
return
true
;
        }
       
catch
(Exception e)
        {
            ErrorMessage
=
e.Message;
           
return
false
;
        }
    }
   
//
方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
   
public
bool
ReducedImage(
double
Percent,
string
targetFilePath)
    {
       
try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb
=
new
Image.GetThumbnailImageAbort(ThumbnailCallback);
            ImageWidth
=
Convert.ToInt32(ResourceImage.Width
*
Percent);
            ImageHeight
=
(ResourceImage.Height)
*
ImageWidth
/
ResourceImage.Width;
//
等比例缩放
            ReducedImage
=
ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
           
return
true
;
        }
       
catch
(Exception e)
        {
            ErrorMessage
=
e.Message;
           
return
false
;
        }
    }
}
后台代码:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
_Default : System.Web.UI.Page
{
   
protected
void
Page_Load(
object
sender, EventArgs e)
    {
    }
   
protected
void
bt_upload_Click(
object
sender, EventArgs e)
    {
       
try
        {
           
if
(FileUpload1.PostedFile.FileName
==
""
)
            {
               
this
.lb_info.Text
=
"
请选择文件!
"
;
            }
           
else
            {
               
string
filepath
=
FileUpload1.PostedFile.FileName;
               
string
filename
=
filepath.Substring(filepath.LastIndexOf(
"
\\
"
)
+
1
);
               
string
serverpath1
=
Server.MapPath(
"
images/
"
)
+
filename;
               
string
serverpath2
=
Server.MapPath(
"
images/
"
)
+
System.DateTime.Now.ToString(
"
yyy-MM-dd-hh-mm-ss
"
)
+
Session.SessionID
+
filename;
                FileUpload1.PostedFile.SaveAs(serverpath1);
                ImageThumbnail img
=
new
ImageThumbnail(filepath);
                img.ReducedImage(
0.4
, serverpath2);
//
0.4表示缩小40%
               img.ResourceImage.Dispose(); //这里要释放一下
               
this
.lb_info.Text
=
"
上传成功!
"
;
            }
        }
       
catch
(Exception error)
        {
           
this
.lb_info.Text
=
"
上传发生错误!原因:
"
+
error.ToString();
        }
    }
}

转载于:https://www.cnblogs.com/fogwang/archive/2012/06/04/2666580.html

你可能感兴趣的文章
linux下单个网卡设置多IP
查看>>
大硬盘在服务器上使用
查看>>
阿里五年晋升三次,这个程序员要聊聊他的选择
查看>>
Linux 开机流程及修复MBR
查看>>
How-to use MySQL-python in Python
查看>>
从用户层面考虑如何做好一款产品
查看>>
MYSQL的limit和offset
查看>>
基于Corosync + LNMP + NFS 服务实现高可用
查看>>
Python 多线程
查看>>
[java理论篇]--JAVA反射机制
查看>>
PostgreSQL的HA(主备切换)
查看>>
安装Office2010/2007出现1935错误解决办法
查看>>
使用rpm包安装配置 LAMP
查看>>
如何利用PS脚本查询和管理硬盘空间
查看>>
AFN获取数据后刷新TableView
查看>>
我的友情链接
查看>>
vsftpd添加新用户
查看>>
Math 单元下的公用函数目录
查看>>
WinAPI: CreatePatternBrush - 建立位图画刷
查看>>
PHP配置文件详解php.ini
查看>>