Browse Source

Improving image output quality

Former-commit-id: 59c72f6a5d834a699776ad7b9df768c4af74ac7f
af/merge-core
James South 12 years ago
parent
commit
dc35100e88
  1. 49
      src/ImageProcessor/Processors/Resize.cs
  2. 1
      src/Images/Thumbs.db.REMOVED.git-id
  3. 3
      src/Images/bus.jpg

49
src/ImageProcessor/Processors/Resize.cs

@ -11,7 +11,6 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using #region Using
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
@ -20,10 +19,8 @@ namespace ImageProcessor.Processors
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ImageProcessor.Extensions; using ImageProcessor.Extensions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
#endregion #endregion
/// <summary> /// <summary>
@ -145,14 +142,14 @@ namespace ImageProcessor.Processors
Size size = this.ParseSize(toParse); Size size = this.ParseSize(toParse);
ResizeLayer resizeLayer = new ResizeLayer(size) ResizeLayer resizeLayer = new ResizeLayer(size)
{ {
ResizeMode = this.ParseMode(toParse), ResizeMode = this.ParseMode(toParse),
AnchorPosition = this.ParsePosition(toParse), AnchorPosition = this.ParsePosition(toParse),
BackgroundColor = this.ParseColor(toParse), BackgroundColor = this.ParseColor(toParse),
Upscale = !UpscaleRegex.IsMatch(toParse), Upscale = !UpscaleRegex.IsMatch(toParse),
}; CenterCoordinates = this.ParseCoordinates(toParse),
};
resizeLayer.CenterCoordinates = this.ParseCoordinates(toParse);
this.DynamicParameter = resizeLayer; this.DynamicParameter = resizeLayer;
return this.SortOrder; return this.SortOrder;
@ -186,7 +183,6 @@ namespace ImageProcessor.Processors
int.TryParse(this.Settings["MaxHeight"], out defaultMaxHeight); int.TryParse(this.Settings["MaxHeight"], out defaultMaxHeight);
List<Size> restrictedSizes = this.ParseRestrictions(restrictions); List<Size> restrictedSizes = this.ParseRestrictions(restrictions);
return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, restrictedSizes, backgroundColor, mode, anchor, upscale, centerCoordinates); return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, restrictedSizes, backgroundColor, mode, anchor, upscale, centerCoordinates);
} }
#endregion #endregion
@ -437,36 +433,25 @@ namespace ImageProcessor.Processors
using (Graphics graphics = Graphics.FromImage(newImage)) using (Graphics graphics = Graphics.FromImage(newImage))
{ {
// We want to use two different blending algorithms for enlargement/shrinking. // We want to use two different blending algorithms for enlargement/shrinking.
// Bicubic is better enlarging for whilst Bilinear is better for shrinking.
// http://www.codinghorror.com/blog/2007/07/better-image-resizing.html
if (image.Width < destinationWidth && image.Height < destinationHeight) if (image.Width < destinationWidth && image.Height < destinationHeight)
{ {
// We are making it larger. // We are making it larger.
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
} }
else else
{ {
// We are making it smaller. // We are making it smaller.
graphics.SmoothingMode = SmoothingMode.None; graphics.SmoothingMode = SmoothingMode.None;
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
} }
// An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// as the algorithm appears to be pulling averaging detail from surrounding pixels beyond the edge graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror graphics.CompositingMode = CompositingMode.SourceCopy;
// images of the pixels within solves this problem. graphics.CompositingQuality = CompositingQuality.HighQuality;
using (ImageAttributes wrapMode = new ImageAttributes())
{ graphics.Clear(backgroundColor);
wrapMode.SetWrapMode(WrapMode.TileFlipXY); Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight);
graphics.Clear(backgroundColor); graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel);
Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight);
graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel, wrapMode);
}
// Reassign the image. // Reassign the image.
image.Dispose(); image.Dispose();
@ -676,4 +661,4 @@ namespace ImageProcessor.Processors
return floats; return floats;
} }
} }
} }

1
src/Images/Thumbs.db.REMOVED.git-id

@ -1 +0,0 @@
4d0266cc69c162b3cb0ea5d5506c82a525eac00f

3
src/Images/bus.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:27c958940a4a328791b39ebb89987d35d898670343332d9376e6da748ee99f33
size 52330
Loading…
Cancel
Save