C# 图片缩放和图片裁剪

/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp"> 原始Bitmap </param>
/// <param name="newW"> 新的宽度 </param>
/// <param name="newH"> 新的高度 </param>
/// <param name="Mode"> 保留着,暂时未用 </param>
/// <returns> 处理以后的图片 </returns>
public static Bitmap KiResizeImage(Bitmap bmp,
int newW, int newH, int Mode)
{
try
{
Bitmap b
= new Bitmap(newW, newH);
Graphics g
= Graphics.FromImage(b);

// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(bmp,
new Rectangle( 0 , 0 , newW, newH),
new Rectangle( 0 , 0 , bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();

return b;
}
catch
{
return null ;
}
}

// ===============================

/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b"> 原始Bitmap </param>
/// <param name="StartX"> 开始坐标X </param>
/// <param name="StartY"> 开始坐标Y </param>
/// <param name="iWidth"> 宽度 </param>
/// <param name="iHeight"> 高度 </param>
/// <returns> 剪裁后的Bitmap </returns>
public static Bitmap KiCut(Bitmap b, int StartX,
int StartY, int iWidth, int iHeight)
{
if (b == null )
{
return null ;
}

int w = b.Width;
int h = b.Height;

if (StartX >= w || StartY >= h)
{
return null ;
}

if (StartX + iWidth > w)
{
iWidth
= w - StartX;
}

if (StartY + iHeight > h)
{
iHeight
= h - StartY;
}

try
{
Bitmap bmpOut
= new Bitmap(iWidth, iHeight,
PixelFormat.Format24bppRgb);

Graphics g
= Graphics.FromImage(bmpOut);
g.DrawImage(b,
new Rectangle( 0 , 0 , iWidth, iHeight),
new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();

return bmpOut;
}
catch
{
return null ;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值