首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

再100分讨论.net下缩放图片后质量有关问题

2011-12-14 
再100分讨论.net下缩放图片后质量问题http://community.csdn.net/Expert/topic/5400/5400258.xml?temp.13

再100分讨论.net下缩放图片后质量问题
http://community.csdn.net/Expert/topic/5400/5400258.xml?temp=.1330683
前段时间,我发帖请教了.net下缩放图片后质量不好的问题。

非常感谢net_lover(【孟子E章】)   给我一个解决方法是使用Image.GetThumbnailImage方法来实现,的确这样的图像的确很高,几乎可以和ps缩放后生成的最高质量jpg相比。

但是这个方法有一个问题,如果原图中带有一个缩略图信息的话(比如这张图:http://adow.thmz.com/pk/images/k.jpg),GetThumbnailImage就会直接从图像中取得这个缩略图返回,而不是根据整张图片来生成。问题在于,我希望缩放后的图片大小是我定义的,如果一张原图中包括了缩略图被返回的话,就会从一个很小的尺寸(一般图像信息中包括的所录图只有128*96这样级别的)被放大到我指定的尺寸,那样的图片惨不忍睹。对这样的图片,大家看有什么方法解决。

[解决办法]
up
[解决办法]
up
[解决办法]
不是太明白。
[解决办法]
不会
[解决办法]
原图中带有一个缩略图信息??
[解决办法]
http://www.microsoft.com/china/MSDN/library/archives/library/DNAspp/html/colorquant.asp
[解决办法]
楼主看看我写的代码,如果原图小于缩放以后的图的话,是不会执行的
/// <summary>
/// 按比例缩放图片,并非按原来大小裁剪
/// 水平长度优先.
/// </summary>
public static string ResizeImage(string _srcImage, int _DstX, string _dstPath)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(_srcImage);
int newHight = img.Height;
int newWidth = img.Width;
while (newWidth > _DstX)//按比例缩小图片
{
newHight = Convert.ToInt32(newHight / 1.01);
newWidth = Convert.ToInt32(newWidth / 1.01);
}
System.Drawing.Image newimg = img.GetThumbnailImage(newWidth, newHight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbCallBack), IntPtr.Zero);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newimg);
string tempName=GenRandomNumber(22) + ".jpg ";
bmp.Save(_dstPath + tempName , System.Drawing.Imaging.ImageFormat.Jpeg);
return tempName;
}

/// <summary>
/// 按比例缩放图片,并非按原来大小裁剪
/// 垂直长度优先
/// </summary>
public static string ResizeImage(int _DstY, string _srcImage, string _dstPath)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(_srcImage);
int newHight = img.Height;
int newWidth = img.Width;
while (newWidth > _DstY)//按比例缩小图片
{
newHight = Convert.ToInt32(newHight / 1.01);
newWidth = Convert.ToInt32(newWidth / 1.01);
}
System.Drawing.Image newimg = img.GetThumbnailImage(newWidth, newHight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbCallBack), IntPtr.Zero);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newimg);
string tempName = GenRandomNumber(22) + ".jpg ";
bmp.Save(_dstPath + tempName, System.Drawing.Imaging.ImageFormat.Jpeg);
return tempName;
}
public static bool ThumbCallBack()
{
return true;
}
public static string GenRandomNumber(int _seed)
{
Random rdm = new Random(_seed);
return System.DateTime.Now.Millisecond.ToString() + rdm.Next(1, 65536).ToString();
}
------解决方案--------------------


protected void btnUp_Click(object sender, EventArgs e)
{
string mPath;
if (upImage.PostedFile.ContentLength > 4096000)//判断是否大于4M
{

Response.Write( " <Script language = JavaScript> alert( '大! ') </Script> ");
}
else
{
if ( " " != upImage.PostedFile.FileName)
{
string imagePath = upImage.PostedFile.FileName;
string imageType = imagePath.Substring(imagePath.LastIndexOf( ". ") + 1);
string imageName = imagePath.Substring(imagePath.LastIndexOf( "\\ ") + 1);
if ( "jpg " != imageType && "gif " != imageType && "GIF " != imageType && "JPG " != imageType)
{
Response.Write( " <script language= 'javascript '> alert( '请选择jpg或者gif格式! '); </script> ");
return;
}
else
{
try
{
mPath = Server.MapPath( "upfile ");
upImage.PostedFile.SaveAs(mPath + "\\ " + imageName);
imageSource.ImageUrl = "upfile/ " + imageName;
System.Drawing.Image image = System.Drawing.Image.FromFile(mPath + "\\ " + imageName);

System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);


System.Drawing.Image newimage = image.GetThumbnailImage(50, 50, myCallback, IntPtr.Zero);

newimage.Save(Server.MapPath( "upfile ") + "\\small " + imageName);
image.Dispose();
newimage.Dispose();
imageSmall.ImageUrl = "upfile/ " + "small " + imageName;
Response.Write( "成功 ");
}
catch
{
Response.Write( "失败 ");
}
}
}
}
}


image.GetThumbnailImage(50, 50, myCallback, IntPtr.Zero);
这里的数字不就是控制大小吗?

热点排行