(C#)用PEN画线

(C#)用PEN画线

复制代码

//实现功能:用创建的PEN画线。

//(本例使用using创建Pen.其好处在于,在对象(pen)超出作用域时,using结构会自动调用Dispose(),将PEN删除。

// 若用其它方法创建绘图对象(一般都很费资源),则一定要显式调用Dispose() ,如:

// " Graphics g = this.CreateGraphics();

//   ........

//   ........

//  g.Dispose(); "

//要点:

//   创建Pen, Pen的颜色与粗细  获取客户区的height与Width

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace myDrawPanA

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        protected override void OnPaint(PaintEventArgs e)

        {

            //base.OnPaint(e);

            Graphics g = e.Graphics;

            using (Pen myPen = new Pen(Color.Red,1))

            {

                if (ClientRectangle.Height/10>0)

                {

                    for (int y=0; y< ClientRectangle.Height; y += ClientRectangle.Height/10)

                    {

                        g.DrawLine(myPen,new Point(0,0), new Point(ClientRectangle.Width,y));

                    }

                }

            }

        }

 

 

    }

}

复制代码

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用Bitmap类来在图像上画线。根据引用\[1\]的代码,可以看到在button2_Click事件中,首先判断pictureBox1.Image是否为空,然后创建一个Bitmap对象bt,并从该对象中获取Graphics对象g。接下来,使用g.DrawLine方法来画线,通过指定起点和终点的坐标来确定线的位置和长度。在这个例子中,使用了四个g.DrawLine方法来画出一个十字形。最后,将修改后的Bitmap对象赋值给pictureBox1.Image,以更新显示的图像。 引用\[1\]: ```csharp private void button2_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { Bitmap bt = new Bitmap(pictureBox1.Image); Graphics g = Graphics.FromImage(bt); g.DrawLine(new Pen(Color.Red, 40), new Point(0, bt.Height / 2), new Point(bt.Width, bt.Height / 2)); g.DrawLine(new Pen(Color.Red, 40), new Point(bt.Width / 2, 0), new Point(bt.Width / 2, bt.Height)); g.DrawLine(new Pen(Color.Red, 40), new Point(0, 0), new Point(bt.Width, bt.Height)); g.DrawLine(new Pen(Color.Red, 40), new Point(0, bt.Height), new Point(bt.Width, 0)); pictureBox1.Image = bt; } } ``` 如果想要在其他地方画线,可以使用类似的方法。首先创建一个Bitmap对象,然后获取Graphics对象,最后使用g.DrawLine方法来画线。可以根据需要指定不同的起点和终点坐标,以及线的颜色和宽度。 希望这个回答对您有帮助! #### 引用[.reference_title] - *1* *3* [C#在图片上画线](https://blog.csdn.net/biyusr/article/details/95634254)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [用C# Bitmap作为画布写个3D软渲染器](https://blog.csdn.net/linjf520/article/details/96047240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值