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();
}
}
}
}
pictureBox 鼠标拖动图像闪烁问题
最新推荐文章于 2024-02-02 23:54:44 发布