C# webapi 文件流 stream 两种上传方式《第二部分 base64 图片上传》

 博客仅用于记录工作学习中遇到的坑,欢迎交流!

2.base64 图片上传

2.1 客户端

/// 
        /// 接口保存图片
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string SaveLearningImage(int pic_width, int pic_height, string bitMapData, string url, int saveType = 1)
        {
            var requstData = new
            {
                pic_width = pic_width,
                pic_height = pic_height,
                bitMapData = bitMapData,
                saveType = saveType
            };
            return HttpPostRequst(url, JsonConvert.SerializeObject(requstData), "");
        }

2.2 webapi(附上整个代码)

  [FromBody]
    /// 
    /// 图片上传
    /// 
    public class ImageSaveController : ApiController
    {
        /// 
        /// 保存图片
        /// 
        //public class SaveImage
        //{
        private object _lock = new object();
        /// 
        /// 保存学习网图片
        /// 
        /// 
        /// 
        /// 

        [HttpPost]
        //public string SaveLearningImage(int pic_width, int pic_height, dynamic  string bitMapData, int saveType = 4)
        public string SaveLearningImage([FromBody] Object  bit)
    
        {
            lock (_lock)
            {
                //JavaScriptSerializer jss = new JavaScriptSerializer();
                //string myJson = jss.Serialize(bitMapData);
                //return myJson;
                string jsonstr = JsonConvert.SerializeObject(bit);
                Data res = JsonConvert.DeserializeObject(jsonstr);
                //Data res = new Data
                //{
                //    BitMapData = ((Newtonsoft.Json.Linq.JContainer)(bit)).First
                //};
                return SavePhoto(res.pic_width, res.pic_height, res.BitMapData, res.saveType);
            }
        }
        private class Data {
          public string BitMapData { get; set; }
          public int pic_width { get; set; }
          public int pic_height { get; set; }
         public int saveType { get; set; }

        }
        private string SavePhoto(int pic_width, int pic_height, string bitMapData, int saveType)
        {
            byte[] bitmap_data = Convert.FromBase64String(bitMapData);
            int PhotoD = 170;
            Stream stream = new MemoryStream(bitmap_data);
            var m_pic = new Bitmap(stream);
            string Year = System.DateTime.Now.Year.ToString();
            string Month = System.DateTime.Now.Month.ToString();
            string Day = System.DateTime.Now.Day.ToString();
            string Hour = System.DateTime.Now.Hour.ToString();

            string ConfigPath = "", filePath = "";
            switch (saveType)
            {
                case 4:
                    {
                        ConfigPath = ConfigurationManager.AppSettings["LearPicPath"];
                        filePath = "/imagepath/onlinephoto/";
                        break;
                    }
                case 1:
                    {
                        ConfigPath = ConfigurationManager.AppSettings["LearPicPath_Theory"];
                        filePath = "/";
                        break;
                    }
            }

            filePath += Year + "/" + Month + "/" + Day + "/" + Hour + "/";
            //判断路径是否存在,若不存在则创建路径
            DirectoryInfo upDir = new DirectoryInfo(ConfigPath + filePath);
            if (!upDir.Exists)
            {
                upDir.Create();
            }

            //生成随机文件名                
            DateTime date = DateTime.Now;
            string fileName = Guid.NewGuid().ToString() + ".jpg";
            int towidth = pic_width;
            int toheight = pic_height;

            //towidth = PhotoD;
            //toheight = PhotoD;

            int width = pic_width;
            int height = pic_height;

            int x = 0;
            int y = 0;
            int ow = m_pic.Width;
            int oh = m_pic.Height;

            switch (ThumbnailMode.Cut)
            {
                case ThumbnailMode.HW://指定高宽缩放(可能变形)                
                    break;
                case ThumbnailMode.W://指定宽,高按比例                    
                    toheight = m_pic.Height * width / m_pic.Width;
                    break;
                case ThumbnailMode.H://指定高,宽按比例
                    towidth = m_pic.Width * height / m_pic.Height;
                    break;
                case ThumbnailMode.Cut://指定高宽裁减(不变形)                
                    if ((double)m_pic.Width / (double)m_pic.Height > (double)towidth / (double)toheight)
                    {
                        oh = m_pic.Height;
                        ow = m_pic.Height * towidth / toheight;
                        y = 0;
                        x = (m_pic.Width - ow) / 2;
                    }
                    else
                    {
                        ow = m_pic.Width;
                        oh = m_pic.Width * height / towidth;
                        x = 0;
                        y = (m_pic.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }
            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(m_pic, new Rectangle(0, 0, towidth, toheight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);
            string fullPath = ConfigPath + filePath + fileName;
            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(fullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                m_pic.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
            return filePath + fileName;
        }

        /// 
        /// 缩略图格式
        /// 
        public enum ThumbnailMode
        {
            /// 
            /// 指定高宽缩放(可能变形)。 
            /// 
            HW,
            /// 
            /// 指定宽,高按比例。 
            /// 
            W,
            /// 
            /// 指定高,宽按比例。 
            /// 
            H,
            /// 
            /// 指定高宽裁减(不变形)。 
            /// 
            Cut
        }
    }

图片上传的比较简单,base64对图片来说,还是比较方便的,不论是前端还是后端。

你可能感兴趣的:(asp.net学习记录,C#)