| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3465 人关注过本帖
标题:关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的图像 ...
只看楼主 加入收藏
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
结帖率:50%
收藏
 问题点数:0 回复次数:7 
关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的图像还是消失了?
程序还没有写完。为什么每次最小化以后,以前所绘的图像都消失了,是paint事件错了吗?
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;

namespace shixun
{

    public partial class Form1 : Form
    {
        public int n;           //图形形状
        Point p1 = Point.Empty, p2 = Point.Empty;
        Color A=Color.Black, B;             //线条颜色,填充颜色
        bool moused = false;    //记下鼠标是否按下
        public ArrayList array = new ArrayList();    //重绘
        public int Xt=1;          //线条粗细
        int k;


        public Form1()
        {
            InitializeComponent();
        }
        private void radioButLine_CheckedChanged(object sender, EventArgs e)
        {
            n = 1;
        }

        private void radioButRecetangle_CheckedChanged(object sender, EventArgs e)
        {
            n = 2;
        }

        private void radioButEllipse_CheckedChanged(object sender, EventArgs e)
        {
            n = 3;
        }

        private void radioButRece_CheckedChanged(object sender, EventArgs e)
        {
            n = 4;
        }

        private void radioButEllip_CheckedChanged(object sender, EventArgs e)
        {
            n = 5;
        }


        private void 线条颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog Cdh = new ColorDialog();
            Cdh.ShowDialog();
            A = Cdh.Color;

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            switch (n)
            {
                case 1:
                    if (moused)
                    {
                        Graphics g = this.pictureBox1.CreateGraphics();
                        Pen pen = new Pen(Color.White, Xt);
                        g.DrawLine(pen, p1, p2);                  //清除前一根直线
                        p2.X = e.X; p2.Y = e.Y;
                        pen = new Pen(A, 1);
                        g.DrawLine(pen, p1, p2);
                        g.Dispose();
                    }
                    break;
                case 2:
                    if (moused)
                    {
                        Graphics g1 = this.pictureBox1.CreateGraphics();
                        Pen pen1 = new Pen(Color.White, Xt);
                        g1.DrawRectangle(pen1, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen2 = new Pen(A, 1);
                        g1.DrawRectangle(pen2, p1.X, p1.Y, b, c);
                        g1.Dispose();
                    }
                    break;
                case 3:
                    if (moused)
                    {
                        Graphics g1 = this.pictureBox1.CreateGraphics();
                        Pen pen3 = new Pen(Color.White, Xt);
                        g1.DrawRectangle(pen3, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen4 = new Pen(A, Xt);
                        g1.DrawRectangle(pen4, p1.X, p1.Y, b, c);
                        g1.Dispose();
                    }
                    break;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Cline ob = new Cline();
            Pen pen = new Pen(A);
            pen.Width = Xt;
            Graphics g = this.pictureBox1.CreateGraphics();
            switch (n)
            {
                case 1:
                    p2.X = e.X;
                    p2.Y = e.Y;
                    g.DrawLine(pen, p1, p2);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 1;
                    array.Add(ob);
                    break;
                case 2:
                    p2.X = e.X; p2.Y = e.Y;
                    float Rectangleb, Rectanglec;
                    Rectangleb = p2.X - p1.X;
                    Rectanglec = p2.Y - p1.Y;
                    g.DrawRectangle(pen, p1.X, p1.Y, Rectangleb, Rectanglec);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 2;
                    array.Add(ob);
                    break;
                case 3:
                    p2.X = e.X; p2.Y = e.Y;
                    float Roundb, Roundc;
                    Roundb = p2.X - p1.X;
                    Roundc = p2.Y - p1.Y;
                    g.DrawEllipse(pen, p1.X, p1.Y, Roundb, Roundc);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 3;
                    array.Add(ob);
                    break;
            }  
        }



        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            p1.X = e.X;
            p1.Y = e.Y;
            moused = true;
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            array.Clear();
            this.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.pictureBox1.CreateGraphics();
            Cline ob;
            for (int i = 0; i < array.Count; i++)
            {

                ob = (Cline)array[i];
                Pen pen = new Pen(ob.c);
                k = ob.k;
                pen.Width = ob.s;
                if (k == 1)
                {
                    g.DrawLine(pen, new Point(ob.x1, ob.y1), new Point(ob.x2, ob.y2));
                }
                if (k == 2)
                {
                    g.DrawRectangle(pen,
                    ob.x1, ob.y1,
                    ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
                if (k == 3)
                {
                    g.DrawEllipse(pen,
                    ob.x1, ob.y1,
                   ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
            }
        }



    }
    [Serializable()]
    public class Cline                       //记录所绘图形
    {
        public Color c;
        public float s;
        public int k;
        public int x1;
        public int y1;
        public int x2;
        public int y2;
    }
}
搜索更多相关主题的帖子: Point 
2010-12-26 11:17
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
好长的代码啊 很少有耐心看 程序LZ打个包也行啊 帮LZ调试一下 还得添控件太麻烦了
最小化图像消失 考虑一下重绘吧

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-26 11:45
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
收藏
得分:0 
回复 2楼 wangnannan
GDI+.rar (41.77 KB)
2010-12-26 12:23
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
首先,LZ是往PictrueBox上画线,而不是在pictrueBox 的图像上划线! 窗体大小变化相当于一次窗体的重绘.那么之前所有绘制的数据都会被清除.你需要在窗体的paint中重新绘制这些东西. 我试过在LZ的代码中加refresh 但是这样貌似效果不太好 闪烁很大 可以参照下用C#实现画板的做法 绘制在一张图片上
可以参照这样的代码
bitmap bm = new Bitmap(pictruebox1.Image);
Graphics g = Graphics.FromImage(bm);
g.DrawLine(Point)



出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-27 09:21
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
学习一下!!
2010-12-27 10:23
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
学习了一下,现在把代码贴出来(由于我的VS是08的)看看,没有细想,里面肯定还有地方可以再优化!!
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using namespace shixun
{

    public partial class Form1 : Form
    {
        public int n;           //图形形状
        Point p1 = Point.Empty, p2 = Point.Empty;
        Color A=Color.Black, B;             //线条颜色,填充颜色
        bool moused = false;    //记下鼠标是否按下
        public ArrayList array = new ArrayList();    //重绘
        public int Xt=1;          //线条粗细
        int k;
        Bitmap bm = null;
        Graphics g = null;

        public Form1()
        {
            InitializeComponent();
            bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bm);
        }
        private void radioButLine_CheckedChanged(object sender, EventArgs e)
        {
            n = 1;
        }

        private void radioButRecetangle_CheckedChanged(object sender, EventArgs e)
        {
            n = 2;
        }

        private void radioButEllipse_CheckedChanged(object sender, EventArgs e)
        {
            n = 3;
        }

        private void radioButRece_CheckedChanged(object sender, EventArgs e)
        {
            n = 4;
        }

        private void radioButEllip_CheckedChanged(object sender, EventArgs e)
        {
            n = 5;
        }


        private void 线条颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog Cdh = new ColorDialog();
            Cdh.ShowDialog();
            A = Cdh.Color;

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            switch (n)
            {
                case 1:
                    if (moused)
                    {
                        Graphics g = this.pictureBox1.CreateGraphics();
                        Pen pen = new Pen(Color.White, Xt);
                      
                        //g.DrawLine(Point);

                        g.DrawLine(pen, p1, p2);                  //清除前一根直线
                        p2.X = e.X; p2.Y = e.Y;
                        pen = new Pen(A, 1);
                        g.DrawLine(pen, p1, p2);
                        g.Dispose();
                    }
                    break;
                case 2:
                    if (moused)
                    {
                        Graphics g = this.pictureBox1.CreateGraphics();
                        Pen pen1 = new Pen(Color.White, Xt);
                        g.DrawRectangle(pen1, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen2 = new Pen(A, 1);
                        g.DrawRectangle(pen2, p1.X, p1.Y, b, c);
                        g.Dispose();
                    }
                    break;
                case 3:
                    if (moused)
                    {
                        Graphics g = this.pictureBox1.CreateGraphics();
                        Pen pen3 = new Pen(Color.White, Xt);
                        g.DrawRectangle(pen3, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen4 = new Pen(A, Xt);
                        g.DrawRectangle(pen4, p1.X, p1.Y, b, c);
                        g.Dispose();
                    }
                    break;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Cline ob = new Cline();
            Pen pen = new Pen(A);
            pen.Width = Xt;
            //Graphics g = this.pictureBox1.CreateGraphics();
            switch (n)
            {
                case 1:
                    p2.X = e.X;
                    p2.Y = e.Y;
                    g.DrawLine(pen, p1, p2);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 1;
                    array.Add(ob);
                    break;
                case 2:
                    p2.X = e.X; p2.Y = e.Y;
                    float Rectangleb, Rectanglec;
                    Rectangleb = p2.X - p1.X;
                    Rectanglec = p2.Y - p1.Y;
                    g.DrawRectangle(pen, p1.X, p1.Y, Rectangleb, Rectanglec);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 2;
                    array.Add(ob);
                    break;
                case 3:
                    p2.X = e.X; p2.Y = e.Y;
                    float Roundb, Roundc;
                    Roundb = p2.X - p1.X;
                    Roundc = p2.Y - p1.Y;
                    g.DrawEllipse(pen, p1.X, p1.Y, Roundb, Roundc);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 3;
                    array.Add(ob);
                    break;
            }
            pictureBox1.Refresh();//刷新窗口,这个很重要
        }



        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            p1.X = e.X;
            p1.Y = e.Y;
            moused = true;
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            array.Clear();
            this.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = this.pictureBox1.CreateGraphics();
            Cline ob;
            for (int i = 0; i < array.Count; i++)
            {

                ob = (Cline)array[i];
                Pen pen = new Pen(ob.c);
                k = ob.k;
                pen.Width = ob.s;
                if (k == 1)
                {
                    g.DrawLine(pen, new Point(ob.x1, ob.y1), new Point(ob.x2, ob.y2));
                }
                if (k == 2)
                {
                    g.DrawRectangle(pen,
                    ob.x1, ob.y1,
                    ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
                if (k == 3)
                {
                    g.DrawEllipse(pen,
                    ob.x1, ob.y1,
                   ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            array.Clear();
            int l = 0;
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
            string FileName = dlg.FileName;
            if (FileName == "")
            {
                return;
            }
            FileStream fs = File.OpenRead(FileName);
            Bitmap image = new Bitmap(FileName);
            pictureBox1.Image = image;
           
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            pictureBox1.Image = (Image)bm;
        }

    }
      
    [Serializable()]
    public class Cline                       //记录所绘图形
    {
        public Color c;
        public float s;
        public int k;
        public int x1;
        public int y1;
        public int x2;
        public int y2;
    }
}
2010-12-27 11:09
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
收藏
得分:0 
回复 6楼 c1_wangyf
为什么我一加那个pictureBox1.Refresh();语句,直线怎么就画不上了呢?(代码和你的一样)如果删除后,最小化以后,还是直线没有了。
还有我把那个绘图换到form1上,然后,然后在private void Form1_Paint(object sender, PaintEventArgs e)中加入picturebox1_paint事件中同样的代码,结果最小化以后图像还是会在。
为什么换到picturebox1中,就不行了呢?
2010-12-27 12:55
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
我不知道你的代码是我的那部分拷贝过去的还是只把我加注释的那部分加上了,因为我在上面也做了一些修改,建议把代码全拷进去。

因为是在picture1里面的画布上画图,在画的过程中移动直线的终点时可能出现把其他线“擦除”的现象,所以用pictureBox1.Refresh();把图片框刷新一下....
2010-12-27 20:12
快速回复:关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054183 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved