?? resizer.cs
字號(hào):
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace VirtualPhotoOrganizer.Photo
{
/// <summary>
/// Handles resizing, cropping, rotating, and the creation of thumbnails
/// </summary>
internal class Resizer
{
public Resizer() { }
/// <summary>
/// Resizes the specified image to the specified size with the specified InterpolationMode
/// </summary>
public Bitmap Resize(Bitmap image, int width, int height, InterpolationMode interpolationMode) {
// create the resized Bitmap and its Graphics object
Bitmap resizedImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(resizedImage);
g.InterpolationMode = interpolationMode;
// fill the resized Bitmap with the resized image
Rectangle destRect = new Rectangle(0, 0, width, height);
g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
// dispose the unneeded objects and return the resized Bitmap
g.Dispose();
return resizedImage;
}
/// <summary>
/// Returns a Bitmap containing the specified cropArea
/// </summary>
public Bitmap Crop(Bitmap image, Rectangle cropArea) {
// create the resized Bitmap and its Graphics object
Bitmap croppedImage = new Bitmap(cropArea.Width, cropArea.Height);
Graphics g = Graphics.FromImage(croppedImage);
// fill the resized Bitmap with the cropArea
Rectangle destRect = new Rectangle(0, 0, cropArea.Width, cropArea.Height);
g.DrawImage(image, destRect, cropArea, GraphicsUnit.Pixel);
// dispose the unneeded objects and return the cropped Bitmap
g.Dispose();
return croppedImage;
}
/// <summary>
/// Rotates the given image according to the specified RotateFlipType
/// </summary>
public Bitmap Rotate(Bitmap image, RotateFlipType flipType) {
image.RotateFlip(flipType);
return image;
}
public void Rotate(string path, RotateFlipType flipType, int quality) {
Image image = Bitmap.FromFile(path);
Bitmap b = new Bitmap(image); // create the bitmap
image.Dispose();
// rotate and save the bitmap
b.RotateFlip(flipType);
JpegSaver js = new JpegSaver();
js.SaveJpeg(path, b, quality);
// dispose the bitmap
b.Dispose();
}
/// <summary>
/// Saves a thumbnail of the specified image in the specified path
/// </summary>
public void CreateThumbnail(string srcPath, string destPath) {
// the defined constants for the max thumbnail dimensions
const int cthWidth = 150;
const int cthHeight = 113;
// the aspect ratio and the newly calculated thumbnail dimensions
int thWidth = 0;
int thHeight = 0;
// load the source image
Image img = Bitmap.FromFile(srcPath);
Bitmap srcImage = new Bitmap(img);
// calculate the new tumbnail dimensions, based on the longer side of the image
double ratio = srcImage.Height / srcImage.Width;
if (ratio < cthHeight / cthWidth) {
// Width is constant
thWidth = cthWidth;
thHeight = (srcImage.Height * cthWidth) / srcImage.Width;
} else {
// Height is constant
thHeight = cthHeight;
thWidth = (srcImage.Width * cthHeight) / srcImage.Height;
}
// create the thumbnail
Image thumb = srcImage.GetThumbnailImage(thWidth, thHeight, null, IntPtr.Zero);
// create a new bitmap for the border creation
Bitmap finthumb = new Bitmap(cthWidth, cthHeight);
Graphics g = Graphics.FromImage(finthumb);
// draw thumb into finthumb
Rectangle dest;
double size;
if (thumb.Width > thumb.Height) {
size = (cthHeight - thumb.Height) / 2;
if (size != cthHeight / 2) // check if image does not have cthWidth
dest = new Rectangle(0, (int) Math.Round(size, 0), thumb.Width, thumb.Height);
else
dest = new Rectangle(0, 0, thumb.Width, thumb.Height);
} else {
size = (cthWidth - thumb.Width) / 2;
if (size != cthWidth / 2) // check if image does not have cthWidth
dest = new Rectangle((int) Math.Round(size, 0), 0, thumb.Width, thumb.Height);
else
dest = new Rectangle(0, 0, thumb.Width, thumb.Height);
}
g.DrawImage(thumb, dest);
// save finthumb
finthumb.Save(destPath);
// dispose the gdi+ objects
g.Dispose();
img.Dispose();
srcImage.Dispose();
thumb.Dispose();
finthumb.Dispose();
}
#region HTMLExport
/// <summary>
/// Saves a thumbnail (without borders) of the specified image in the specified path
/// </summary>
public void CreateThumbnailWoBorders(string srcPath, string destPath) {
// the defined constants for the max thumbnail dimensions
const int cthWidth = 150;
const int cthHeight = 113;
// the aspect ratio and the newly calculated thumbnail dimensions
int thWidth = 0;
int thHeight = 0;
// load the source image
Image img = Bitmap.FromFile(srcPath);
Bitmap srcImage = new Bitmap(img);
// calculate the new tumbnail dimensions, based on the longer side of the image
double ratio = srcImage.Height / srcImage.Width;
if (ratio < cthHeight / cthWidth) {
// Width is constant
thWidth = cthWidth;
thHeight = (srcImage.Height * cthWidth) / srcImage.Width;
} else {
// Height is constant
thHeight = cthHeight;
thWidth = (srcImage.Width * cthHeight) / srcImage.Height;
}
// create the thumbnail
Image thumb = srcImage.GetThumbnailImage(thWidth, thHeight, null, IntPtr.Zero);
// save the thumbnail
thumb.Save(destPath);
// dispose the gdi+ objects
thumb.Dispose();
srcImage.Dispose();
img.Dispose();
}
/// <summary>
/// Resizes the specified image to the specified size with the specified InterpolationMode and then saves it with the specified quality
/// </summary>
public void Resize(string srcPath, string destPath, int width, int height, int quality, InterpolationMode interpolationMode) {
// open the original image
Image img = Bitmap.FromFile(srcPath);
Bitmap image = new Bitmap(img);
// calculate the right size for the destRect
Rectangle destRect = new Rectangle(0, 0, width, height);
// calculate the new tumbnail dimensions, based on the longer side of the image
double ratio = image.Height / image.Width;
if (ratio < height / width) {
// Width is constant
destRect.Width = width;
destRect.Height = (image.Height * width) / image.Width;
} else {
// Height is constant
destRect.Height = height;
destRect.Width = (image.Width * height) / image.Height;
}
// create the resized Bitmap and its Graphics object
Bitmap resizedImage = new Bitmap(destRect.Width, destRect.Height);
Graphics g = Graphics.FromImage(resizedImage);
g.InterpolationMode = interpolationMode;
// fill the resized Bitmap with the resized image
g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
// save the resized image
JpegSaver js = new JpegSaver();
js.SaveJpeg(destPath, resizedImage, quality);
// dispose the unneeded objects
g.Dispose();
resizedImage.Dispose();
img.Dispose();
image.Dispose();
}
#endregion
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -