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

/// <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;

}

相关推荐
逝水无殇12 小时前
C# 异常处理详解
开发语言·后端·c#
玖玥拾13 小时前
C# 语言进阶(十五)C# 游戏服务端 MySQL 数据库
服务器·开发语言·网络·数据库·mysql·c#
逝水无殇15 小时前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#
唐青枫20 小时前
看懂 IL 的关键:C#.NET 栈机执行模型实战拆解
c#·.net
我才是银古20 小时前
从零构建 Windows 桌面 RPA 框架:Go 与 .NET 的跨语言协奏
c#·go·rpa
逝水无殇1 天前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博1 天前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
吴可可1231 天前
C# CAD二次开发添加带按钮的窗口
c#
铅笔侠_小龙虾1 天前
Rust 学习(8)-切片类型
学习·rust·c#
习明然2 天前
我的本地化AI项目(三)
人工智能·python·electron·c#·avalonia