有没有朋友知道,如何让图像出现在 picturebox上点击鼠标的位置

解决方案 »

  1.   

            Bitmap bt = new Bitmap(@"c:\1.jpg");
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                Graphics g = pictureBox1.CreateGraphics();
                g.DrawImage(bt, e.Location);
                g.Dispose();        }
      

  2.   

    但是~~! private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                Color color;
                Graphics paper = pictureBoxshow.CreateGraphics();            if (radioButtonCircle.Checked)
                {
                    r = radioButtonCircle;
                    string circle = radioButtonCircle.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;
                    }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {                    if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }                }
                }
                else if (radioButtonsquare.Checked)
                {
                    r = radioButtonsquare;
                    string square = radioButtonsquare.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;
                    }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {
                        if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }
                    }            }
                else
                {
                    r = radioButtontriangle;
                    string triangle = radioButtontriangle.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;                }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {
                        if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }                }
                    paper.Dispose();
                    int a = Convert.ToInt32(paper.DpiX);
                    int b = Convert.ToInt32(paper.DpiY);
                    Shape shapeselect = new Shape(a, b, trackBarsize.Value, trackBarsize.Value, color);
                    shapeselect.Draw(paper, r.Text);仍然不可实现阿??为什么呢??
      

  3.   

    这个不使用mouseclick吗???mouseclick 和mousedown怎么区别呢???
      

  4.   

    private void pictureBox1_Click(object sender, EventArgs e)
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    MouseEventArgs e中包含坐标信息。
      

  5.   

    也就是说,我还需要加一个rivate void pictureBox1_Click(object sender, EventArgs e) ???
    把int a = Convert.ToInt32(paper.DpiX);
                    int b = Convert.ToInt32(paper.DpiY)
    换成了 int a = Convert.ToInt32(e.X);
                    int b = Convert.ToInt32(e.Y );
    为什么还不行呢???(太谢谢你了)
      

  6.   

    朋友们帮帮忙吧,我明天要把project呈上的5555555555555555
      

  7.   

                    paper.Dispose(); //这行应该在后面吧?
                    int a = Convert.ToInt32(paper.DpiX);
                    int b = Convert.ToInt32(paper.DpiY);
                    Shape shapeselect = new Shape(a, b, trackBarsize.Value, trackBarsize.Value, color);
                    shapeselect.Draw(paper, r.Text);//shapeselect没有显示吧
      

  8.   

    --也就是说,我还需要加一个rivate void pictureBox1_Click(object sender, EventArgs e) ??? 
    private void pictureBox1_Click(object sender, EventArgs e)这行不需要。
      

  9.   

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                Color color;
                Graphics paper = pictureBoxshow.CreateGraphics();            if (radioButtonCircle.Checked)
                {
                    r = radioButtonCircle;
                    string circle = radioButtonCircle.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;
                    }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {                    if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }                }
                }
                else if (radioButtonsquare.Checked)
                {
                    r = radioButtonsquare;
                    string square = radioButtonsquare.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;
                    }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {
                        if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }
                    }            }
                else
                {
                    r = radioButtontriangle;
                    string triangle = radioButtontriangle.Text;
                    if (radioButtonRed.Checked)
                    {
                        color = Color.Red;                }
                    else if (radioButtonGreen.Checked)
                    {
                        color = Color.Green;
                    }
                    else
                    {
                        color = Color.Blue;
                    }
                    if (radioButtonother.Checked)
                    {
                        if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            color = colorDialog1.Color;
                        }                }
                   
                    int a = Convert.ToInt32(e.X);
                    int b = Convert.ToInt32(e.Y );
                    Shape shapeselect = new Shape(a,b, trackBarsize.Value, trackBarsize.Value, color);
                    shapeselect.Draw(paper, r.Text);
                    paper.Dispose();
    我移到后面了,可是,picturebox照样雷打不动的不给我在鼠标点位置显示shape ~~!
      

  10.   

    就我前面那一大串的if statement的情况,在后面写code去画shape不应该这样写吗??
      

  11.   

    Shape shapeselect = new Shape(a, b, trackBarsize.Value, trackBarsize.Value, color);  
    是wpf程序?
      

  12.   

    我写了一个关于shape的class ,就是要求shapa (x_,y_,w_,h_,color),所以我可以在form1 call  Shape shapeselectet
      

  13.   

    Shape shapeselect = new Shape(a,b, trackBarsize.Value, trackBarsize.Value, color);
    shapeselect.Draw(paper, r.Text); 
    替换成
    paper.DrawString(new Pen(r.Text, new Font("Arial", 16), new SolidBrush(color), new PointF(a, b));
      

  14.   

    Error 1 'System.Drawing.Pen' does not contain a constructor that takes '4' arguments \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 34 WindowsFormsApplication2
    Error 2 No overload for method 'DrawString' takes '1' arguments \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 17 WindowsFormsApplication2
      

  15.   

    paper.DrawString(new Pen(r.Text, new Font("Arial", 16), new SolidBrush(color), new PointF(a, b)); 
      

  16.   

    Error 1 Unexpected character ')' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 48 WindowsFormsApplication2
    Error 2 The best overloaded method match for 'System.Drawing.Pen.Pen(System.Drawing.Brush)' has some invalid arguments \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 34 WindowsFormsApplication2
    Error 3 Argument '1': cannot convert from 'string' to 'System.Drawing.Brush' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 42 WindowsFormsApplication2
    Error 4 No overload for method 'DrawString' takes '1' arguments \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 17 WindowsFormsApplication2
    Error 5 Only assignment, call, increment, decrement, and new object expressions can be used as a statement \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 44 WindowsFormsApplication2
    Error 6 Only assignment, call, increment, decrement, and new object expressions can be used as a statement \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 48 WindowsFormsApplication2
    Error 7 Only assignment, call, increment, decrement, and new object expressions can be used as a statement \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 71 WindowsFormsApplication2
    Error 8 Only assignment, call, increment, decrement, and new object expressions can be used as a statement \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 94 WindowsFormsApplication2
    Error 9 Only assignment, call, increment, decrement, and new object expressions can be used as a statement \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 112 WindowsFormsApplication2
    Error 10 Invalid expression term '' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 48 WindowsFormsApplication2
    Error 11 ) expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 48 WindowsFormsApplication2
    Error 12 Invalid expression term ',' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 49 WindowsFormsApplication2
    Error 13 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 49 WindowsFormsApplication2
    Error 14 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 51 WindowsFormsApplication2
    Error 15 Invalid expression term ',' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 72 WindowsFormsApplication2
    Error 16 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 72 WindowsFormsApplication2
    Error 17 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 74 WindowsFormsApplication2
    Error 18 Invalid expression term ',' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 95 WindowsFormsApplication2
    Error 19 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 95 WindowsFormsApplication2
    Error 20 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 97 WindowsFormsApplication2
    Error 21 Invalid expression term ')' \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 113 WindowsFormsApplication2
    Error 22 ; expected \\oslo.scms.waikato.ac.nz\home\fz48\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 424 113 WindowsFormsApplication2
      

  17.   

    这是我做的例子,在Form1中拖入一个pictureBox,并指定其MouseDown事件处理程序:
        public partial class Form1 : Form
        {
            Bitmap bt = new Bitmap(@"c:\1.jpg");
            public Form1()
            {
                InitializeComponent();
            }        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                Graphics g = pictureBox1.CreateGraphics();
                int a = 100, b = 10;
                g.DrawString(Text, new Font("Arial", 16), new SolidBrush(Color.FromArgb(0, 255, 0)), new PointF(e.X, e.Y));//写字
                //g.DrawImage(bt, e.Location);//画图
                g.Dispose();
                        }
        }
      

  18.   

    因为,我在class里是 通过if(shape=="circle")
    而且 我的r= radiobutton 称之为 "cicle"
    所以,r.text=circle,
    所以会判别shape=circle执行画圆
      

  19.   

    哦,理解错了。
    shapeselect.Draw里面怎么写的?
      

  20.   

     Shape shape = new Shape(rand.Next(0, pictureBoxshow.Width - trackBarsize.Value), rand.Next(0, pictureBoxshow.Height - trackBarsize.Value), trackBarsize.Value, trackBarsize.Value, color);
                shape.Draw(paper, r.Text);这个市我的随即draw 
      

  21.   

    private void pictureBoxshow_MouseClick_1(object sender, MouseEventArgs e)
            {
    Shape shape = new Shape(MousePosition .X - pictureBoxshow.Width, MousePosition .Y - pictureBoxshow .Height , trackBarsize.Value, trackBarsize.Value, color);
                shape.Draw(paper, r.Text);我通过这个 倒是可以在picturebox画图了,但是。图的位置总有偏差。。如果我写成Shape shape = new Shape(MousePosition .X  , MousePosition .Y , trackBarsize.Value, trackBarsize.Value, color);就 什么都划不出来了。。
      

  22.   

    MousePosition是相对于窗体的坐标,所以要剪掉picturebox的起点坐标,才能得到相对于picturebox的相对坐标,这个可能还要减去picturebox的margin和padging。用MouseDown事件中MouseEventArgs e的e.Location是最准确的
      

  23.   

    如果我写成Shape shape = new Shape(MousePosition .X  , MousePosition .Y , trackBarsize.Value, trackBarsize.Value, color);就 什么都划不出来了。。那是画到picturebox外面去了。
      

  24.   

    超级感谢大哥阿!!!!!!!!!!!!!!!!!!!!!!完成了~~~~~~~~~
     Shape shape = new Shape(e.X, e.Y, trackBarsize.Value, trackBarsize.Value, color);
                shape.Draw(paper, r.Text);
    就这么一句话+ 
    private void pictureBoxshow_MouseClick(object sender, MouseEventArgs e)
    我从早上8点到实验室一直折腾到下午5点。。太感谢了!!!!!!!!!!0210549147我的qq 太谢谢大哥了!!!!!!看来这个课程不会挂掉了~~这个课程的fail率至少60%。