c#画图提问,图在下面

img


请问这么用c#画上面这张图啊,画图的时候不出线,代码写好了,就是不出现线,请问该怎么办啊,是用winform写吗

using System;
using System.Drawing;
using System.Windows.Forms;
namespace _1
{
    partial class Form1
    {
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Blue, 5);
            Graphics g = this.CreateGraphics();
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//实线
            g.DrawLine(p, 10, 40, 200, 40);
            g.Dispose();
            p.Dispose();

        }
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            button1 = new Button();
            button2 = new Button();
            SuspendLayout();
            // 
            // button1
            // 
            button1.BackColor = Color.Lime;
            button1.ForeColor = Color.Lime;
            button1.Location = new Point(150, 169);
            button1.Name = "button1";
            button1.Size = new Size(25, 27);
            button1.TabIndex = 0;
            button1.Text = "button1";
            button1.UseVisualStyleBackColor = false;
            // 
            // button2
            // 
            button2.BackColor = Color.Lime;
            button2.ForeColor = Color.Lime;
            button2.Location = new Point(267, 169);
            button2.Name = "button2";
            button2.Size = new Size(25, 27);
            button2.TabIndex = 1;
            button2.Text = "button2";
            button2.UseVisualStyleBackColor = false;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(9F, 20F);
            AutoScaleMode = AutoScaleMode.Font;
            BackColor = SystemColors.ControlLightLight;
            ClientSize = new Size(800, 450);
            Controls.Add(button2);
            Controls.Add(button1);
            ForeColor = SystemColors.ControlText;
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }
       
        #endregion

        private Button button1;
        private Button button2;
    }
}

测试没问题,就贴出来的代码来看,Form的Paint事件没加上


        private void InitializeComponent()
        {
            button1 = new Button();
            button2 = new Button();
            SuspendLayout();
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);//////

把你的画图的代码贴一下,看看哪里没画出来,是用 g.DrawLine 画的么

首先你应该把你的代码贴出来人家才能分析、另外你可以看看GPT提供的画线方法

以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:

是的,你可以用C#的WinForms来画这张图。在你的窗体上创建一个Paint事件处理程序,并在其中编写绘图逻辑。你可以使用Graphics对象来绘制直线、矩形和文本。

以下是一个简单的例子,它演示了如何使用Graphics对象在窗体上绘制一条直线:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    // 创建一个Pen对象,设置颜色和线宽
    Pen pen = new Pen(Color.Black, 2);

    // 使用Graphics对象绘制直线
    e.Graphics.DrawLine(pen, 20, 20, 100, 100);

    // 释放Pen对象
    pen.Dispose();
}

在这个例子中,我们在Paint事件处理程序中创建了一个Pen对象,并使用Graphics对象的DrawLine方法绘制了一条直线。最后,我们释放了Pen对象以释放资源。

你可以使用类似的方法来绘制矩形和文本。要绘制矩形,你可以使用Graphics对象的DrawRectangle方法。要绘制文本,你可以使用Graphics对象的DrawString方法。

希望这能帮助你开始在C#中绘制图形。

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/210084
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:C#winform数据绑定
  • 除此之外, 这篇博客: c# WinForm 多次点击这个按钮会弹出多个窗体, 怎么才能只显示一个窗体。解决方案!中的 第一种解决方法 “单例” 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 点击按钮弹出窗体

     private void toolStripLabel1_Click(object sender, EventArgs e)
            {
                AddStudent add=AddStudent.AddStudentCreateInstance();
                add.MdiParent=this;
                add.Show();
            }

    需要弹出的窗体实现

      private  AddStudent()
            {
                InitializeComponent();
            }
    
            
            private static AddStudent _addStudent=null;
            /// <summary>
            /// 判断 AddStudent是否被构造过,如果构造过,不构造,否则构造
            /// </summary>
            /// <returns></returns>
            public static AddStudent AddStudentCreateInstance()
            { 
                if(_addStudent==null || _addStudent.IsDisposed)
                    {
                    _addStudent=new AddStudent();
                    }
                return _addStudent;
             }

    第一步:把需要弹出的窗体的构造方法设置成“私有”

    第二步:声明一个以窗体命为 类型的 变量并且等于null

    第三步:写一个公开的静态方法,返回类型是窗体类名:

    3.1判断是否等于null,如果等于null就创建对象返回,否则直接返回

     

  • 您还可以看一下 武占文老师的C#WinForm图表控件应用详解课程中的 使用平均值、标准差和中值公式小节, 巩固相关知识点
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632