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

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

}

相关推荐
唐宋元明清21889 小时前
.NET Win32磁盘动态卷/跨区卷触发“函数不正确”问题排查
windows·c#·存储
hez201010 小时前
Satori GC:同时做到高吞吐、低延时和低内存占用
c#·.net·.net core·gc·clr
唐青枫1 天前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临2 天前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine6 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530146 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526746 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526746 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang7 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf10 天前
WPF新手村教程(三)—— 路由事件
c#·wpf