📄 imageprocessor.cs
字号:
using System;
using System.Drawing;
namespace Addot.Drawing
{
/// <summary>
/// Implementation of some image processing.
/// Please, refer to the .Net framework for the complete documentation.
/// </summary>
public class ImageProcessor
{
public static Bitmap RotateFlip(Bitmap source, RotateFlipType change)
{
Bitmap dest = new Bitmap(source.Size.Height, source.Size.Width);
int MaxWidthIndex = source.Size.Width - 1;
int MaxHeightIndex = source.Size.Height - 1;
for(int X = 0; X < source.Size.Width; X++)
{
for(int Y = 0; Y < source.Size.Height; Y++)
{
switch(change)
{
case RotateFlipType.Rotate270FlipNone:
dest.SetPixel(Y, MaxWidthIndex - X, source.GetPixel(X, Y));
break;
case RotateFlipType.Rotate90FlipNone:
dest.SetPixel(MaxHeightIndex - Y, X, source.GetPixel(X, Y));
break;
}
}
}
return dest;
}
public static Bitmap GetThumbnailImage(Bitmap sourceImg, int width, int height, bool drawRectangle, bool stretchLittle)
{
//
// Create the thumb
//
Bitmap thumb = new Bitmap(width, height);
Graphics toBeDrawn = Graphics.FromImage(thumb);
//
// Fill the thumb with WHITE
//
SolidBrush white = new SolidBrush(Color.White);
toBeDrawn.FillRectangle(white, 0, 0, thumb.Width, thumb.Height);
//
// Resize the sourceImg
//
Size newImageSize = new Size(sourceImg.Size.Width, sourceImg.Size.Height);
Point location = new Point(0, 0);
// If the original image is not bigger than the thumb,
// it is displayed as this, just center in its container.
if( !stretchLittle && (sourceImg.Width < width) && (sourceImg.Height < height) )
{
location = SizeProcessor.CenterToContainer(new Size(width, height), newImageSize);
}
else
{
SizeProcessor.FitToContainer(thumb.Size, ref newImageSize, ref location);
}
//
// Draw the image
//
Rectangle source = new Rectangle(0, 0, sourceImg.Width, sourceImg.Height);
Rectangle dest = new Rectangle(location.X, location.Y, newImageSize.Width, newImageSize.Height);
toBeDrawn.DrawImage(sourceImg, dest, source, GraphicsUnit.Pixel);
//
// Draw the thumbnail rectangle
//
if( drawRectangle )
{
Pen pen = new Pen(Color.Black);
toBeDrawn.DrawRectangle(pen, 0, 0, width - 1, height - 1);
}
return thumb;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -