使用GDI+绘制矩形框——winform

1、创建一个 Graphics object

2、使用这个Graphics object 画线,画矩形,画图片等

具体代码

 

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Rectangle RcDraw;
        private float PenWidth = 5;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            RcDraw.X = e.X;
            RcDraw.Y = e.Y;

        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            // Determine the width and height of the rectangle...

            if (e.X < RcDraw.X)
            {
                RcDraw.Width = RcDraw.X - e.X;
                RcDraw.X = e.X;
            }
            else
            {
                RcDraw.Width = e.X - RcDraw.X;
            }

            if (e.Y < RcDraw.Y)
            {
                RcDraw.Height = RcDraw.Y - e.Y;
                RcDraw.Y = e.Y;
            }
            else
            {
                RcDraw.Height = e.Y - RcDraw.Y;
            }

            // Force a repaint of the region occupied by the rectangle...

            this.Invalidate(RcDraw);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Create image.
            Image imageFile = Image.FromFile(@"D:\MFCCode\WindowsFormsApp1\WindowsFormsApp1\10.jpg");

            // Create graphics object for alteration.
            Graphics newGraphics = Graphics.FromImage(imageFile);

            // Alter image.
            newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);

            // Draw image to screen.
            e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));

            // Dispose of graphics object.
            newGraphics.Dispose();

            // Draw the rectangle...
            e.Graphics.DrawRectangle(new Pen(Color.Blue, PenWidth), RcDraw);

        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值