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

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

}

相关推荐
如果'\'真能转义说21 小时前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
我是唐青枫21 小时前
终于不用手搓两级缓存了!C#.NET HybridCache 详解:L1 L2、标签失效与防击穿实战
redis·缓存·c#·.net
CHANG_THE_WORLD1 天前
C语言中的 %*s 和 %.*s 和C++的字符串格式化输出
c语言·c++·c#
zxbmmmmmmmmm1 天前
在 Avalonia 中编写高性能动画
c#·xaml·avalonia·compositon
加号31 天前
【C#】 HTTP 请求通讯实现指南
开发语言·http·c#
步步为营DotNet1 天前
.NET 11 中 Microsoft.Extensions.AI 在智能后端推理与决策优化的应用
云原生·c#·.net
工程师0071 天前
.NET 线程池 工作线程 扩容 + 空闲 + 回收 原理
c#·线程池·扩容·回收·空闲
njsgcs1 天前
c# solidworks createline 拉伸发现有微小两点间隙 导致拉伸变成薄壁特征 改bug画了6个小时 解决结果
c#·bug·solidworks
时光追逐者1 天前
一款基于 C# 开发的 Windows 10/11 系统增强工具,精简、优化、定制一站完成!
开发语言·windows·c#·.net