求助,winform窗体显示时绘图用哪个事件?

u011021100 2016-08-31 10:20:44
想让winform在显示时按照指定参数绘制图像,可是把绘图方法写在load和shown事件里都不行,可能是因为窗体还没显示出来,请问写在哪里比较合适,我的目的是让窗体展示出来的时候就已经绘制好图了。
...全文
239 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011021100 2016-09-05
  • 打赏
  • 举报
回复
引用 6 楼 yuankaiwsl 的回复:
你试的都不对,试试这段代码
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    DrawSquare(e.Graphics);
}
private void DrawSquare(Graphics g)
{
    string[] split = "10,10,90,90".Split(',');
    p_start.X = int.Parse(split[0]);
    p_start.Y = int.Parse(split[1]);
    p_end.X = int.Parse(split[2]);
    p_end.Y = int.Parse(split[3]);
    try
    {
        //绘制矩形
        if (p_end.X < p_start.X)
        {
            int a = p_end.X;
            p_end.X = p_start.X;
            p_start.X = a;
        }
        if (p_end.Y < p_start.Y)
        {
            int a = p_end.Y;
            p_end.Y = p_start.Y;
            p_start.Y = a;
        }
        //g = pictureBox1.CreateGraphics();
        //g.DrawImage(pictureBox1.Image, 0, 0);
        Pen pen = new Pen(Color.Red, 2);
        g.DrawRectangle(pen, p_start.X, p_start.Y, p_end.X - p_start.X, p_end.Y - p_start.Y);
    }
    catch
    {
        throw;
    }
}
谢谢哥。我好像理解一些了。
巴士上的邂逅 2016-09-02
  • 打赏
  • 举报
回复
你试的都不对,试试这段代码
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    DrawSquare(e.Graphics);
}
private void DrawSquare(Graphics g)
{
    string[] split = "10,10,90,90".Split(',');
    p_start.X = int.Parse(split[0]);
    p_start.Y = int.Parse(split[1]);
    p_end.X = int.Parse(split[2]);
    p_end.Y = int.Parse(split[3]);
    try
    {
        //绘制矩形
        if (p_end.X < p_start.X)
        {
            int a = p_end.X;
            p_end.X = p_start.X;
            p_start.X = a;
        }
        if (p_end.Y < p_start.Y)
        {
            int a = p_end.Y;
            p_end.Y = p_start.Y;
            p_start.Y = a;
        }
        //g = pictureBox1.CreateGraphics();
        //g.DrawImage(pictureBox1.Image, 0, 0);
        Pen pen = new Pen(Color.Red, 2);
        g.DrawRectangle(pen, p_start.X, p_start.Y, p_end.X - p_start.X, p_end.Y - p_start.Y);
    }
    catch
    {
        throw;
    }
}
u011021100 2016-09-02
  • 打赏
  • 举报
回复
都试过啊。不行啊。
巴士上的邂逅 2016-09-01
  • 打赏
  • 举报
回复
你这是要画在pictureBox1上吗?要用pictureBox1的Paint事件
u011021100 2016-09-01
  • 打赏
  • 举报
回复
在form的paint事件里写了,也不行。 //以特定的值来初始化红框 if (!string.IsNullOrEmpty(location[position])) { string[] split = location[position].Split(','); p_start.X = int.Parse(split[0]); p_start.Y = int.Parse(split[1]); p_end.X = int.Parse(split[2]); p_end.Y = int.Parse(split[3]); DrawSquare(); } private void DrawSquare() { try { //绘制矩形 if (p_end.X < p_start.X) { int a = p_end.X; p_end.X = p_start.X; p_start.X = a; } if (p_end.Y < p_start.Y) { int a = p_end.Y; p_end.Y = p_start.Y; p_start.Y = a; } g = pictureBox1.CreateGraphics(); g.DrawImage(pictureBox1.Image, 0, 0); Pen pen = new Pen(Color.Red, 2); g.DrawRectangle(pen, p_start.X, p_start.Y, p_end.X - p_start.X, p_end.Y - p_start.Y); } catch { } } 结果显示出来的时候还是没有红色矩形
xuzuning 2016-08-31
  • 打赏
  • 举报
回复
绘制图像 都要在 Paint 事件里进行 古来有之,并没有因技术的发展而改变
巴士上的邂逅 2016-08-31
  • 打赏
  • 举报
回复
显示的时候才会绘制,不显示不会绘制 在Paint事件里试试

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧