winForm pictureBox拖动图片and动态new 窗体&移动Panel窗体头

 

public void getScreen()
        {
            this.saveFileDialog2.Filter = "Image files (*.JPG)|*.JPG";
            this.saveFileDialog2.FilterIndex = 0;
            this.saveFileDialog2.RestoreDirectory = true;
            this.saveFileDialog2.Title = "导出文件保存路径";
            this.saveFileDialog2.FileName = "屏幕截图";
            if (this.saveFileDialog2.ShowDialog() == DialogResult.Cancel)
            {
                this.Cursor = Cursors.Default;
            }
            else
            {
                this.Refresh();
                Image image = new Bitmap(0x800, 0x600);
                Graphics graphics = Graphics.FromImage(image);
                graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(800, 600));
                IntPtr hdc = graphics.GetHdc();
                graphics.ReleaseHdc(hdc);
                image.Save(this.saveFileDialog2.FileName);
                MessageBox.Show("OK", "提示!");
            }
        }

==================pictureBox拖动图片====================================

   private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                Cursor = Cursors.Hand;
            }
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            Cursor = Cursors.Default;
            p.X = 0;
            p.Y = 0;
            driftX = 0;
            driftY = 0;
            mx = 0;
            my = 0;        
        }
        bool wselected = false;
        Point p = new Point();

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            wselected = true;
            p.X = e.X;
            p.Y = e.Y;   
        }
        int driftX = 0, driftY = 0;
        int mx = 0, my = 0;
        Graphics g;
        Bitmap bm;
  
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (wselected && this.pictureBox1.Image != null&&Cursors.Hand==Cursor)
                {
                    this.Invalidate();

                    driftX = p.X - e.X;
                    driftY = p.Y - e.Y;                

                    mx = mx - driftX;
                    my = my - driftY;
                    bm = new Bitmap(this.pictureBox1.Image);                   
                    g = pictureBox1.CreateGraphics();
                    g.Clear(pictureBox1.BackColor);                   
                    g.DrawImage(bm, mx, my);          

                 bm.Dispose();
                g.Dispose();        
                    p.X = e.X;
                    p.Y = e.Y;

                }
            }
            finally
            { }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            wselected = false;
        }

 =============图片另存为================

 SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif|Png 图片|*.png|Wmf  图片|*.wmf";
            saveFileDialog.FilterIndex = 0;
            if (this.pictureBox1.Image == null)
            {
                MessageBox.Show("没有预览图片!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (pictureBox1.Image != null)
                {
                     Bitmap bit = new Bitmap(pictureBox1.Image);
                    bit.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    bit.Dispose();
                }
            }

 //动态new 窗体

 Type t = Type.GetType("MyTest.Form6");//窗体名要加上程序集名称 
            Form f = (Form)System.Activator.CreateInstance(t, new object[] { new string[] { "测试1", "测试2" } });
            f.Show();

//winform Panel 作标题栏(移动Panel)

private bool IsMouseDown = false;
        private Point mouseOffset;

        private void panelTop_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsMouseDown = true;
            }
            mouseOffset = new Point(-e.X, -e.Y);
        }

        private void panelTop_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown == true)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                this.Location = mousePos;
            }            
        }

        private void panelTop_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseDown = false;
        }

//winform Panel 作标题栏(移动Panel)/结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm中,可以使用PictureBox控件来显示和操作图片。要实现图片的裁剪,可以按照以下步骤进行: 1. 首先,需要从文件或其他来源加载图片PictureBox控件中。可以使用PictureBox的Image属性来设置图片的路径或将图片直接赋值给Image属性。 2. 接下来,可以使用C#中的Graphics类来进行图片的裁剪操作。可以通过使用PictureBox的CreateGraphics方法获取PictureBox的画布。 3. 使用Graphics类的DrawImage方法,可以在画布上绘制图片。通过指定源图片的位置和大小,可以实现图片的裁剪。可以借助坐标和宽高等参数来确定要剪裁的部分。 以下是一个简单示例代码,实现将PictureBox控件中的图片按指定位置和大小进行裁剪: ``` private void CropImage(int x, int y, int width, int height) { Bitmap originalImage = (Bitmap)pictureBox1.Image; Bitmap croppedImage = new Bitmap(width, height); // 剪裁图片 using (Graphics g = Graphics.FromImage(croppedImage)) { g.DrawImage(originalImage, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel); } // 将裁剪后的图片显示在PictureBox控件上 pictureBox1.Image = croppedImage; } ``` 在上述代码中,CropImage方法接受四个参数分别表示裁剪的起始点坐标和裁剪的宽度和高度。该方法首先将原始图片转为Bitmap对象,再创建一个裁剪图片的Bitmap对象。通过使用Graphics类的DrawImage方法,将原始图片的指定部分绘制到裁剪图片的位置上。最后,将裁剪后的图片赋值给PictureBox控件的Image属性,从而显示裁剪后的图片效果。 希望这个回答对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值