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