绘制用户定义的矩形

发布于 2024-09-26 14:22:23 字数 4016 浏览 5 评论 0原文

我当前的代码允许我从用户定义的位置绘制矩形,但不是按照我想要的方式绘制。我需要它就像你在绘画中所做的那样,这是我当前的代码:

namespace SimpleDraw2 { /// /// MainForm 的描述。 /// 公共部分类 MainForm :表单 { bool IsMouseDown = false; 鼠标指向位置; int DrawShape = 0; 位图存储图像;

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
        pictureBox1.Image = new Bitmap    (pictureBox1.Width,pictureBox1.Height);                      
        StoredImage =  new Bitmap(pictureBox1.Width,pictureBox1.Height);
    }

    void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        MousePosition = e.Location;
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        gStored.DrawImage(pictureBox1.Image, 0, 0);
    }

    void PictureBox1MouseUp(object sender, MouseEventArgs e)
    {
        IsMouseDown = false;
    }

    void PictureBox1MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        if (DrawShape == 0) 
        {
            Pen p = new Pen(Color.Red, 10);
            if (IsMouseDown) 
            {
                g.DrawLine(p,MousePosition,e.Location);
                MousePosition = e.Location;
            }
        }
        if (DrawShape == 1) 
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage,0,0);
            g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

        }
        if (DrawShape == 2)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y);
        }
        if (DrawShape == 3)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X);
        }
        //if (DrawShape == 4)
        //{
        //    g.Clear(Color.Transparent);
        //    g.DrawImage(StoredImage, 0, 0);
        //    g.DrawPolygon(Pens.Indigo, Point[] e.X);
        //}

        this.Refresh();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            axWindowsMediaPlayer1.URL = ofd.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
        Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
        Graphics gfx = Graphics.FromImage(bmp);
        gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.BackgroundImage = bmp;
        //axWindowsMediaPlayer1.Visible = false;
        //pictureBox1.Visible = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage);
        gg.Clear(Color.Transparent);
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        g.Clear(Color.Transparent);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        DrawShape = 1;

    }

    private void button6_Click(object sender, EventArgs e)
    {
        DrawShape = 2;
    }

    private void button8_Click(object sender, EventArgs e)
    {
       DrawShape = 3;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        DrawShape = 0;
    }
}

如果

有人可以帮助我编辑我的代码来解决问题,使拖拽系统变得简单,我将非常感激。

预先感

谢克里斯

My current code allows me to draw rectangles from a user defined spot but not in the way in whihc i desire. I need it to be like you would do it in paint, here is my current code:

namespace SimpleDraw2
{
///
/// Description of MainForm.
///
public partial class MainForm : Form
{
bool IsMouseDown = false;
Point MousePosition;
int DrawShape = 0;
Bitmap StoredImage;

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
        pictureBox1.Image = new Bitmap    (pictureBox1.Width,pictureBox1.Height);                      
        StoredImage =  new Bitmap(pictureBox1.Width,pictureBox1.Height);
    }

    void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        MousePosition = e.Location;
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        gStored.DrawImage(pictureBox1.Image, 0, 0);
    }

    void PictureBox1MouseUp(object sender, MouseEventArgs e)
    {
        IsMouseDown = false;
    }

    void PictureBox1MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        if (DrawShape == 0) 
        {
            Pen p = new Pen(Color.Red, 10);
            if (IsMouseDown) 
            {
                g.DrawLine(p,MousePosition,e.Location);
                MousePosition = e.Location;
            }
        }
        if (DrawShape == 1) 
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage,0,0);
            g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

        }
        if (DrawShape == 2)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y);
        }
        if (DrawShape == 3)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X);
        }
        //if (DrawShape == 4)
        //{
        //    g.Clear(Color.Transparent);
        //    g.DrawImage(StoredImage, 0, 0);
        //    g.DrawPolygon(Pens.Indigo, Point[] e.X);
        //}

        this.Refresh();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            axWindowsMediaPlayer1.URL = ofd.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
        Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
        Graphics gfx = Graphics.FromImage(bmp);
        gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.BackgroundImage = bmp;
        //axWindowsMediaPlayer1.Visible = false;
        //pictureBox1.Visible = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage);
        gg.Clear(Color.Transparent);
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        g.Clear(Color.Transparent);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        DrawShape = 1;

    }

    private void button6_Click(object sender, EventArgs e)
    {
        DrawShape = 2;
    }

    private void button8_Click(object sender, EventArgs e)
    {
       DrawShape = 3;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        DrawShape = 0;
    }
}

}

If someone could help me edit my code to iron out the issue to make it easy drag and draw system it would me much appreciate.

Thanks in Advance

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-10-03 14:22:23

来自 msdn

绘制由a指定的矩形
坐标对,宽度和
高度。

所以你的代码将无法工作:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

应该是这样的

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y));

From msdn:

Draws a rectangle specified by a
coordinate pair, a width, and a
height.

So your code won't work:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

Should be something like

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y));
半衾梦 2024-10-03 14:22:23

我看到的最大问题是您试图绘制鼠标事件。这意味着您的绘图将在您收到刷新事件时立即被擦除。

只能在 Paint 事件中绘制,而不能在鼠标事件中绘制。如果您希望应用程序通过鼠标事件进行绘制,请在鼠标事件中设置一个点、矩形或任何内容(就像您开始使用 IsMouseDown 所做的那样),使您要在 MouseMoved 事件中更改的区域无效,然后绘制您的矩形或 Paint 事件中的任何内容。

The biggest problem I see is that you're trying to draw in the mouse events. This means your drawing will be wiped away the instant you get a refresh event.

Only draw in Paint events, never in mouse events. If you want your app to draw as a result of mouse events, set a point, rectangle, or whatever in the mouse events (like you start to do with IsMouseDown), invalidate the area you want to change in your MouseMoved event, then draw your rectangle or whatever in your Paint event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文