设为首页 加入收藏

TOP

调用微信JS上传照片接口上传图片(一)
2017-10-10 12:50:59 】 浏览:3456
Tags:调用 上传 照片 接口 图片
public ActionResult UploadImge(string serverId)
        {
            var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
            string AbsoluteFilePath = Server.MapPath(headPath);
            if (!Directory.Exists(AbsoluteFilePath))
            {
                Directory.CreateDirectory(AbsoluteFilePath);
            }
            string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";//原图
            string AbsolutePath = Server.MapPath(imgPath);
            WeChatClient.SaveMultimedia(serverId, AbsolutePath);


            return Json(imgPath);

        }

        public ActionResult UploadImge2(string serverId)
        {
            var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
            string AbsoluteFilePath = Server.MapPath(headPath);
            if (!Directory.Exists(AbsoluteFilePath))
            {
                Directory.CreateDirectory(AbsoluteFilePath);
            }
            string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";
            string AbsolutePath = Server.MapPath(imgPath);

            WeChatClient.SaveMultimedia(serverId, AbsolutePath);

            CommonUtil.MakeThumbnail(AbsolutePath, AbsolutePath.Replace(".jpg", "_2.jpg"),3, 4);//以3:4比例裁剪
            return Json(imgPath);

        }
        public ActionResult UploadImge3(string serverId)
        {
            var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
            string AbsoluteFilePath = Server.MapPath(headPath);
            if (!Directory.Exists(AbsoluteFilePath))
            {
                Directory.CreateDirectory(AbsoluteFilePath);
            }
            string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";
            string AbsolutePath = Server.MapPath(imgPath);

            WeChatClient.SaveMultimedia(serverId, AbsolutePath);

            CommonUtil.MakeThumbnail(AbsolutePath, AbsolutePath.Replace(".jpg", "_3.jpg"), 4, 3);//以4:3比例裁剪图片
            return Json(imgPath);

        }
  public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int bw = 4, int bh = 3)
        {
            try
            {
                Image originalImage = Image.FromFile(originalImagePath);
                var bl = originalImage.Height * 1.00 / originalImage.Width;
                int orgWith, orgHeight, startY = 0, startX = 0, newWith, newHeight = 0;
                var bd = bh * 1.00 / bw;
                if (bl > bd)
                {
                    orgWith = originalImage.Width;
                    orgHeight = (int)(originalImage.Width * bh / bw);
                    startY = (originalImage.Height - orgHeight) / 2;
                    newWith = orgWith;
                    if (newWith > 720)
                    {
                        newWith = 720;
                    }
                    newHeight = (int)(newWith * bh / bw);

                }
                else
                {
                    orgWith = originalImage.Height * bw / bh;
                    orgHeight = originalImage.Height;
                    startX = (originalImage.Width - orgWith) / 2;
                    newWith = orgWith;
                    if (newWith > 720)
                    {
                        newWith = 720;
                    }
                    newHeight = (int)(newWith * bh / bw);
                }


                Bitmap destBitmap = new Bitmap(newWith, newHeight);//目标图
                Rectangle destRect = new Rectangle(0, 0, newWith, newHeight);//矩形容器
                Rectangle srcRect = new Rectangle(startX, startY, orgWith, orgHeight);

                var g = Graphics.FromImage(destBitmap);
                //设置高质量插值法 
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空画布并以透明背景色填充 
                g.Clear(Color.Transparent);

                try
                {
                    g.DrawImage(originalImage, destRect, srcRect, GraphicsUnit.Pixel);
                    destBitmap.Save(thumbnailPath, originalImage.RawFormat);
                }
                ca
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇一个C#操作RabbitMQ的完整例子 下一篇关于.Net WebAPI数据认证(包括登..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目