在ASP.NET中缩放/调整大小/重新采样图像

本文讨论了一位开发者在C#中使用缩略图功能时遇到的图像质量问题,提出通过调整SmoothingMode和InterpolationMode来改善重采样效果,但未找到明显改进。寻求可能的解决方案和相关技术优化建议。
摘要由CSDN通过智能技术生成

A friend was an image in C# yesterday, and was commenting on how crappy the output looked.  His code was very similar to the code in dasBlog that resizes an image when an entry is email-to-blog'ed.

一位朋友昨天在C#中是一张图片,并在评论输出看起来多么糟糕。 他的代码与dasBlog中的代码非常相似,后者在条目通过电子邮件发送到博客时会调整图像的大小。

/// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
/// <summary>
/// This function is used for thumbnailing and gets an image encoder
/// for a given mime type, such as image/jpeg
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == mimeType)
{
return codec;
}
}
return null;
}

///版权所有(c)2003,newtelligence AG。 ( http://www.newtelligence.com )
/// <摘要>
///此功能用于缩略图并获取图像编码器
///对于给定的mime类型,例如image / jpeg
/// </ summary>
/// <param name =“ mimeType”> </ param>
/// <returns> </ returns>
私有ImageCodecInfo GetEncoderInfo(字符串mimeType)
{
ImageCodecInfo []编解码器= ImageCodecInfo.GetImageEncoders();
foreach (编解码器中的ImageCodecInfo编解码器)
{
如果(codec.MimeType == mimeType)
{
返回编解码器;
}
}
返回null ;
}

<SNIPPET>

<片段>

string absoluteFileName = Path.Combine(binariesPath, fileName);
string thumbBaseFileName = Path.GetFileNameWithoutExtension(fileName)+"-thumb.dasblog.JPG";
string thumbFileName = Path.Combine(binariesPath, thumbBaseFileName);
Bitmap sourceBmp = new Bitmap(absoluteFileName);
if ( sourceBmp.Height > siteConfig.Pop3InlinedAttachedPicturesThumbHeight )
{
Bitmap targetBmp = new Bitmap(sourceBmp,new Size(
Convert.ToInt32(Math.Round((((double)sourceBmp.Width) * (((double)siteConfig.Pop3InlinedAttachedPicturesThumbHeight) / ((double)sourceBmp.Height))),0)),
siteConfig.Pop3InlinedAttachedPicturesThumbHeight));
ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
Encoder encoder = Encoder.Quality;
EncoderParameters encoderParams= new EncoderParameters(1);
long compression=75;
EncoderParameter encoderParam = new EncoderParameter(encoder,compression);
encoderParams.Param[0] = encoderParam;
targetBmp.Save(thumbFileName,codecInfo,encoderParams);
string absoluteUri = new Uri( binariesBaseUri, fileName ).AbsoluteUri;
string absoluteThumbUri = new Uri( binariesBaseUri, thumbBaseFileName ).AbsoluteUri;
entry.Content += String.Format("<div class=\"inlinedMailPictureBox\"> <a href=\"{0}\"><img border=\"0\" class=\"inlinedMailPicture\" src=\"{2}\"></a> <br> <a class=\"inlinedMailPictureLink\" href=\"{0}\">{1}</a></div>",absoluteUri, fileName, absoluteThumbUri);
scalingSucceeded = true;

字符串absoluteFileName = Path.Combine(binariesPath,fileName);
字符串thumbBaseFileName = Path.GetFileNameWithoutExtension(fileName) + “ -thumb.dasblog.JPG” ;
字符串thumbFileName = Path.Combine(binariesPath,thumbBaseFileName);
位图sourceBmp =新的位图(absoluteFileName);
如果(sourceBmp.Height> siteConfig.Pop3InlinedAttachedPicturesThumbHeight)
{
位图targetBmp =新位图(sourceBmp,新大小(
Convert.ToInt32(Math.Round((((( double )sourceBmp.Width) * (((( double )siteConfig.Pop3InlinedAttachedPicturesThumbHeight) / ((( double )sourceBmp.Height))),0)),)
siteConfig.Pop3InlinedAttachedPicturesThumbHeight));
ImageCodecInfo codecInfo = GetEncoderInfo( “ image / jpeg” );
编码器编码器= Encoder.Quality;
EncoderParameters encoderParams =新的EncoderParameters(1);
长压缩= 75;
EncoderParameter encoderParam =新的EncoderParameter(encoder,compression);
encoderParams.Param [0] = encoderParam;
targetBmp.Save(thumbFileName,codecInfo,encoderParams);
字符串absoluteUri =新的Uri(binariesBaseUri,fileName).AbsoluteUri;
字符串absoluteThumbUri =新的Uri(binariesBaseUri,thumbBaseFileName).AbsoluteUri;
entry.Content + = String.Format( “ <div class = \” inlinedMailPictureBox \“> <a href=\"{0}\"> <img border = \” 0 \“ class =” \ inlinedMailPicture \“ src = \“ {2} \”> </a> <br> <a class=\"inlinedMailPictureLink\" href=\"{0}\"> {1} </a> </ div>“ ,absoluteUri,fileName ,absoluteThumbUri);
scaleSucceeded = true ;

</SNIPPET>

</ SNIPPET>

This code (above) works fine for extracting an image from an email and scaling (thumbnailing) it, but the result is fairly jaggy, and one would certainly expect better resampling than is seen in the image above.

上面的代码对于从电子邮件中提取图像并缩放(缩略图显示)效果很好,但是结果是比较麻烦的,当然可以期望获得比上图中更好的重采样效果。

I found this article on DevEx that seemed to offer these lines as a possible solution:

在DevEx上发现了这篇文章,似乎提供了以下行作为可能的解决方案:

    g.SmoothingMode =SmoothingMode.HighQuality;
    g.InterpolationMode =InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode =PixelOffsetMode.HighQuality;

g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

But there doesn't appear to be any visible change (to my eye) with these added.  Should the SmoothingMode be set to 'SmoothingModeAntiAlias' instead?  Smooth resampling of an image in C# certainly seems it should be a 'solved problem.'  Perhaps I'm not Googling well.

但是,添加这些似乎没有任何可见的变化(在我看来)。 应该将SmoothingMode设置为“ SmoothingModeAntiAlias”吗? 在C#中对图像进行平滑的重采样显然似乎应该是“已解决的问题”。 也许我不太喜欢Google。

翻译自: https://www.hanselman.com/blog/scalingresizingresampling-an-image-in-aspnet

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值