如何减少图像文件大小? [英] How to reduce image file size?

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

问题描述

在从客户端上传文件后,我是否可以调用任何减少图像文件大小的例程。我希望在上传后使用C#以编程方式减少文件大小

。我知道

Photoshop可以做到但我需要在我的网页中动态完成。我尝试用自己的一些Bitmap缩略图说明来做这件事,但是

产生的图像非常糟糕。我需要一个文件缩减程序,它可以创建一个更小的优质图像。我想如果Photoshop可以做到那么我可以这样吗b / b但是如何?

Is there any routine I can call to reduce the size of an image file after
uploading a file from a client. I am looking to reduce file sizes
programmatically using C# in my web page after uploading. I know that
Photoshop can do it but I need to do it dynamically in my web page. I tried
doing it myself with some of the Bitmap thumbnail instructions but the
resulting image is very bad. I need a file reduction routine that creates a
good image that is smaller. I figured if Photoshop can do it then so can I
but how?

推荐答案

查看Bitmap.GetThumbnailImage


-Brock
http://工作人员。 develop.com/ballen

Check out Bitmap.GetThumbnailImage

-Brock
http://staff.develop.com/ballen

在上传文件后,我是否可以调用任何减少图像文件大小的例程?客户。我希望在上传后使用C#在我的网页中以编程方式减少文件大小。我知道Photoshop可以做到但我需要在我的网页上动态地完成它。我尝试用一​​些Bitmap缩略图指令自己做,但结果图像非常糟糕。我需要一个文件减少例程来创建一个更小的好图像。我想
如果Photoshop可以做到那么我可以,但是如何?
Is there any routine I can call to reduce the size of an image file
after uploading a file from a client. I am looking to reduce file
sizes programmatically using C# in my web page after uploading. I know
that Photoshop can do it but I need to do it dynamically in my web
page. I tried doing it myself with some of the Bitmap thumbnail
instructions but the resulting image is very bad. I need a file
reduction routine that creates a good image that is smaller. I figured
if Photoshop can do it then so can I but how?



Brock;

感谢你答复。我尝试了Get ThumbnailImage函数,但

结果图像是模糊的。我将1360 X 2048图像缩小为272 X 408,并且

边缘模糊不清。我需要一些能让我获得更好分辨率的东西。


" Brock Allen"写道:
Brock;
Thanks for yur reply. I tried the Get ThumbnailImage function but the
resulting image is fuzzy. I reduced a 1360 X 2048 image to 272 X 408 and the
edges are blurry. I need something that gives me a better resolution.

"Brock Allen" wrote:
查看Bitmap.GetThumbnailImage

-Brock
http://staff.develop.com/ballen

Check out Bitmap.GetThumbnailImage

-Brock
http://staff.develop.com/ballen

我可以打电话到从客户端上传文件后,减小图像文件的大小。我希望在上传后使用C#在我的网页中以编程方式减少文件大小。我知道Photoshop可以做到但我需要在我的网页上动态地完成它。我尝试用一​​些Bitmap缩略图指令自己做,但结果图像非常糟糕。我需要一个文件减少例程来创建一个更小的好图像。我想
如果Photoshop可以做到那么我可以,但是如何?
Is there any routine I can call to reduce the size of an image file
after uploading a file from a client. I am looking to reduce file
sizes programmatically using C# in my web page after uploading. I know
that Photoshop can do it but I need to do it dynamically in my web
page. I tried doing it myself with some of the Bitmap thumbnail
instructions but the resulting image is very bad. I need a file
reduction routine that creates a good image that is smaller. I figured
if Photoshop can do it then so can I but how?






鹦鹉 <帕**** @ discussions.microsoft.com>在消息中写道

news:53 ********************************** @ microsof t.com ...

"Parrot" <Pa****@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
Brock;
感谢您的回复。我尝试了Get ThumbnailImage函数,但得到的图像是模糊的。我将1360 X 2048图像缩小为272 X 408并且
边缘模糊。我需要一些能让我获得更好分辨率的东西。

Brock Allen写道:
Brock;
Thanks for yur reply. I tried the Get ThumbnailImage function but the
resulting image is fuzzy. I reduced a 1360 X 2048 image to 272 X 408 and
the
edges are blurry. I need something that gives me a better resolution.

"Brock Allen" wrote:
查看Bitmap.GetThumbnailImage
Check out Bitmap.GetThumbnailImage




我使用GetThumbnailImage的第一个尝试非常难看,但是这个制作图片

看起来很好:

-Fred

使用系统;


使用系统.Data;


使用System.Configuration;


使用System.Web;


使用System.Web.Security;


使用System.Web.UI;


使用System.Web.UI.WebControls;


使用System.Web.UI.WebControls.WebParts;


使用System.Web.UI.HtmlControls;


使用System.IO;


使用System.Drawing;


///< summary>

/// makeThumbnail的摘要说明


///< / summary>


公共类makeThumbnail

{


public makeThumbnail(string parmFileIn,out int rc)


{


rc = 0;


string filePathSourceFile = HttpContext.Current.Server.MapPath("〜/ images / ");


string filePathDestFile =

HttpContext.Current.Server.MapPath("〜/ thumbnails /");
< br $>
string fullSourceFile = filePathSourceFile + parmFileIn;


//字符串currentImageFile;


//字符串thumbnailFile;


int thumbnailHeight;


int thumbnailWidth;


int maxThumbSize = 150;


// int minThumbSize = 120; // 4 x 6比率


double hx; double wx;


//使用我们刚检索的文件名创建一个图像对象


System.Drawing.Image sourceImage =

System.Drawing.Image.FromFile(fullSourceFile);


hx = sourceImage.Height;


wx = sourceImage。宽度;


//将缩略图宽度和高度计算:-scale到最大值


if(sourceImage.Width> sourceImage.Height )// landscape:宽度固定,

比例高度:


{


thumbnailWidth = maxThumbSize;


// thumbnailHeight = minThumbSize;


thumbnailHeight = Convert.ToInt32((hx / wx)* thumbnailWidth);


}


其他//肖像:高度固定为最大,比例宽度:


{


thumbnailHeight = maxThumbSize;


thumbnailWidth = Convert.ToInt32((wx / hx)* thumbnailHeight);


}


System.Drawing.Image缩略图Image =

sourceImage.GetThumbnailImage(thumbnailWidth,thumbnailHeight,new

System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),

IntPtr.Zero );

//使内存流处理图像字节


MemoryStream imageStream = new MemoryStream();


//将图像放入内存流中

thumbnailImage.Save(imageStream,System.Drawing.Imaging.ImageFormat.Jpeg);


string outfile = filePathDestFile + parmFileIn;


试试


{


thumbnailImage 。保存(outfile);


rc = 77;


}


catch(异常) e)


{


rc = 27;


string dummy = e.ToString( );


返回;


}


}


///< summary>


///< / summary>


///< returns>真LT; /回报& gt;


public bool ThumbnailCallback()


{


返回true;


}

}



My first trys using GetThumbnailImage were very ugly, but this makes images
that look just fine:
-Fred
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Drawing;

/// <summary>

/// Summary description for makeThumbnail

/// </summary>

public class makeThumbnail

{

public makeThumbnail(string parmFileIn, out int rc)

{

rc = 0;

string filePathSourceFile = HttpContext.Current.Server.MapPath("~/images/");

string filePathDestFile =
HttpContext.Current.Server.MapPath("~/thumbnails/");

string fullSourceFile = filePathSourceFile + parmFileIn;

//string currentImageFile;

//string thumbnailFile;

int thumbnailHeight;

int thumbnailWidth;

int maxThumbSize = 150;

//int minThumbSize = 120; // 4 x 6 ratio

double hx; double wx;

// create an image object, using the filename we just retrieved

System.Drawing.Image sourceImage =
System.Drawing.Image.FromFile(fullSourceFile);

hx = sourceImage.Height;

wx = sourceImage.Width;

// calc the thumbnail Width and Height: -scale to max with

if (sourceImage.Width > sourceImage.Height) // landscape: width is fixed,
scale height:

{

thumbnailWidth = maxThumbSize;

//thumbnailHeight = minThumbSize;

thumbnailHeight = Convert.ToInt32((hx / wx) * thumbnailWidth);

}

else // portrait: height is fixed at max, scale width:

{

thumbnailHeight = maxThumbSize;

thumbnailWidth = Convert.ToInt32((wx / hx) * thumbnailHeight);

}

System.Drawing.Image thumbnailImage =
sourceImage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);
// make a memory stream to work with the image bytes

MemoryStream imageStream = new MemoryStream();

// put the image into the memory stream

thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

string outfile = filePathDestFile + parmFileIn;

try

{

thumbnailImage.Save(outfile);

rc = 77;

}

catch (Exception e)

{

rc = 27;

string dummy = e.ToString();

return;

}

}

/// <summary>

/// </summary>

/// <returns>true</returns>

public bool ThumbnailCallback()

{

return true;

}
}


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

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