如何在不丢失质量的情况下裁剪,压缩jquery,MVC中的图像 [英] How to crop, compress image in jquery, MVC without losing the quality

查看:70
本文介绍了如何在不丢失质量的情况下裁剪,压缩jquery,MVC中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理mvc应用程序,我正在上传图像,首先裁剪图像然后在服务器上上传图像。当我裁剪图像并在后面的代码中传递图像时,图像大小会增加。例。如果我选择50KB图像并上传图像,它的大小会增加1MB到1MB以上。



我尝试了什么:



1)

i working on mvc application, in which i am uploading an image, first croping the image and then upload the image on server. when i crop the image and pass the image in code behind, the image size getting increase in size. example. if i am select the 50KB image and upload the image the it's size getting increase 1MB to more than 1MB.

What I have tried:

1)

public JsonResult UploadEventImage(string uploadedFile)

{

Bitmap bmp= null;

 

                byte[] byteBuffer = Convert.FromBase64String(uploadedFile.Replace("data:image/png;base64,", ""));

                MemoryStream memoryStream = new MemoryStream(byteBuffer);

 

                memoryStream.Position = 0;


                bmp= (Bitmap)Bitmap.FromStream(memoryStream);

                memoryStream.Close();

                memoryStream = null;

                byteBuffer = null;

 

                if (bmp!= null)

                {
                    string fileName = "image1.jpg";

                    fileName = "~/Content/img/" + fileName;
                    bmp.Save(Server.MapPath(fileName), System.Drawing.Imaging.ImageFormat.Bmp);

                    }

}

2)  public JsonResult UploadEventImage(string uploadedFile){ <pre>Bitmap bmp= null;

 

                byte[] byteBuffer = Convert.FromBase64String(uploadedFile.Replace("data:image/png;base64,", ""));

                MemoryStream memoryStream = new MemoryStream(byteBuffer);

 

                memoryStream.Position = 0;

 

                bmp= (Bitmap)Bitmap.FromStream(memoryStream);

 

 

                Image img = bmp;

                Bitmap resizedImg = new Bitmap(img.Width, img.Height);

 

                double ratioX = (double)resizedImg.Width / (double)img.Width;

                double ratioY = (double)resizedImg.Height / (double)img.Height;

                double ratio = ratioX < ratioY ? ratioX : ratioY;

 

                int newHeight = Convert.ToInt32(img.Height * ratio);

                int newWidth = Convert.ToInt32(img.Width * ratio);

 

                using (Graphics g = Graphics.FromImage(resizedImg))

                {

                    g.DrawImage(img, 0, 0, newWidth, newHeight);

                }
 if (bmp!= null)

                {

                    string fileName = "image1.jpg";

                    fileName = "~/Content/img/" + fileName;

                   

                    resizedImg.Save(Server.MapPath(fileName));
}
}

推荐答案

首先要注意的是你使用了完全错误的文件格式: JPG是一种有损压缩,每次将文件保存为JPG时,您总是会失去质量。保存太频繁,图像将变得无法识别!但很小。非常小。



考虑使用BMP,GIF或PNG - BMP未压缩,但GIF和PNG使用无损压缩算法。
The first thing to note is that you are using totally the wrong file format: JPG is a lossy compression, and every time you save a file as JPG you are always going to lose quality. Save too often, and the image will become unrecognisable! But small. Very small.

Consider using BMP, GIF, or PNG - BMP is not compressed, but GIF and PNG use a lossless compression algorithm.

这篇关于如何在不丢失质量的情况下裁剪,压缩jquery,MVC中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆