vs2005平台 如何获取picturebox图片中的图像坐标,即鼠标在图片上移动时显示鼠标当前的坐标,求c#代码

vs2005平台 如何获取picturebox图片中的图像坐标,即鼠标在图片上移动时显示鼠标当前的坐标,求c#代码,第1张

坐标是指相对屏幕的坐标还是相对picturebox的坐标?

如果是相对屏幕坐标,可以调用系统API函数获取

[DllImport("User32dll")]

public static extern bool GetCursorPos (ref Point NewPoint);

如果是相对picturebox坐标,可是使用mousemove事件,然后在事件里调用ex,ey就能获取到坐标位置了

软糖来回答罗,纯手打

如果缩放比例是固定的,用picturebox变大后的尺寸可以计算出的尺寸。

首先你获取默认状态下Picturebox的尺寸和的尺寸。

然后相除获得缩放比例scaleX和scaleY

计算出picturebox变大后的中心点

centerX = picturebox1X + picturebox1Width / 2;

centerY = picturebox1Y + picturebox1Height / 2;

计算缩放后的尺寸,假设缩放比例已从上面获得(scaleX和scaleY)

imageWidth = image1Width  scaleX

imageHeight = image1Height  scaleY

然后就可以计算出的左上角位置

 image1X = centerX - imageWidth / 2;

 image1Y = centerY - imageHeight / 2;

如果缩放比例不确定,那就有点复杂了,要用到SystemDrawing命名空间:

使用GDI+绘图来控制内部显示,这样最方便了。

当picturebox尺寸变化时(SizeChanged事件还是Resize事件试一下),

按上面的方法计算出中心点、缩放比例,然后计算出矩形

最后获取picturebox的graphics对象,进行绘图。

picturebox不要装入,使用绘制图像方法来绘制,在FormLoad事件里面也绘制一次。

        var 画纸 = picturebox1creategraphics();

        画纸Clear(背景Color);

        画纸绘制图像(位图, 目标矩形);

        public void 绘制图像(Bitmap 位图, Rectangle 目标矩形)

        { 画纸DrawImage(位图, 目标矩形); }

我觉得比较好的办法是在pictureBox的容器中循环找pictureBox控件,再根据pictureBox控件的某一属性,如它显示的等确定具体的控件,最后利用Location属性获取其坐标信息

两种方法:

1在pictureBox1的MouseDown事件里得到坐标:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

{

int x = eX;

int y = eY;

}

//x,y就是相对pictureBox1的坐标,

2先得到相对于屏幕的坐标,然后通过pictureBox1PointToClient()转换到相对于pictureBox1的坐标:

Point mx = MousePosition;

Point mx2 = pictureBox1PointToClient(mx);

//mx2就是相对pictureBox1的坐标,

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using SystemDrawingImaging;

namespace qiequtupian

{

public partial class mainform : Form

{

bool isDrag = false;

Rectangle theRectangle = new Rectangle(new Point(0, 0), new Size(0, 0));

Point startPoint, oldPoint;

private Graphics ig;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

openFileDialog1Filter = "jpg,jpeg,bmp,gif,ico,png,tif,wmf|jpg;jpeg;bmp;gif;ico;png;tif;wmf";

openFileDialog1ShowDialog();

Image myImage = SystemDrawingImageFromFile(openFileDialog1FileName);

pictureBox1Image = myImage;

}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

{

if (eButton == MouseButtonsLeft)

{

//如果开始绘制,则开始记录鼠标位置

if ((isDrag = !isDrag) == true)

{

startPoint = new Point(eX, eY);

oldPoint = new Point(eX, eY);

}

}

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

{

isDrag = false;

ig = pictureBox1CreateGraphics();

igDrawRectangle(new Pen(ColorBlack, 1), startPointX, startPointY, eX - startPointX, eY - startPointY);

theRectangle = new Rectangle(startPointX, startPointY, eX - startPointX, eY - startPointY);

}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

{

Graphics g = thisCreateGraphics();

if (isDrag)

{

gDrawRectangle(new Pen(ColorBlack, 1), startPointX, startPointY, eX - startPointX, eY - startPointY);

}

}

private void Form1_MouseClick(object sender, MouseEventArgs e)

{

try

{

Graphics graphics = thisCreateGraphics();

Bitmap bitmap = new Bitmap(pictureBox1Image);

Bitmap cloneBitmap = bitmapClone(theRectangle, PixelFormatDontCare);

graphicsDrawImage(cloneBitmap, eX, eY);

Graphics g = pictureBox1CreateGraphics();

SolidBrush myBrush = new SolidBrush(ColorWhite);

gFillRectangle(myBrush, theRectangle);

}

catch

{ }

}

}

}

以上就是关于vs2005平台 如何获取picturebox图片中的图像坐标,即鼠标在图片上移动时显示鼠标当前的坐标,求c#代码全部的内容,包括:vs2005平台 如何获取picturebox图片中的图像坐标,即鼠标在图片上移动时显示鼠标当前的坐标,求c#代码、c#获取一个小图片在一个大的picturebox里面的左上角的位置坐标、C#怎么获得某个动态生成控件的坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9464283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存