9 个解决方案
#1
请注意,不是拖动pictureBox,谢谢
#2
采用图片重新绘。
#3
你可以通过继承PictureBox来达到这一点。
#4
你可以参考下面的代码来自己编写一个控件:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
namespace PictureBoxApp
{
public class ImageCtr : ScrollableControl
{
private Image m_Img;
public Image Img
{
get { return m_Img; }
set
{
if (value != this.m_Img)
{
m_Img = value;
if (this.m_Img != null)
{
this.AutoScrollMinSize = this.m_Img.Size;
}
else
{
this.AutoScrollMinSize = Size.Empty;
}
this.Invalidate();
}
}
}
public ImageCtr()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.m_Img != null)
{
e.Graphics.DrawImage(this.m_Img, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.m_Img.Width, this.m_Img.Height);
}
}
protected override void OnPrint(PaintEventArgs e)
{
if (this.m_Img != null)
{
e.Graphics.DrawImage(this.m_Img, 0, 0, this.m_Img.Width, this.m_Img.Height);
}
}
}
}
#5
直接把picturebox控件的SizeMode属性修改为StretchImage
#6
多谢各位,还是通过重绘解决了问题
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;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (wselected)
{
driftX = p.X - e.X;
driftY = p.Y - e.Y;
mx = mx - driftX;
my = my - driftY;
Bitmap bm = new Bitmap(this.pictureBox1.Image);
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1.BackColor);
g.DrawImage(bm, mx, my);
p.X = e.X;
p.Y = e.Y;
bm.Dispose();
g.Dispose();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
wselected = false;
}
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;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (wselected)
{
driftX = p.X - e.X;
driftY = p.Y - e.Y;
mx = mx - driftX;
my = my - driftY;
Bitmap bm = new Bitmap(this.pictureBox1.Image);
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1.BackColor);
g.DrawImage(bm, mx, my);
p.X = e.X;
p.Y = e.Y;
bm.Dispose();
g.Dispose();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
wselected = false;
}
#7
谢楼上的代码...!!!
#8
楼上代码很给力 就是闪烁太厉害了 谢谢楼主
#9
不错的贴!谢谢LZ的代码~~
#1
请注意,不是拖动pictureBox,谢谢
#2
采用图片重新绘。
#3
你可以通过继承PictureBox来达到这一点。
#4
你可以参考下面的代码来自己编写一个控件:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
namespace PictureBoxApp
{
public class ImageCtr : ScrollableControl
{
private Image m_Img;
public Image Img
{
get { return m_Img; }
set
{
if (value != this.m_Img)
{
m_Img = value;
if (this.m_Img != null)
{
this.AutoScrollMinSize = this.m_Img.Size;
}
else
{
this.AutoScrollMinSize = Size.Empty;
}
this.Invalidate();
}
}
}
public ImageCtr()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.m_Img != null)
{
e.Graphics.DrawImage(this.m_Img, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.m_Img.Width, this.m_Img.Height);
}
}
protected override void OnPrint(PaintEventArgs e)
{
if (this.m_Img != null)
{
e.Graphics.DrawImage(this.m_Img, 0, 0, this.m_Img.Width, this.m_Img.Height);
}
}
}
}
#5
直接把picturebox控件的SizeMode属性修改为StretchImage
#6
多谢各位,还是通过重绘解决了问题
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;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (wselected)
{
driftX = p.X - e.X;
driftY = p.Y - e.Y;
mx = mx - driftX;
my = my - driftY;
Bitmap bm = new Bitmap(this.pictureBox1.Image);
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1.BackColor);
g.DrawImage(bm, mx, my);
p.X = e.X;
p.Y = e.Y;
bm.Dispose();
g.Dispose();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
wselected = false;
}
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;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (wselected)
{
driftX = p.X - e.X;
driftY = p.Y - e.Y;
mx = mx - driftX;
my = my - driftY;
Bitmap bm = new Bitmap(this.pictureBox1.Image);
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1.BackColor);
g.DrawImage(bm, mx, my);
p.X = e.X;
p.Y = e.Y;
bm.Dispose();
g.Dispose();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
wselected = false;
}
#7
谢楼上的代码...!!!
#8
楼上代码很给力 就是闪烁太厉害了 谢谢楼主
#9
不错的贴!谢谢LZ的代码~~