c# 拖动 picturebox里的图片 比如图片比较大 或者经过放大后超出了picturebox的边框 怎么拖动图片来看的比较舒服或者看到到被遮住的部分 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请注意,不是拖动pictureBox,谢谢 你可以通过继承PictureBox来达到这一点。 你可以参考下面的代码来自己编写一个控件: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); } } }} 直接把picturebox控件的SizeMode属性修改为StretchImage 多谢各位,还是通过重绘解决了问题 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; } 请问已知窗口句柄,如何获取控件的信息 想用c#架一个C/s程序.关于在线车辆管理系统的. C#中定义常量 关于回调函数。 在C#.NET中,如果设定了自定义绑定一个表中两列,怎样实现分页 给我公司的员工加分 我遇到的问题,大家帮忙!50分在线等! DataGridView单元格重绘后在选中时背景色的问题 如何查询某个数据库有多少个数据表??? System.Array不包含"Zip"的定义 如何使TabPage的标签在一定时间段内背景色变化? 关于反双斜杠 "\\" 转换的问题
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);
}
}
}
}
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;
}