码迷,mamicode.com
首页 > Windows程序 > 详细

(c#.net)前端js图片压缩将得到的base64字符串保存为图片

时间:2016-03-18 10:48:02      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
<script src="jquery-1.7.2.min.js"></script>
<body>
    <input type="file" id="img" name="img" />
    <img id="test" src="" />
    <input type="button" id="submit" value="确定" onclick="submit()" />
    <script>
            
        function submit() {
            var f = document.getElementById("img").files[0];
            var reader = new FileReader();

            // Closure to capture the file information.
            reader.onload = (function (theFile) {
                return function (e) {
                    var i = document.getElementById("test");
                    i.src = event.target.result;
                    var data = compress(i, 50);
                    var fd = new FormData();
                    fd.append("img", data);
                    $.ajax({
                        url: "webform1.aspx?type=upload",
                        type: "POST",
                        data: fd,
                        processData: false,  // 告诉jQuery不要去处理发送的数据
                        contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
                        success: function (data) {
                            $(".loadingbox").hide();
                            if (data == 0)
                                myalert("提示", "保存失败");
                            else
                                myalerttoone("提示", "保存成功", "share.html");
                        }
                    });
                };
            })(f);
            reader.readAsDataURL(f);
        }

        function compress(source_img_obj, quality, output_format){

            var mime_type = "image/jpeg";
            if(output_format!=undefined && output_format=="png"){
                mime_type = "image/png";
            }


            var cvs = document.createElement(canvas);
            //naturalWidth真实图片的宽度
            cvs.width = source_img_obj.naturalWidth;
            cvs.height = source_img_obj.naturalHeight;
            var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
            return cvs.toDataURL(mime_type, quality/100);
        }
    </script>
</body>
前端代码
技术分享
protected void Page_Load(object sender, EventArgs e)
        {
            Base64StringToImage(Request.Form["img"]);
        }

        private void Base64StringToImage(string inputStr)
        {
            if (string.IsNullOrWhiteSpace(inputStr))
                return;
            try
            {
                string filePath = "E:\\bb.jpg";
                byte[] arr = Convert.FromBase64String(inputStr.Substring(inputStr.IndexOf("base64,") + 7).Trim(\0));
                using (MemoryStream ms = new MemoryStream(arr))
                {
                    Bitmap bmp = new Bitmap(ms);
                    //新建第二个bitmap类型的bmp2变量。
                    Bitmap bmp2 = new Bitmap(bmp, bmp.Width, bmp.Height);
                    //将第一个bmp拷贝到bmp2中
                    Graphics draw = Graphics.FromImage(bmp2);
                    draw.DrawImage(bmp, 0, 0);
                    draw.Dispose();
                    bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    ms.Close();
                }
               
            }
            catch (Exception ex)
            {
              
            }
        }
后端代码

 

(c#.net)前端js图片压缩将得到的base64字符串保存为图片

标签:

原文地址:http://www.cnblogs.com/zcjiang/p/5291080.html

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