zoukankan      html  css  js  c++  java
  • asp.net上传图片压缩

    转自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();
            }
        }


    }

  • 相关阅读:
    执行插件的替代方式:用JS调用操作
    查找字段的筛选-使用addCustomView
    Dynamics CRM 中Web API中的深度创建(Deep Insert)
    使用JS通过Web API执行批量操作,多个操作是一个事务!
    使用Dynamics 365 CE Web API查询数据加点料及选项集字段常用查询
    配置Postman通过OAuth 2 implicit grant获取Dynamics 365 CE Online实例的Access Token
    Dynamics 365 Customer Engagement中使用JavaScript和C#调用操作Action示例
    Dynamics 365 We API ODATA语法根据父记录查询子记录,根据子记录查询父记录(附上根据团队,队列名称查成员)
    Dynamics 365 Online通过OAuth 2 Client Credential授权(Server-to-Server Authentication)后调用Web API
    控制台程序(C#)不弹出登录窗口连接到Dynamics CRM Online的Web API
  • 原文地址:https://www.cnblogs.com/fogwang/p/2666580.html
Copyright © 2011-2022 走看看