2020-08-17

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FacePage
{
    public class DragComponent
    {
        #region 实现窗体内的控件拖动
        //用法:在Form初始化或者Form_Load时先执行
        //DragComponent a = new DragComponent();
        //a.initProperty(groupBox1);
        //将界面groupBox1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件。 

        private Control control;
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p, p1;

        public enum EnumMousePointPosition
        {

            MouseSizeNone = 0, //'无

            MouseSizeRight = 1, //'拉伸右边框

            MouseSizeLeft = 2, //'拉伸左边框

            MouseSizeBottom = 3, //'拉伸下边框

            MouseSizeTop = 4, //'拉伸上边框

            MouseSizeTopLeft = 5, //'拉伸左上角

            MouseSizeTopRight = 6, //'拉伸右上角

            MouseSizeBottomLeft = 7, //'拉伸左下角

            MouseSizeBottomRight = 8, //'拉伸右下角

            MouseDrag = 9 // '鼠标拖动

        }

        public void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            p.X = e.X;

            p.Y = e.Y;

            p1.X = e.X;

            p1.Y = e.Y;

        }

        public void MyMouseLeave(object sender, System.EventArgs e)
        {

            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;

            control.Cursor = Cursors.Arrow;

        }

        public EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
        {

            if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {

                if (e.X < Band)
                {

                    if (e.Y < Band)
                    {
                        return EnumMousePointPosition.MouseSizeTopLeft;
                    }

                    else
                    {

                        if (e.Y > -1 * Band + size.Height)
                        {
                            return EnumMousePointPosition.MouseSizeBottomLeft;
                        }

                        else
                        {
                            return EnumMousePointPosition.MouseSizeLeft;
                        }

                    }

                }

                else
                {

                    if (e.X > -1 * Band + size.Width)
                    {

                        if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTopRight;
                        }

                        else
                        {

                            if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottomRight;
                            }

                            else
                            {
                                return EnumMousePointPosition.MouseSizeRight;
                            }

                        }

                    }

                    else
                    {

                        if (e.Y < Band)
                        {
                            return EnumMousePointPosition.MouseSizeTop;
                        }

                        else
                        {

                            if (e.Y > -1 * Band + size.Height)
                            {
                                return EnumMousePointPosition.MouseSizeBottom;
                            }

                            else
                            {
                                return EnumMousePointPosition.MouseDrag;
                            }

                        }

                    }

                }

            }

            else
            {
                return EnumMousePointPosition.MouseSizeNone;
            }

        }

        public void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            Control lCtrl = (sender as Control);
            if (e.Button == MouseButtons.Left)
            {

                switch (m_MousePointPosition)
                {

                    case EnumMousePointPosition.MouseDrag:

                        lCtrl.Left = lCtrl.Left + e.X - p.X;

                        lCtrl.Top = lCtrl.Top + e.Y - p.Y;

                        break;

                    case EnumMousePointPosition.MouseSizeBottom:

                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;

                        p1.X = e.X;

                        p1.Y = e.Y; //'记录光标拖动的当前点

                        break;

                    case EnumMousePointPosition.MouseSizeBottomRight:

                        lCtrl.Width = lCtrl.Width + e.X - p1.X;

                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;

                        p1.X = e.X;

                        p1.Y = e.Y; //'记录光标拖动的当前点

                        break;

                    case EnumMousePointPosition.MouseSizeRight:

                        lCtrl.Width = lCtrl.Width + e.X - p1.X;

                        //      lCtrl.Height = lCtrl.Height + e.Y - p1.Y;

                        p1.X = e.X;

                        p1.Y = e.Y; //'记录光标拖动的当前点

                        break;

                    case EnumMousePointPosition.MouseSizeTop:

                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);

                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);

                        break;

                    case EnumMousePointPosition.MouseSizeLeft:

                        lCtrl.Left = lCtrl.Left + e.X - p.X;

                        lCtrl.Width = lCtrl.Width - (e.X - p.X);

                        break;

                    case EnumMousePointPosition.MouseSizeBottomLeft:

                        lCtrl.Left = lCtrl.Left + e.X - p.X;

                        lCtrl.Width = lCtrl.Width - (e.X - p.X);

                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;

                        p1.X = e.X;

                        p1.Y = e.Y; //'记录光标拖动的当前点

                        break;

                    case EnumMousePointPosition.MouseSizeTopRight:

                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);

                        lCtrl.Width = lCtrl.Width + (e.X - p1.X);

                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);

                        p1.X = e.X;

                        p1.Y = e.Y; //'记录光标拖动的当前点

                        break;

                    case EnumMousePointPosition.MouseSizeTopLeft:

                        lCtrl.Left = lCtrl.Left + e.X - p.X;

                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);

                        lCtrl.Width = lCtrl.Width - (e.X - p.X);

                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);

                        break;

                    default:

                        break;

                }

                if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;

                if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;

            }

            else
            {

                m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态

                switch (m_MousePointPosition) //'改变光标
                {

                    case EnumMousePointPosition.MouseSizeNone:

                        control.Cursor = Cursors.Arrow;       //'箭头

                        break;

                    case EnumMousePointPosition.MouseDrag:

                        control.Cursor = Cursors.SizeAll;     //'四方向

                        break;

                    case EnumMousePointPosition.MouseSizeBottom:

                        control.Cursor = Cursors.SizeNS;      //'南北

                        break;

                    case EnumMousePointPosition.MouseSizeTop:

                        control.Cursor = Cursors.SizeNS;      //'南北

                        break;

                    case EnumMousePointPosition.MouseSizeLeft:

                        control.Cursor = Cursors.SizeWE;      //'东西

                        break;

                    case EnumMousePointPosition.MouseSizeRight:

                        control.Cursor = Cursors.SizeWE;      //'东西

                        break;

                    case EnumMousePointPosition.MouseSizeBottomLeft:

                        control.Cursor = Cursors.SizeNESW;    //'东北到南西

                        break;

                    case EnumMousePointPosition.MouseSizeBottomRight:

                        control.Cursor = Cursors.SizeNWSE;    //'东南到西北

                        break;

                    case EnumMousePointPosition.MouseSizeTopLeft:

                        control.Cursor = Cursors.SizeNWSE;    //'东南到西北

                        break;

                    case EnumMousePointPosition.MouseSizeTopRight:

                        control.Cursor = Cursors.SizeNESW;    //'东北到南西

                        break;

                    default:

                        break;

                }

            }
        }

        public void initProperty(Control ctl)
        {
            control = ctl;
            control.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
            control.MouseLeave += new System.EventHandler(MyMouseLeave);
            control.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);

        }

        #endregion

    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FacePage
{
    public class SaveImage
    {
        ///   将容器内容转成图片导出,这里的controller就是this
        /// </summary>
        public static void OutTheControllerToPic(Control control)
        {

            Bitmap bitmap = new Bitmap(control.Width, control.Height);
            DrawToBitmap(control, bitmap, new Rectangle(0, 0, control.Width, control.Height));
            bool isSave = true;
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "图片保存";
            saveImageDialog.Filter = @"png|*.png|jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();
                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;
                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            case "gif":
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;
                                break;
                            case "png":
                                imgformat = System.Drawing.Imaging.ImageFormat.Png;
                                break;
                            default:
                                MessageBox.Show("只能存取为: jpg,bmp,gif,png 格式");
                                isSave = false;
                                break;
                        }
                    }
                    //默认保存为JPG格式  
                    if (imgformat == null)
                    {
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }
                    if (isSave)
                    {
                        try
                        {
                            bitmap.Save(fileName, imgformat);
                            MessageBox.Show("图片已经成功保存!");
                        }
                        catch
                        {
                            MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
                        }
                    }
                }
            }
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);
        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        public static extern bool BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
        /// <summary>
        /// 支持呈现到指定的位图。
        /// </summary>
        public static Bitmap DrawToBitmap(Control control, Bitmap bitmap, Rectangle targetBounds)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }
            if (((targetBounds.Width <= 0) || (targetBounds.Height <= 0)) || ((targetBounds.X < 0) || (targetBounds.Y < 0)))
            {
                throw new ArgumentException("targetBounds");
            }
            Bitmap image = new Bitmap(control.Width, control.Height, bitmap.PixelFormat);
            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hdc = graphics.GetHdc();
                SendMessage(new HandleRef(control, control.Handle), 0x317, hdc, (IntPtr)30);
                using (Graphics graphics2 = Graphics.FromImage(bitmap))
                {
                    IntPtr handle = graphics2.GetHdc();
                    BitBlt(new HandleRef(graphics2, handle), 0, 0, control.Width, control.Height, new HandleRef(graphics, hdc), targetBounds.X, targetBounds.Y, 0xcc0020);
                    graphics2.ReleaseHdcInternal(handle);
                }
                graphics.ReleaseHdcInternal(hdc);
            }
            return image;
        }
    }
}
 

.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FacePage
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnAddPanel_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog lvse = new OpenFileDialog())
            {
                lvse.Title = "选择图片";
                lvse.InitialDirectory = "";
                lvse.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
                lvse.FilterIndex = 1;
                if (lvse.ShowDialog() == DialogResult.OK)
                {
                    PictureBox pic = PanelHelper.CreatePictureBox(lvse.FileName);
                    DragComponent a = new DragComponent();
                    a.initProperty(pic);
                    panel1.Controls.Add(pic);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveImage.OutTheControllerToPic(panel1);
        }


    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值