C#圖片剪裁類

bdnp 9年前發布 | 2K 次閱讀 C#

public class ImageCut
{
    /// <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 Bitmap KiCut(Bitmap b)
{
    if (b == null)
    {
        return null;
    }

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

    if (X >= w || Y >= h)
    {
        return null;
    }

    if (X + Width > w)
    {
        Width = w - X;
    }

    if (Y + Height > h)
    {
        Height = h - Y;
    }

    try
    {
        Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

        Graphics g = Graphics.FromImage(bmpOut);
        g.DrawImage(b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
        g.Dispose();

        return bmpOut;
    }
    catch
    {
        return null;
    }
}

public int X = 0;
public int Y = 0;
public int Width = 120;
public int Height = 120;
public ImageCut(int x, int y, int width, int heigth)
{
    X = x;
    Y = y;
    Width = width;
    Height = heigth;
}

}</pre>

 本文由用戶 bdnp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!