Frågan#1
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public static void GenerateThumbNail(Bitmap bitmap, ImageFormat imageFormat, int width, int height, string destinationPath, bool framed)
{
// generate a white empty area.
if (framed)
{
using (System.Drawing.Image oThumbNail = new Bitmap(bitmap, width, height))
{
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
oGraphic.FillRectangle(new SolidBrush(Color.White), -2, -2, oThumbNail.Size.Width+2, oThumbNail.Size.Height+2);
Size newSize = GetCalculatedSize(bitmap.Size, width, height);
int x = (newSize.Width < oThumbNail.Size.Width) ? ((oThumbNail.Size.Width - newSize.Width) / 2) : 0;
int y = (newSize.Height < oThumbNail.Size.Height) ? ((oThumbNail.Size.Height - newSize.Height) / 2) : 0;
Rectangle oRectangle = new Rectangle(x, y, newSize.Width, newSize.Height);
oGraphic.DrawImage(bitmap, oRectangle);
oThumbNail.Save(destinationPath, imageFormat);
}
}
else
{
Size newSize = GetCalculatedSize(bitmap.Size, width, height);
using (System.Drawing.Image oThumbNail = new Bitmap(bitmap, newSize.Width, newSize.Height))
{
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
Rectangle oRectangle = new Rectangle(0, 0, newSize.Width, newSize.Height);
oGraphic.DrawImage(bitmap, oRectangle);
oThumbNail.Save(destinationPath, imageFormat);
}
}
}
public static void ResizeImage(Bitmap bitmap, ImageFormat imageFormat, int width, int height, string destinationPath)
{
// generate a white empty area.
using (System.Drawing.Image oThumbNail = new Bitmap(bitmap, width, height))
{
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
oGraphic.FillRectangle(new SolidBrush(Color.White), -2, -2, oThumbNail.Size.Width+2, oThumbNail.Size.Height+2);
Size newSize = GetCalculatedSize(bitmap.Size, width, height);
int x = (newSize.Width < oThumbNail.Size.Width) ? ((oThumbNail.Size.Width - newSize.Width) / 2) : 0;
int y = (newSize.Height < oThumbNail.Size.Height) ? ((oThumbNail.Size.Height - newSize.Height) / 2) : 0;
Rectangle oRectangle = new Rectangle(x, y, newSize.Width, newSize.Height);
oGraphic.DrawImage(bitmap, oRectangle);
oThumbNail.Save(destinationPath, imageFormat);
}
}
public static Size GetCalculatedSize(Size originalSize, int maxWidth, int maxHeight)
{
Size newSize = new Size(1,1);
float thumbProps = (float)maxWidth / (float)maxHeight;
float imgProps = (float)originalSize.Width / (float)originalSize.Height;
if (imgProps > thumbProps)
{
// width is max
newSize.Width = maxWidth;
newSize.Height = System.Convert.ToInt32(maxWidth / imgProps);
}
else
{
// height is max
newSize.Height = maxHeight;
newSize.Width = System.Convert.ToInt32(maxHeight * imgProps);
}
return newSize;
}
