public int Width {
set {
if(value < 300) {
m_Width = 300;
} else {
m_Width = value;
}
}
get {
return m_Width;
}
} public int Height {
set {
if(value < 300) {
m_Height = 300;
} else {
m_Height = value;
}
}
get {
return m_Height;
}
} private int m_Width = 640; //图像宽度
private int m_Height = 480; //图像高度
//初始化和填充图像区域,画出边框,初始标题
private void InitializeGraph() { //根据给定的高度和宽度创建一个位图图像
objBitmap = new Bitmap(Width, Height);
}
set {
if(value < 300) {
m_Width = 300;
} else {
m_Width = value;
}
}
get {
return m_Width;
}
} public int Height {
set {
if(value < 300) {
m_Height = 300;
} else {
m_Height = value;
}
}
get {
return m_Height;
}
} private int m_Width = 640; //图像宽度
private int m_Height = 480; //图像高度
//初始化和填充图像区域,画出边框,初始标题
private void InitializeGraph() { //根据给定的高度和宽度创建一个位图图像
objBitmap = new Bitmap(Width, Height);
}
//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;你这两个设置这么高,肯定会很慢的呀
/// 制作小正方形
/// </summary>
/// <param name="fileName">原图的文件路径</param>
/// <param name="newFileName">新地址</param>
/// <param name="newSize">长度或宽度</param>
public static void MakeSquareImage(string fileName, string newFileName, int newSize)
{
Image image = Image.FromFile(fileName); int i = 0;
int width = image.Width;
int height = image.Height;
if (width > height)
i = height;
else
i = width; Bitmap b = new Bitmap(newSize, newSize); try
{
Graphics g = Graphics.FromImage(b);
g.InterpolationMode = InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
//清除整个绘图面并以透明背景色填充
g.Clear(Color.Transparent);
if (width < height)
g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel);
else
g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel); SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(fileName).ToString().ToLower()));
}
finally
{
image.Dispose();
b.Dispose();
}
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="image">Image 对象</param>
/// <param name="savePath">保存路径</param>
/// <param name="ici">指定格式的编解码参数</param>
private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
{
//设置 原图片 对象的 EncoderParameters 对象
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long)100));
image.Save(savePath, ici, parameters);
parameters.Dispose();
} /// <summary>
/// 获取图像编码解码器的所有相关信息
/// </summary>
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
/// <returns>返回图像编码解码器的所有相关信息</returns>
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType)
return ici;
}
return null;
}