码迷,mamicode.com
首页 > 其他好文 > 详细

按比例压缩图片和图片转换成BASE64

时间:2017-12-29 15:18:50      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:idt   算法   efault   缩小   false   returns   sage   rap   nbsp   

通常会需要将突破转换为字符串的操作,贴此详例,仅供参考和积累

   #region 按比例压缩图片和图片转换成BASE64
        /// <summary>
        /// 按照比例缩小图片
        /// </summary>
        /// <param name="srcImage">要缩小的图片</param>
        /// <param name="percent">缩小比例</param>
        /// <returns>缩小后的结果</returns>
        public static Bitmap PercentImage(Image srcImage, double percent)
        {
            // 缩小后的高度
            int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
            // 缩小后的宽度
            int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
            try
            {
                // 要保存到的图片
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量
                g.InterpolationMode = InterpolationMode.Default;
                g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 将图片转换成BASE64
        /// </summary>
        /// <param name="imagefile"></param>
        /// <returns></returns>
        public static string GetBase64FromImage(string imagefile)
        {
            string strbaser64 = "";
            try
            {
                Bitmap bmp = new Bitmap(imagefile);
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
                bmp.Dispose();
            }
            catch (Exception)
            {
                //throw new Exception("Something wrong during convert!");
            }
            return strbaser64;
        }
        #endregion


        //将base64字符转换为图片保存
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strbase64">base64字符串</param>
        /// <param name="desPathUrl">图片存放路径</param>
       /// <returns></returns>
        public  static bool Base64StringToImage(string strbase64,string desPathUrl)
        {
            try
            {
                string imgUrl = string.Empty;
                byte[] arr = Convert.FromBase64String(strbase64);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);

                bmp.Save(desPathUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Close();
                return true;
            }
            catch (Exception ex)
            {
                WriteLog("操作:将base64转换为图片并存储,错误原因:"+ex.Message);
                return false;
            }
        }  

 

按比例压缩图片和图片转换成BASE64

标签:idt   算法   efault   缩小   false   returns   sage   rap   nbsp   

原文地址:https://www.cnblogs.com/DreakSeeker/p/8143738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!