Pete,
There are a number of threads in this newsgroup that cover that topic.
Please feel free to use Google Groups or another archive to search for
them. If you do the search correctly, you will probably even find the
article that Jeff Johnson posted in this very message thread in which he
included a brief summary of the technique. Once you've seen the general
technique to apply, if you still have specific questions, please do feel
free to ask for help.
I performed searches in google and tried code samples shown in comments.
All those methods produce non-sharp thumbnails compared to thumbnail created
in silverlight client.
How to create high-quality small-size thumbails in server?
Andrus.
Byte[] MakeMeAGoodThumbnail(Byte[] source, int imageHeight, int imageWidth)
{
Bitmap bmp = new Bitmap(new MemoryStream(source));
int width, height;
if (bmp.Height > imageHeight || bmp.Width > imageWidth)
{
float percent = DeterminePercentageForResize(bmp.Height,
bmp.Width);
float floatWidth = (float)bmp.Width * percent;
float floatHeight = (float)bmp.Height * percent;
width = Convert.ToInt32(floatWidth);
height = Convert.ToInt32(floatHeight);
}
else
{
width = bmp.Width;
height = bmp.Height;
}
var ms = new MemoryStream();
//
http://www.west-wind.com/Weblog/posts/283.aspx comment
var resized = BetterThumbnail(bmp, width, height);
#if false
//
http://www.west-wind.com/Weblog/posts/283.aspx
var resized = new Bitmap(width, height);
Graphics g = Graphics.FromImage(resized);
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, width, height);
g.DrawImage(bmp, 0, 0, width, height);
bmp.Dispose();
g.Dispose();
#endif
#if false
//
http://www.bobpowell.net/changing_resolution.htm
Bitmap resized = new Bitmap(width, height);
Graphics g = Graphics.FromImage(resized);
g.DrawImage(bmp, new Rectangle(0, 0, resized.Width,
resized.Height), 0, 0,
bmp.Width,
bmp.Height, GraphicsUnit.Pixel);
g.Dispose();
#endif
#if false
// digital photos contain built-in thumbnails. This function
resizes them and produces low
// quality thumbnail:
Image resized = bmp.GetThumbnailImage(width, height,
() => false, IntPtr.Zero);
#endif
resized.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
public static Bitmap BetterThumbnail(Bitmap inputImage, int width, int
height)
{
Bitmap outputImage = new Bitmap(width, height,
PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(outputImage);
g.CompositingMode = CompositingMode.SourceCopy;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle destRect = new Rectangle(0, 0, width, height);
g.DrawImage(inputImage, destRect, 0, 0, inputImage.Width,
inputImage.Height, GraphicsUnit.Pixel);
g.Dispose();
return outputImage;
}
float DeterminePercentageForResize(int height, int width)
{
int highestValue;
if (height > width)
highestValue = height;
else
highestValue = width;
float percent = 100 / (float)highestValue;
if (percent > 1 && percent != 0)
throw new Exception("Percent cannot be greater than 1 or
equal to zero");
else
return percent;
}