1

我正在开发一个显示 DICOM 文件的应用程序,现在我正在进入 MPR。是否有一个控件可以让您在图片框或面板中拥有像这样的一行:

http://www.vcreatelogic.com/docs/gcf-2.6.0/html/MPRView.jpg 目的是移动那条线并在移动时执行一些操作,所以我基本上需要某种控件或自定义控件(或者可能是自定义图标),这将允许用户在移动鼠标时看到他在图片框中的移动位置。

谢谢!

更新:这是我正在使用的代码:

在 Picturebox 的 Mouse Down 中:

 if (e.Clicks == 2)
        {

            horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
            horizontalend = new Point(picbox_mpr.Width, e.Y);//End point of Horizontal line.
            verticalstart = new Point(e.X, 0);//Start point of Vertical line
            verticalend = new Point(e.X, picbox_mpr.Height);//End point of Vertical line.
 }

然后在 MouseMove (我可以平移图片)中,我希望这些线条保持固定在绘制的位置..

更新:整个想法是如果用户双击图像来绘制线条,然后如果图像被移动,则将这些线条与图像一起移动。

4

2 回答 2

1

解决了!

这是代码:

//Variable declaration:
    private Point horizontalstart, horizontalend, verticalstart, verticalend, horizontalstart_initialpoint, horizontalend_initialpoint, verticalstart_initialpoint, verticalend_initialpoint;
    System.Drawing.Pen redline= new System.Drawing.Pen(System.Drawing.Color.Red);
    private Point redlinemovedpoints = Point.Empty;
    private Point redline_panstart = Point.Empty;


 void picbox_mpr_MouseDown(object sender, MouseEventArgs e)
    {
        redline_panstart = new Point(e.X, e.Y); //saves the point from where you pan the picture

 if (e.Clicks == 2) //if the user double clicks, then the red line is drawn in the PictureBox
        {
            horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
            horizontalend = new Point(picbox_mpr.Width, e.Y);//End point of Horizontal line.
            verticalstart = new Point(e.X, 0);//Start point of Vertical line
            verticalend = new Point(e.X, picbox_mpr.Height);//End point of Vertical line.

            //fixed reference points, this is used to store where the line was drawn initially.
            horizontalstart_initialpoint= horizontalstart;
            horizontalend_initialpoint = horizontalend;
            verticalstart_initialpoint = verticalstart;
            verticalend_initialpoint = verticalend;

            picbox_mpr.Invalidate(); // Draw the lines (see paint event at the bottom)
        }


 void picbox_mpr_MouseMove(object sender, MouseEventArgs e)
    {

 if (e.Button == MouseButtons.Left & pan) // don't worry about pan, is a variable I use to determine if the user is dragging the mouse.
        {

            redlinemovedpoints = new Point(e.Location.X - redline_panstart.X,
                    e.Location.Y - redline_panstart.Y); // this is the X and Y points move

            horizontalstart = new Point(horizontalstart_initialpoint.X + redlinemovedpoints.X, horizontalstart_initialpoint.Y+redlinemovedpoints.Y);
            horizontalend = new Point(picbox_mpr.Width, horizontalend_initialpoint.Y + redlinemovedpoints.Y);
            verticalstart = new Point(verticalstart_initialpoint.X + redlinemovedpoints.X, verticalstart_initialpoint.Y + redlinemovedpoints.Y);
            verticalend = new Point(verticalstart_initialpoint.X + redlinemovedpoints.X, picbox_mpr.Height);

            picbox_mpr.Invalidate(); //re draws the lines, this time moved X or Y depending the pan.

        }

}

void picbox_mpr_MouseUp(object sender, MouseEventArgs e)
    {

        //save the line points positions where after panning the image
        horizontalstart_initialpoint = horizontalstart;
        horizontalend_initialpoint = horizontalend;
        verticalstart_initialpoint = verticalstart;
        verticalend_initialpoint = verticalend;
     }

 private void picbox_mpr_Paint(object sender, PaintEventArgs e)
    {
        Image tmp = (Image)img.RenderImage(0);
        e.Graphics.Clear(System.Drawing.Color.Black);
        e.Graphics.DrawImage(tmp, movingPoint);
        tmp.Dispose();
        e.Graphics.DrawLine(redline, horizontalstart, horizontalend);//Draw Horizontal line.
        e.Graphics.DrawLine(redline, verticalstart, verticalend);//Draw Horizontal line.

     }
于 2013-09-04T17:49:33.353 回答
1

First things first,the method I am giving does not require a custom control in order to work,it makes use of two of the events provided by PictureBox to run the logic,but if still you need a custom control you should be able to do that by porting these methods to that control with a little coding.

The following are two fields that will hold most of the values in our logic,

private Point horizontalstart, horizontalend;
private Point verticalstart, verticalend;
private bool drawlines;//Specifies a value whether PictureBox should redraw itself.

Let me explain what are these points for;
The first Point statement holds starting and end point of Horizontal line,and
the second Point statement holds starting and end point of Vertical line.
When you'r done with it write this code in MouseMoveEvent of your PictureBox;

    private void SourcePictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if(drawlines==true)//If the lines should move.
        {
         horizontalstart = new Point(0, e.Y);//Start point of Horizontal line.
         horizontalend = new Point(SourcePictureBox.Width, e.Y);//End point of Horizontal line.
         verticalstart = new Point(e.X, 0);//Start point of Vertical line
         verticalend = new Point(e.X, SourcePictureBox.Height);//End point of Vertical line.
         SourcePictureBox.Invalidate();//Force PictureBox to repaint.
        }

        else if(drawlines==false)//To lock the lines at current coordinates.
        {
         //Add any code if needed.
        }
    }

where SourcePictureBox is the PictureBox that we are operating upon.As soon as the MouseMoveEvent is raised new coordinates for both lines are calculated and a repaint message is sent to PictureBox,which draws the lines using the coordinates declared and calculated above.

Ok so now write the code below in the Paint event of your PictureBox;

    private void SourcePictureBox_Paint(object sender, PaintEventArgs e)
    {
        if (SourcePictureBox.Image != null)
        {
            e.Graphics.DrawLine(SystemPens.WindowText, horizontalstart, horizontalend);//Draw Horizontal line.
            e.Graphics.DrawLine(SystemPens.WindowText, verticalstart, verticalend);//Draw Vertical line.
        }
    }

One more thing that i noticed in your image,the guidelines the image is showing are a little small than the image,if this is the case,then replace both the statements in MouseMoveEvent event excluding the Invalidate() one with the following;

        horizontalstart = new Point(0+10, e.Y);//Start point of Horizontal line.

        horizontalend = new Point(SourcePictureBox.Width-10, e.Y);//End point of Horizontal line.

        verticalstart = new Point(e.X, 0+10);//Start point of Vertical line

        verticalend = new Point(e.X, SourcePictureBox.Height-10);//End point of Vertical line.

But what's new in these statements,nothing except they increase the start point of both lines by value 10(int) and decrease the end points with value 10,thus giving you some space between the edges of PictureBox and the guiding lines.Please note if you want to increase or decrease the space replace value 10 with a value you would like but keep in mind the value must not be bigger than half of the size of your PictureBox.
The other thing i would like to address is if you want to change the colour of guiding lines,replace the first argument of both statements in Paint Event with any of values offered by class SystemPens.If there is nothing in the class that satisfies you,you are always free to create on for yourself.If you know how to do it,then its the best choice,incase if you need some help,take a look here.Creating a new pen gives you the option of defining a width for the guilding lines.

Update

`At anytime,to stop the lines from moving,set drawlines(declared above) to false,to resume their functionality set it to true.

It's not a foolproof method but I Hope it's enough to get your job done,and don't forget to rename the PictureBox and associated events with the name of your PictureBox.

于 2013-08-31T16:25:18.210 回答