GDI+ 图片移动问题

hotmail_mk 2008-07-25 03:52:54
我刚使用GDI+不久,现在在图片移动上遇到如下困惑,一整天了,没找到原因。采用的是pictureBox显示,首先将一幅背景图和一幅小图画到对象bmp中,然后显示在pictureBox1上。当鼠标拖动图片的的时候判断拖得是大图还是小图,拖大图就一起移动,拖小图就小图移动。现在载入图片后直接拖小图没有问题,但是拖完大图再拖小图坐标系统就出问题了。由于对GDI+刚开始学习,所以想不通错误的地方。高手们帮看看代码有什么问题?谢谢!以下是测试主要代码(只是为了测试功能):
public partial class Form1 : Form
{
private Bitmap bmp = null;
private Bitmap bmt = null;
private Bitmap bmk = null;
private Point lastPoint = new Point(); //点击位置
private Point offset = new Point();//最终位置
int i = 150, k = 0;
Rectangle r = new Rectangle(150, 0, 65,82);//直接将小图初始位置标出

private void Form1_Load(object sender, EventArgs e)
{
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height);
}

private void btNew_Click(object sender, EventArgs e)
{
Graphics g1 = Graphics.FromImage(bmp);
Bitmap bmtest = new Bitmap(@"2.jpg");
bmk = new Bitmap(bmtest);
bmtest.SetResolution(128f, 128f);
g1.DrawImage(bmtest, 0, 0);
bmtest.Dispose();
bmtest = new Bitmap(@"1.jpg");
bmt = new Bitmap(bmtest);
g1.DrawImage(bmtest, 150, 0);
pictureBox1.Invalidate();
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = e.Location;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g1 = Graphics.FromImage(bmp);


PointTextBox.Text = e.Location.X.ToString() + "," + e.Location.Y.ToString() + "|" + r.X.ToString() + "," + r.Y.ToString();
if (e.Button == MouseButtons.Left)
{

Cursor.Current = Cursors.Hand;

if (r.Contains(lastPoint))
{

r.Offset((e.X - lastPoint.X), (e.Y - lastPoint.Y));
g1.DrawImage(bmt,r);
}
else
{
offset.X += e.X - lastPoint.X;
offset.Y += e.Y - lastPoint.Y;
r.Offset((e.X - lastPoint.X), (e.Y - lastPoint.Y));
}

lastPoint = e.Location;
pictureBox1.Invalidate();
}

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (bmp != null)
{

//e.Graphics.DrawImage(bmp, pictureBox1.ClientRectangle, -offset.X, -offset.Y, pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height, GraphicsUnit.Pixel);
e.Graphics.DrawImage(bmp, offset);
}
}

...全文
246 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hotmail_mk 2008-07-28
  • 打赏
  • 举报
回复
不是误差产生的,是移动背景图的坐标处理有问题。
hotmail_mk 2008-07-28
  • 打赏
  • 举报
回复
自己解决了。
jl_lsj 2008-07-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hotmail_mk 的回复:]
引用 1 楼 sjm2003 的回复:
判断坐标对象的问题

不理解,能具体一些吗?
[/Quote]
hotmail_mk 2008-07-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sjm2003 的回复:]
判断坐标对象的问题
[/Quote]
不理解,能具体一些吗?
hotmail_mk 2008-07-25
  • 打赏
  • 举报
回复
我自己的分析是由于再做计算的时候的误差累积造成,因为我发现移动后画图的位置和计算的结果差值不稳定。如果是这个原因该如何解决?
另外加一个问题,像我这样的移动图片的情况,c#结合GDI+可不可以像Photoshop里的一样把小图作为一个图层画在大图上面,有没有这样的例子?
sjm2003 2008-07-25
  • 打赏
  • 举报
回复
判断坐标对象的问题

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧