将图像文件大小减小到150 KB [英] Reduce Image File Size to 150 KB
本文介绍了将图像文件大小减小到150 KB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友
我正在开发一个图像调整大小项目
我的客户希望图像大小应该是240 * 320和大小应该是50-150 kb
但尺寸不受控制尺寸超过150 kb,图像超过1 MB
我的代码是:
protected void uploadButton_Click( object sender,EventArgs e)
{
Bitmap bmp1 = new 位图(fileUpload.FileContent);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// 根据GUID创建一个编码器对象
质量参数类别的 // 。
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// 创建一个EncoderParameters对象。
// EncoderParameters对象有一个EncoderParameter数组
// 对象。在这种情况下,数组中只有一个
// EncoderParameter对象。
EncoderParameters myEncoderParameters = new EncoderParameters( 1 );
// EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,50L);
// myEncoderParameters.Param [0] = myEncoderParameter;
// bmp1.Save(@c:\ TestPhotoQualityFifty.jpg,jgpEncoder,myEncoderParameters);
// myEncoderParameter = new EncoderParameter(myEncoder,100L);
// myEncoderParameters.Param [0] = myEncoderParameter;
// bmp1.Save(@c:\ TestPhotoQualityHundred.jpg,jgpEncoder,myEncoderParameters);
< span class =code-comment> // Sav将位图作为JPG文件,质量级压缩为零。
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,25L);
myEncoderParameters.Param [ 0 ] = myEncoderParameter;
bmp1.Save( @ c:\ TestPhotoQualityZero.jpg,jgpEncoder,myEncoderParameters );
if (fileUpload.HasFile)
{
// 查找fileUpload控件
string filename = fileUpload.FileName;
// 检查我们希望上传图像的目录是否确实存在
if (!Directory.Exists(MapPath( @ Uploaded-Files)))
{
// 如果没有,那么我们只是在继续之前创建它
Directory.CreateDirectory(MapPath( @ 上传的档案 跨度>));
}
// 指定上传目录
string directory = Server.MapPath( @ 已上传 - Files\跨度>);
// 在内存中创建fileUpload控件内容的位图
Bitmap originalBMP = new 位图( @ C:\TestPhotoQualityZero.jpg跨度>);
// 计算新图片尺寸
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 240 ;
// int newHeight = newWidth / sngRatio;
int newHeight = 320 ;
// 创建一个新的位图,该位图将保存先前调整大小的位图
位图newBMP = 新位图(originalBMP,newWidth,newHeight);
// 基于新位图创建图形
图形oGraphics = Graphics .FromImage(newBMP);
// 设置新图形文件的属性
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 根据调整后的位图绘制新图形
oGraphics.DrawImage (originalBMP, 0 , 0 ,newWidth,newHeight);
// 将新图形文件保存到服务器
newBMP.Save(目录+ tn _ + filename);
// 完成位图对象后,我们将其解除分配。
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
// 写一条消息告知用户一切正常
label.Text = 文件名:颜色:红色 > + filename + 的<峰; br> 中跨度>;
label.Text + = 内容类型:颜色:红色< span class =code-string> > + fileUpload.PostedFile.ContentType + < br>;
label.Text + = 文件大小:颜色:红色< span class =code-string> > + fileUpload.PostedFile.ContentLength.ToString()+ ;
// 向用户显示图像
Image1.Visible = 真跨度>;
Image1.ImageUrl = @ / Uploaded-Files / tn _ + filename;
}
else
{
label.Text = 没有上传文件!;
}
}
私人 ImageCodecInfo GetEncoder(ImageFormat格式)
{
ImageCodecInfo [] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return 编解码器;
}
}
返回 null ;
}
解决方案
请使用 SearchBox [ ^ ](73页!)。
Hi friends
I am developing a Image resize project
my client wants the image size should be in 240*320 and size should be 50-150 kb
but the size is not in control the size exist more than 150 kb for the image more than 1 MB
my code is:
protected void uploadButton_Click(object sender, EventArgs e)
{
Bitmap bmp1 = new Bitmap(fileUpload.FileContent);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
//EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);
//myEncoderParameter = new EncoderParameter(myEncoder, 100L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);
// Save the bitmap as a JPG file with zero quality level compression.
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);
if (fileUpload.HasFile)
{
// Find the fileUpload control
string filename = fileUpload.FileName;
// Check if the directory we want the image uploaded to actually exists or not
if (!Directory.Exists(MapPath(@"Uploaded-Files")))
{
// If it doesn't then we just create it before going any further
Directory.CreateDirectory(MapPath(@"Uploaded-Files"));
}
// Specify the upload directory
string directory = Server.MapPath(@"Uploaded-Files\");
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(@"c:\TestPhotoQualityZero.jpg");
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 240;
//int newHeight = newWidth / sngRatio;
int newHeight = 320;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(directory + "tn_" + filename);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
// Write a message to inform the user all is OK
label.Text = "File Name: ">" + filename + "<br>";
label.Text += "Content Type: ">" + fileUpload.PostedFile.ContentType + "<br>";
label.Text += "File Size: ">" + fileUpload.PostedFile.ContentLength.ToString() + "";
// Display the image to the user
Image1.Visible = true;
Image1.ImageUrl = @"/Uploaded-Files/tn_" + filename;
}
else
{
label.Text = "No file uploaded!";
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
解决方案
Please, use SearchBox[^] (73 pages!).
这篇关于将图像文件大小减小到150 KB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文