前言
大家好,我是上位机马工,硕士毕业4年年入40万,目前在一家自动化公司担任软件经理,从事C#上位机软件开发8年以上!我们在上位机软件开发过程中经常需要裁剪图像,本文就是对c#中常见的裁剪图像方法进行总结。
1、克隆
直接调用Bitmap的Clone函数,然后指定需要裁剪的区域即可裁剪图像,该种方法不会损失精度
public static Bitmap CropImage_Clone(Bitmap origBitmap, Rectangle rectangle, out bool result)
{
result = false;
Bitmap croppedBitmap = null;
try
{
croppedBitmap = origBitmap.Clone(rectangle, origBitmap.PixelFormat);
result = true;
}
catch (Exception ex)
{
}
return croppedBitmap;
}
2、gdi绘图(低质量)
使用gdi绘图的方式,优点是除了将原始图像根据指定区域裁剪外,而且可以在新的图像上绘制直线、矩形等图形,但是可能会丢失精度。
public static Bitmap CropImage_Gdi_LowerQuality(Bitmap origBitmap, Rectangle rectangle, out bool result)
{
result =