我如何在图像框上撒娇 [英] how i can drwing on image box

查看:94
本文介绍了我如何在图像框上撒娇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制图像,但是当鼠标移出图像时,
因此删除或绘制我所画的线条

I''m drawing on an image, but when the mouse moves out of the image,
so delete or lines I''ve drawn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MapStore
{
    public partial class Form1 : Form
    {
        #region dailg
        OpenFileDialog OpenFiledialog;
        //FileDialog OpenFiledialog;
     
        #endregion

        #region drwing
        Graphics g;
        Pen p = new Pen(Color.Blue, 2);
        Point strart;
        Point end;

        #endregion
        #region enums
        enum Enum_drwing  
        {
            line,
            recntlage
        }
        #endregion


        Int32 var_draw_case;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
            OpenFiledialog.InitialDirectory =  "c:\\";
            OpenFiledialog.FilterIndex = 1;
            if (OpenFiledialog.ShowDialog() == DialogResult.OK )
            {
                pictureEdit1.Image = Image.FromFile(OpenFiledialog.FileName);

            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureEdit1_MouseMove(object sender, MouseEventArgs e)
        {
            


            if ((var_draw_case == (int)Enum_drwing.line) &&  (e.Button == MouseButtons.Left))
            {
                pictureEdit1.Cursor = Cursors.Cross;
                 

                ControlPaint.DrawReversibleLine(pictureEdit1.PointToScreen(strart), pictureEdit1.PointToScreen(end), Color.Black);

                end = new Point(e.X, e.Y);

                ControlPaint.DrawReversibleLine(pictureEdit1.PointToScreen(strart), pictureEdit1.PointToScreen(end), Color.Black);



            }


            else

            {

                strart = new Point();

            }

        }

     


 
        

        private void BUT_drawline_Click(object sender, EventArgs e)
        {
            var_draw_case = (int)Enum_drwing.line;
        }

        private void pictureEdit1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                if ((var_draw_case == (int)Enum_drwing.line))
                {
                    strart.X = e.X;
                    strart.Y = e.Y;

                }
                else
                {
                    strart = new Point();
                }
               
            }
        }
        
      
    }
}

推荐答案

Espens答案是最好的选择.

您现有的代码有很多错误,最好只是将其废弃然后重新开始.绘画应该在控件的Paint事件中完成,而不是在MouseMove中完成.

使用MouseMove可以很好地绘制橡皮筋线,但是释放鼠标按钮后,您将没有代码可将最后一条线绘制到位图图像上,该图像可在需要时用于重新绘制控件.

你为什么要这样?因为如果将另一种表单拖到您的表单上,则会丢失整个图像.一旦再次显示您的表单/控件,根本没有代码或源数据可以重新绘制它们.
Espens answer is the best you''re going to get.

There''s so much wrong with your existing code that its better just to scrap it and start over. Your painting should be done in your controls Paint event, not in a MouseMove.

Using MouseMove is fine for drawing the rubberband line, but once the mouse button is released, you have no code for drawing the final line to a bitmap image that can be used to repaint the control when needed.

Why do you this? Because if another form was dragged over yours, the entire image would be lost. There''s no code nor source data at all to repaint your form/controls once they are visible again.


这里是一篇可能引起您兴趣的文章:
OpenS-CAD,一个简单的2D CAD应用程序 [
Here is an article that might be of interest:
OpenS-CAD, a simple 2D CAD application[^]

Not an exact answer, but it does what you probably need to do. (and then some...)

Regards
Espen Harlinn


这篇关于我如何在图像框上撒娇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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