图像的处理 图片裁剪工具方法 图片按比例缩放的工具方法

/// <summary>

/// 图片裁剪工具方法

/// </summary>

/// <param name="Bitmap"></param>

/// <param name="Rect"></param>

/// <returns></returns>

public static Bitmap CutImage(Bitmap Bitmap, ref Rectangle Rect)

{

if (Rect.X < 0)

{

Rect.X = 0;

}

if (Rect.Y < 0)

{

Rect.Y = 0;

}

if (Rect.X + Rect.Width > Bitmap.Width)

{

Rect.Width = Bitmap.Width - Rect.X;

}

if (Rect.Y + Rect.Height > Bitmap.Height)

{

Rect.Height = Bitmap.Height - Rect.Y;

}

if (0 == Rect.Width)

{

Rect.Width = 4;

}

if (0 == Rect.Height)

{

Rect.Height = 4;

}

return Bitmap.Clone(Rect, Bitmap.PixelFormat);

}

/// <summary>

/// 图片按比例缩放的工具方法

/// </summary>

/// <param name="bitmap"></param>

/// <param name="Scale"></param>

/// <returns></returns>

public static Bitmap ResizeImage(Bitmap bitmap, float Scale = 1f)

{

try

{

if (null != bitmap)

{

int w = bitmap.Width;

}

}

catch (ArgumentException)//图像被释放了

{

bitmap = null;

}

if (1f == Scale || null == bitmap)

{

return bitmap;

}

Bitmap result;

try

{

int width = (int)(bitmap.Width * Scale);

int height = (int)(bitmap.Height * Scale);

Bitmap bitmap2 = new Bitmap(width, height);

Graphics graphics = Graphics.FromImage(bitmap2);

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

graphics.CompositingQuality = CompositingQuality.HighSpeed;

graphics.DrawImage(bitmap, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);

graphics.Dispose();

result = bitmap2;

}

catch

{

result = bitmap;

}

return result;

}

相关推荐
Sunsets_Red2 小时前
浅谈随机化与模拟退火
java·c语言·c++·python·算法·c#·信息学竞赛
两千次4 小时前
图像的处理 图像转haclon
c#
bugcome_com8 小时前
C# 运算符详解:类型、实例及优先级
c#
C#程序员一枚9 小时前
Web Service 和 Web API
c#
MaoziShan1 天前
CMU Subword Modeling | 09 Lexemes, or What Dictionaries Know about Morphology
开发语言·人工智能·机器学习·语言模型·自然语言处理·c#
游乐码1 天前
c#选择排序
c#·排序算法
listhi5201 天前
基于C#实现动态人脸检测
开发语言·c#
rabbitlzx1 天前
《Async in C# 5.0》第十四章 深入探讨编译器对于async的转换
java·开发语言·c#·异步·asynchronous