pictureBox 鼠标拖动图像闪烁问题

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        Point offset = new Point(0, 0);
        Point p;
        Bitmap srcBitmap;
        Panel Panel1; 

        public Form1()
        {
            InitializeComponent();
        }

        private void Panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            p = e.Location;
            Panel1.Cursor = Cursors.Hand;
        }

        private void Panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button ==  MouseButtons.Left)
            {
                offset.Offset(e.X - p.X, e.Y - p.Y);
                this.MyReDrawTest(offset.X, offset.Y);
                p = e.Location;
            }
        }

        private void Panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Panel1.Cursor = Cursors.Default;
        }

        private void Panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            this.MyReDrawTest(offset.X, offset.Y);
        }

        private void MyReDrawTest(int x, int y)
        {
            if (srcBitmap == null)
                return; 
            BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
            BufferedGraphics myBuffer = currentContext.Allocate(this.Panel1.CreateGraphics(), this.Panel1.DisplayRectangle);

            myBuffer.Graphics.Clear(this.Panel1.BackColor);
            myBuffer.Graphics.DrawImage(srcBitmap, offset);
            myBuffer.Render( this.Panel1 .CreateGraphics() );
            myBuffer.Dispose();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            Panel1 = new Panel();
            Panel1.Dock = DockStyle.Fill;
            Panel1.MouseDown += Panel1_MouseDown;
            Panel1.MouseMove += Panel1_MouseMove;
            Panel1.MouseUp += Panel1_MouseUp;
            Panel1.Paint += Panel1_Paint;
            this.Controls.Add(Panel1);

            OpenFileDialog cd = new OpenFileDialog();
            cd.Filter = "bmp文件 (*.bmp)|*.bmp|jpg文件 (*.jpg)|*.jpg|gif文件 (*.gif)|*.gif|所有文件|*.*";
            if (cd.ShowDialog() ==    DialogResult.OK)
            {
                srcBitmap = new Bitmap(cd.FileName);
                offset = new Point(0, 0);
                Panel1.Invalidate();
            }
        } 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值