ASP.Net 图片压缩

.aspx

<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="lb_info" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image1" runat="server" />

.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.PostedFile.FileName == "")
                {
                    this.lb_info.Text = "请选择文件!";
                }
                else
                {
                    byte[] fileBytePicture = new byte[FileUpload1.PostedFile.ContentLength];//用图片的长度来初始化一个字节数组存储临时的图片文件  
                    System.IO.Stream fileStream = FileUpload1.PostedFile.InputStream;//建立文件流对象  
                    fileStream.Read(fileBytePicture, 0, FileUpload1.PostedFile.ContentLength);
                    string photo = ByteToString(fileBytePicture);

                    //存储上传图片
                    string filepath = FileUpload1.PostedFile.FileName;
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    string serverpath = Server.MapPath("images/") + Guid.NewGuid().ToString() + ".jpg";
                    FileUpload1.PostedFile.SaveAs(serverpath);

                    //存储压缩图片  并返回压缩图片路径
                    string newImagePath = ImageThumbnail.ReducedImage(73, 73, serverpath);  //缩放成73*73
                    //string newImagePath = ImageThumbnail.ReducedImage(0.5, serverpath);
                    this.lb_info.Text = "上传成功!";

                    //读取压缩图片 转换成string类型
                    using (FileStream fs = new FileStream(newImagePath, FileMode.Open, FileAccess.Read))  ////将图片以文件流的形式进行保存
                    {
                        BinaryReader br = new BinaryReader(fs);
                        byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //将流读入到字节数组中
                        string photo1 = ByteToString(imgBytesIn);
                    }

                    
                }
            }
            catch (Exception error)
            {
                this.lb_info.Text = "上传发生错误!原因:" + error.ToString();
            }
        }

        /// <summary>  
        /// 将byte数组转为string类型  
        /// </summary>  
        /// <param name="bytes"></param>  
        /// <returns></returns>  
        public string ByteToString(byte[] bytes)
        {
            StringBuilder strBuilder = new StringBuilder();
            foreach (byte bt in bytes)
            {
                strBuilder.AppendFormat("{0:X2}", bt);
            }
            return strBuilder.ToString();
        }


/// <summary>
    /// 图片压缩
    /// </summary>
    public class ImageThumbnail
    {
        // 方法1,按大小
        /// <summary>
        ///按大小压缩
        /// </summary>
        /// <param name="Width">压缩宽</param>
        /// <param name="Height">压缩高</param>
        /// <param name="targetFilePath">要压缩的图片路径</param>
        /// <returns>返回压缩后的图片路径</returns>
        public static string ReducedImage(int Width, int Height, string targetFilePath)
        {
            using (Image ResourceImage = Image.FromFile(targetFilePath))
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(delegate { return false; });
                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);  //按大小压缩
                string newImagePath = targetFilePath.Substring(0, targetFilePath.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + ".jpg";  //压缩图片的存储路径
                ReducedImage.Save(newImagePath, ImageFormat.Jpeg);   //保存压缩图片
                ReducedImage.Dispose();
                return newImagePath;   //返回压缩图片的存储路径
            }
        }


        // 方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
        /// <summary>
        /// 按百分比 压缩
        /// </summary>
        /// <param name="Percent">缩小比例 缩小60% Percent为0.6 </param>
        /// <param name="targetFilePath">要压缩的图片路径</param>
        /// <returns>返回压缩后的图片路径</returns>
        public static string ReducedImage(double Percent, string targetFilePath)
        {
                using (Image ResourceImage = Image.FromFile(targetFilePath))
                {
                    Image ReducedImage;
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(delegate { return false; });
                    int imageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
                    int imageHeight = Convert.ToInt32(ResourceImage.Height * Percent);//等比例缩放
                    ReducedImage = ResourceImage.GetThumbnailImage(imageWidth, imageHeight, callb, IntPtr.Zero);
                    string newImagePath = targetFilePath.Substring(0, targetFilePath.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + ".jpg";  //压缩图片的存储路径
                    ReducedImage.Save(newImagePath, ImageFormat.Jpeg);
                    ReducedImage.Dispose();
                    return newImagePath;
                }
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值