如何使用c sharp减小图像的大小 [英] How to reduce the size of an image using c sharp

查看:135
本文介绍了如何使用c sharp减小图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,





我想减小图片尺寸小于50 kb。这里我给出了我在我的应用程序中使用的代码。



 byte [] imageSize = new byte [FileUpload1.PostedFile.ContentLength] ; 
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize(int)FileUpload1.PostedFile.ContentLength);
var fileLength1 =(FileUpload1.FileContent.Length.ToString());
byte [] ImageData = GenerateThumbnails(0.005,uploadedImage.InputStream);
private byte [] GenerateThumbnails(double scaleFactor,Stream sourcePath)
{
var image = System.Drawing.Image.FromStream(sourcePath);
var newWidth =(int)(image.Width * scaleFactor);
var newHeight =(int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth,newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0,0,newWidth,newHeight);
thumbGraph.DrawImage(image,imageRectangle);

MemoryStream ms = new MemoryStream();
image.Save(ms,ImageFormat.Jpeg);
返回ms.ToArray();
}





这里,生成的GenerateThumbnails减少了图片的实际尺寸,但它非常低。假设我上传了800 kb的图像,功能将从800 kb减少仅仅40 kb。图像的大小影响我的网站的性能。所以,如果有人知道答案,请帮助我。



提前致谢。



问候,

Rajeev K.

解决方案

你,GenerateThumbnails方法很糟糕。看看这篇优秀文章:缩略图生成 [ ^ ]



它允许你调整大小和缩放图像,让您可以更好地控制所制作图像的质量。



干杯!

Eduard

Dear All,


I want to reduce the picture size less than 50 kb. Here i am giving the code which i have used in my application.

byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize(int)FileUpload1.PostedFile.ContentLength);
var fileLength1 = (FileUpload1.FileContent.Length.ToString());
byte[] ImageData = GenerateThumbnails(0.005, uploadedImage.InputStream);
private byte[] GenerateThumbnails(double scaleFactor, Stream sourcePath)
{
      var image = System.Drawing.Image.FromStream(sourcePath);
      var newWidth = (int)(image.Width * scaleFactor);
      var newHeight = (int)(image.Height * scaleFactor);
      var thumbnailImg = new Bitmap(newWidth, newHeight);
      var thumbGraph = Graphics.FromImage(thumbnailImg);
      thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
      thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
      thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
      thumbGraph.DrawImage(image, imageRectangle);

      MemoryStream ms = new MemoryStream();
      image.Save(ms, ImageFormat.Jpeg);
      return ms.ToArray();
}



Here,the fumction GenerateThumbnails reduce the actual size of the picture but it is very low.Suppose i upload the image which has 800 kb ,function will reduce only 40 kb from 800 kb.Size of the image affectiong the perfomance of my website.So if anyone know the answer please help me.

Thanks in advance.

Regards,
Rajeev K.

解决方案

You, the GenerateThumbnails method sucks big time. Take a look at this excellent article : Thumbnails Generation[^]

It allows you to resize and scale images and allows you to have more control over the quality of the image being produced.

Cheers!
Eduard


这篇关于如何使用c sharp减小图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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