裁减Image

        /// <summary>
        /// Cut specified image with specified size
        /// </summary>
        /// <param name="src">the source image</param>
        /// <param name="x">start point_x</param>
        /// <param name="y">start point_y</param>
        /// <param name="width">width of new image</param>
        /// <param name="height">height of new image</param>
        /// <param name="dst">new image after being cut</param>
        /// <returns> 0 = normally return;
        ///          -1 = the source image is null
        ///          -2 = the start point is beyond the range of source image
        ///          -9 = cutting error
        /// </returns>
        public static int CutImage(Bitmap src, int x, int y, int width, int height, out Bitmap dst)
        {
            dst = null;
            if (src == null)
            {
                return -1;
            }
            if ((x >= src.Width) || (y >= src.Height))
            {
                return -2;
            }
            width = ((x + width) > src.Width) ? (src.Width - x) : width;
            height = ((y + height) > src.Height) ? (src.Height - y) : height;
            try
            {
                dst = new Bitmap(width, height, src.PixelFormat);
                Graphics g = Graphics.FromImage(dst);
                g.DrawImage(src, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
                g.Dispose();
            }
            catch (Exception e)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                Debug.WriteLine(String.Format("Line Number: {0}, {1}", sf.GetFileLineNumber(), e.Message));
                return -9;
            }
            return 0;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值