单击图片框上的画线 [英] DrawLines over picturebox Image on mouse click

查看:67
本文介绍了单击图片框上的画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我在图片框的图像上单击鼠标(pictureBox1_MouseClick事件)时画了线,但是在图像上单击两次后,图像消失了.

下面的代码是画线,但在空白的图片框上,因为单击两次后图像逐渐消失....

如果还有另一种方法或对代码进行一些修改,请帮助我.....

Dear Friends,

I am drawing lines on mouse click(pictureBox1_MouseClick event) over an image in picturebox ,but after two clicks over image ,the image get disappears.

Below code is drawing lines but over a blank picturebox because image is getting disappear after two clicks....

Please help me if there is another way to do this or some modification in this code.....

namespace ImageSelection
{
    public partial class Form2 : Form
    {
        int iNumberofClicks = 0;
        Point[] pointArray = new Point[100];
        public Form2()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            pointArray[iNumberofClicks].X = e.X;
            pointArray[iNumberofClicks].Y = e.Y;
            Pen PenSpikes = new Pen(Color.Green);
            SolidBrush solidBrush = new SolidBrush(Color.Blue);
            iNumberofClicks++;
            if (iNumberofClicks > 1)
            {
                Point[] CurrentpointArray = new Point[iNumberofClicks];

                for (int i = 0; i < iNumberofClicks; i++)
                {
                    CurrentpointArray[i].X = pointArray[i].X;
                    CurrentpointArray[i].Y = pointArray[i].Y;
                }

                Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                Graphics offScreenDC = Graphics.FromImage(canvas);
                offScreenDC.DrawLines(PenSpikes, CurrentpointArray);
            
                pictureBox1.Image = canvas;
                offScreenDC.Dispose();
            }
            Point mousePnt = PointToClient(Cursor.Position);
            label2.Visible = true;
            label2.Text = mousePnt.ToString();
        }

        
    }
}




在此先感谢.......




Thanks in advance.......

推荐答案

亲爱的,您必须使用三种方法,如

Hi Dear, You have to three methods as like under

private void PicCapPhoto_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
      {

          try
          {
              if (e.Button == System.Windows.Forms.MouseButtons.Left)
              {
                  cropX = e.X;
                  cropY = e.Y;

                  cropPen = new Pen(cropPenColor, cropPenSize);
                  cropPen.DashStyle = DashStyle.DashDotDot;
                  Cursor = Cursors.Cross;

              }
              picCapture.Refresh();
          }
          catch (Exception ex)
          {
          }

      }

      private void PicCapPhoto_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
      {

          try
          {
              if (PicCapPhoto.Image == null)
                  return;


              if (e.Button == System.Windows.Forms.MouseButtons.Left)
              {
                  PicCapPhoto.Refresh();
                  cropWidth = e.X - cropX;
                  cropHeight = e.Y - cropY;
                  PicCapPhoto.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
              }

          }
          catch (Exception ex)
          {

              return;
          }
      }

      private void PicCapPhoto_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
      {
          try
          {
              Cursor = Cursors.Default;
              if (cropWidth < 1)
              {
                  return;
              }
              Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);

              Bitmap bit = new Bitmap(PicCapPhoto.Image, PicCapPhoto.Width, PicCapPhoto.Height);

              cropBitmap = new Bitmap(cropWidth, cropHeight);
              Graphics g = Graphics.FromImage(cropBitmap);
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
              g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
              g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
              g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);

              msnew = new MemoryStream();
              cropBitmap.Save(msnew, System.Drawing.Imaging.ImageFormat.Jpeg);

              PictureBox1.Image = cropBitmap;

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message.ToString(), "Error in Generate photo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

          }

      }



会很好用的.

Mahesh Patel
MCA-2012
09924625962



It Will Work fine.

Mahesh Patel
MCA-2012
09924625962


图片消失了,因为第二次单击时您生成了新的位图并将其分配给pictureBox:
Image disapears because on second click you generate new bitmap and assign it to pictureBox:
pictureBox1.Image = canvas;


覆盖"原始图像.换句话说,您不是在现有图像上绘制线条,而是在空白图像上绘制.

提取使用:


''overwriting'' the original image. In another words, you''re not drawing lines on existing image, you are drawing on blank image.

To draw over use:

Graphics offScreenDC = Graphics.FromImage(pictureBox1.Image);
...
offScreenDC.Dispose();
pictureBox1.Refresh();



您不需要Bitmap canvas,我也不明白为什么要将pointArray复制到CurrentpointArray-您可以直接使用pointArray.



You don''t need Bitmap canvas and I don''t understand why do you copy pointArray to CurrentpointArray - you can use pointArray directly.


您好,

这行:

Hi,

This line:

Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);



创建一个新图像.如果图片框中有一个预先存在的图像,则在第二次单击后将显示此图像,请注意:



Creates a new image. If there''s a pre-existing image in the picture box, this will after the second click, note:

if (iNumberofClicks > 1)



创建一个新的空白图像.这意味着写回图片框的图像是新的,应该只包含您的行.

如果在现有图像上绘制,则需要存储基本图像,并在每次添加新图像时重新绘制所有线条.或更有效地,只需担心最新的行,而仅将单行添加到现有图像即可.

这是您的代码更新.



create a new blank image. This means the image written back to the picture box is new and should contain only your lines.

If drawing over an existing image you''ll either need to store the base image and redraw all the lines each time a new one is added. Or more efficiently, only worry about the latest line keep adding just single lines to the existing image.

Here is your code updated.

namespace ImageSelection
{
    public partial class Form2 : Form
    {

        Point lastPoint = null;
        public Form2()
        {
            InitializeComponent();
        }
 
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Point Currentpoint = new Point();
            Currentpoint.X = e.X;
            Currentpoint.Y = e.Y;

            if (lastPoint != null)
            {
 
                Bitmap canvas = pictureBox1.Image;
                Graphics offScreenDC = Graphics.FromImage(canvas);
                offScreenDC.DrawLine(PenSpikes, lastPoint, Currentpoint);
            
                pictureBox1.Image = canvas;
                offScreenDC.Dispose();
            }

            lastPoint = Currentpoint;

        }
 
        
    }
}


这篇关于单击图片框上的画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆