想在已有的bmp图像上用鼠标绘图,例如绘制一个矩形,就像photoshop中的选择工具一样,不知如何实现?我现在可以绘出矩形,但是覆盖了要编辑的图像,代码如下:
====================================namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private string ChoFiNam;
        Image Img = null;
        private Size originalSize = new Size(0, 0);
        Rectangle curRect;
        double curZoom;        private bool IfMove;
        private bool firstMove = true;
        private Point StartPoint;
        private Point EndPoint;
        private Point MovingPoint;
        public Form1()
        {
            InitializeComponent();
        }        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            IfMove = true;
            StartPoint = new Point(e.X, e.Y);
        }        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            if (IfMove && e.Button == MouseButtons.Left)
            {
                if (firstMove)
                {
                    firstMove = false;
                    EndPoint = new Point(e.X, e.Y);
                    Brush b = Brushes.Blue;
                    Pen p = new Pen(b);
                    DrawRect(p, StartPoint, EndPoint);
                }
                else
                {                                      //通过利用新的颜色(背景色)重新绘制擦去先前已经画好的线
                    Color c = panel2.BackColor;
                    Pen p = new Pen(c);
                    DrawRect(p, StartPoint, EndPoint);
                    
                    //画新的线
                    MovingPoint = new Point(e.X, e.Y);
                    Brush b = Brushes.Blue;
                    p = new Pen(b);
                    DrawRect(p, StartPoint, MovingPoint);                    EndPoint = MovingPoint;
                }            }
        }        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            //通过利用新的颜色(背景色)重新绘制擦去先前已经画好的线
            Color c = panel2.BackColor;
            //Brush b = Brushes.Blue;
            Pen p = new Pen(c);
            DrawRect(p, StartPoint, EndPoint);
        }
        private void DrawRect(Pen p, Point startPoint, Point endPoint)
        {
            Graphics g = panel2.CreateGraphics();
            int Dwidth = Math.Abs((endPoint.X) - (startPoint.X));
            int DHeight = Math.Abs((endPoint.Y) - (startPoint.Y));
            //g.DrawLine(p, startPoint, endPoint);
            g.DrawRectangle(p, startPoint.X, startPoint.Y, Dwidth, DHeight);
        }
        private void segButton2_Click(object sender, EventArgs e) // 选择要编辑的图像
        {
            if (opFiDg2.ShowDialog() == DialogResult.OK)
            {
                ChoFiNam = opFiDg2.FileName;
                curZoom = 1.0;               // 初始值;                try
                {
                    Img = Image.FromFile(ChoFiNam);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
                panel2.AutoScrollMinSize = new Size
                    ((int)(Img.Width),
                    (int)(Img.Height));
                curRect = new Rectangle(0, 0, Img.Width, Img.Height);
                panel2.Invalidate();
                // Save origianl size of the imageOrWth, OrHit; 
                originalSize.Width = Img.Width;
                originalSize.Height = Img.Height;
            }
        }        private void panel2_Paint(object sender, PaintEventArgs e)// 绘制bmp图像
        {
            Graphics g = e.Graphics;
            if (Img != null)
            {
                g.DrawImage(Img, new Rectangle
                      (panel2.AutoScrollPosition.X,
                      panel2.AutoScrollPosition.Y,
                      (int)(curRect.Width * curZoom),
                      (int)(curRect.Height * curZoom)));            }
        }
    }
}

解决方案 »

  1.   

    以上是全部的代码,问题是: 绘制的矩形框会覆盖bmp图,不知要怎么处理?如果分层的话,具体要怎么实现?我看过http://www.codeproject.com/csharp/DrawTools.asp?df=100 里面的例子,俺是菜鸟,没有看懂,可否贴个简单一些的实例, 请各位大侠多多指教,多谢!!!分不多,多包涵
      

  2.   

    看你的意思,是否要做水印?如果是的话,参看这个例子要好一些
    http://www.codeproject.com/csharp/water.asp
      

  3.   

    我想lz是指所有画图软件里面的区域选择框吧,就是可以在图像上选一个区域然后做剪切,复制操作。
    以前用C++Builder和VB6的时候,就有一个叫Shape的控件,就是起这个作用,很方便的,但是.Net里面没有了。需要自己画。问了一个MS内部的人为什么这么做,会不会刷新上有问题,结果本人英语没听懂。郁闷。
    不过确实是要自己画。也的确是可以的,简单看了lz的代码(太长了),怎么没有用到Image的控件?
      

  4.   

    to:xxelement(未知元素) 

       您好,不知您说的“没有用到img控件”是什么意思啊?我的代码里面的img主要是用来绘制bmp图的,另外有些代码是为了放大缩小等功能做准备的,所以长了些