C#在图片上画线

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace drawLine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }
        }

        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;
            }
        }
    }
}
 

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、付费专栏及课程。

余额充值