C#桌面程序设计复习

1.窗体界面设计

//屏幕高度
            int ScreenH = 1080;
            this.Location = new Point(this.Location.X, ScreenH - this.Height - 20);
//关闭本窗体,打开主窗体
            this.Hide();
            FrmQQMain frmQQMain = new FrmQQMain();
            frmQQMain.ShowDialog();

2.鼠标事件

 //鼠标进入时,变成红色
            label1.ForeColor = Color.Red;
 //鼠标离开时,背景变透明
            pictureBox1.BackColor = Color.Transparent;
 //颜色浅灰半透明
            picShrink.BackColor = Color.FromArgb(50,200,200,200);
//鼠标进入时,切换另一张图片
            picLogin.BackgroundImage = Properties.Resources.iconqq;
//鼠标拖动窗体方法
        bool isDrag;
        Point ClickPoint;
        private void Frm1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrag = true;
            ClickPoint = new Point(e.X ,e.Y );
        }

        private void Frm1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag == true)
                this.Location = new Point(this .Location .X +e.X -ClickPoint .X 
                    ,this .Location .Y +e.Y -ClickPoint .Y );
            ClickPoint.X = e.X;
            ClickPoint.Y = e.Y;
        }

        private void Frm1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
        }

3.无法拒绝的辞职按钮

//随机出现按钮方法
            Random r = new Random();
            button2.Location = new Point(r.Next ()%(this .Width -button2 .Width )
                ,r .Next ()%(this.ClientSize .Height  -button2 .Height ));

4.C#图片操作

//打开资源管理器(click事件中)
           OpenFileDialog ofd=new openfiledialog();//初始化打开文件夹类
           ofd.ShowDialog();//显示资源管理器
//显示图片
(click事件外)image imgOriginal;//定义一张图片
(click事件里) string strFileName = ofd.FileName;//获取该位置的文件名
            imgOriginal = Image.FromFile(strFileName);//获取选取的图片
            pictureBox1.BackgroundImage = imgOriginal;//显示图片

//图片底片化(click事件里)
bitmap bmp=new bitmap(imgoriginal);//从现有的图像初始化位图类
for(int i=0;i<bmp.width;i++)
{
   for(int j=0;j<bmp.height;j++)
{
    color c=bmp.getpixel(i,j);//获得原来像素点颜色
    color cafter=color.fromargb(255-c.r,255-c.g,255-c.b);//改变
    bmp.setpixel(i,j,cafter); //设置回去
}
}
picturebox2.backgroundimage=bmp;

//保存修改后的图片(click事件中)
savefiledialog sfd=new savefiledialog();//初始化保存类
sfd.showdialog();//重载显示资源管理器界面
picturebox2.backgroundimage.save(sfd.filename+".jpg");//获取控件中的背景图片AAA

二.C#模拟QQ登陆器源代码

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 DamonQQ
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //屏幕的高度
            int ScreenH=1080;
            this.Location = new Point(this.Location.X, ScreenH - this.Height - 20);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //关闭本窗体,打开qq好友窗体
            this.Hide();
            FrmQQMain frmQQMain = new FrmQQMain();
            frmQQMain.ShowDialog();
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            //关闭本窗体,打开QQ设置窗体
            this.Hide();
            QQsetup QQsetup = new QQsetup();
            QQsetup.ShowDialog();
           
        }

2.1一些鼠标事件

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时,标签变成黑色
            lblFoundCode.ForeColor = Color.Black;
           

        }

        private void label1_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开时,标签变成灰色
            lblFoundCode.ForeColor = Color.Gray;
        }

        private void checkBox2_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时,标签变成黑色
            chkRemember.ForeColor = Color.Black;
        }

        private void checkBox2_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开时,标签变成灰色
            chkRemember.ForeColor = Color.Gray;
        }

        private void checkBox1_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时,标签变成黑色
            chkAutoLogin.ForeColor = Color.Black;
        }

        private void checkBox1_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开时,标签变成灰色
            chkAutoLogin.ForeColor = Color.Gray;
        }

        private void label2_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时,标签变成黑色
            lblRegister.ForeColor = Color.Black;
        }

        private void label2_MouseLeave(object sender, EventArgs e)
        {

            //鼠标离开时,标签变成灰色
            lblRegister.ForeColor = Color.Gray;
        }

        private void pictureBox10_MouseEnter(object sender, EventArgs e)
        {
            //鼠标进入时 图片背景变红
            picClose.BackColor = Color.Red;
        }

2.2模拟QQ界面的一些特效

        private void pictureBox10_MouseLeave(object sender, EventArgs e)
        {
            picClose.BackColor = Color.Transparent;
        }

        private void pictureBox9_MouseEnter(object sender, EventArgs e)
        {
            //颜色浅灰色半透明
            picShrink.BackColor = Color.FromArgb(50, 200, 200, 200);
           
        }

        private void pictureBox9_MouseLeave(object sender, EventArgs e)
        {
            picShrink.BackColor = Color.Transparent;
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            //变成蓝色小企鹅
            picLogin.BackgroundImage = Properties.Resources.iconqq;//此处应为蓝色
            
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
             picLoginTail.BackgroundImage = Properties.Resources.down_en;
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            picLoginTail.BackgroundImage = Properties.Resources.down_dis;
        }

2.3一些需判定的显示效果

        bool isDrag;//是否拖动窗体
        Point clickPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrag = true;
            clickPoint = new Point(e.X, e.Y);
        }
       // Point movePoint;
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag ==true )
                this.Location = new Point(this.Location.X + e.X - clickPoint.X,
            this.Location .Y +e.Y -clickPoint .Y );
            clickPoint .X =e.X ;
            clickPoint .Y =e.Y ;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
        }

2.4头像背后弹出特效

        bool isMoveout;
        bool isMoveback;
        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            //加号弹出
            timer1.Enabled = true;
            isMoveout = true;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            //加号弹回

            isMoveout = false ;
            isMoveback = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isMoveout)
            {
                picMultiUser.Location = new Point(picMultiUser.Location.X + 10, picMultiUser.Location.Y);
                if (picMultiUser.Location.X >= 277)
                {
                    timer1.Enabled = false;
                    return;
                }
            }
            else 
            {
                picMultiUser.Location = new Point(picMultiUser.Location.X - 10, picMultiUser.Location.Y);

                if (picMultiUser.Location.X <= 178)
                {
                    timer1.Enabled = false;
                    return;
                }
            }
        }

        private void picSet_MouseEnter(object sender, EventArgs e)
        {
            picSet.BackColor = Color.FromArgb(50, 200, 200, 200);
        }

        private void picSet_MouseLeave(object sender, EventArgs e)
        {
            picSet.BackColor = Color.Transparent;
        }

        private void picQuickMark_MouseEnter(object sender, EventArgs e)
        {
            picQuickMark.BackgroundImage = Properties.Resources.tdcode1;
        }

        private void picQuickMark_Click(object sender, EventArgs e)
        {
            //关闭本窗体,打开二维码窗体
            this.Hide();
            QuickMark QuickMark = new QuickMark();
            QuickMark.ShowDialog();
        }

        private void picQuickMark_MouseLeave(object sender, EventArgs e)
        {
            picQuickMark.BackgroundImage = Properties.Resources.tdcode;
        }

        private void picKeyboard_MouseEnter(object sender, EventArgs e)
        {
            picKeyboard.BackgroundImage = Properties.Resources.keyboard1;
        }

        private void picKeyboard_MouseLeave(object sender, EventArgs e)
        {
            picKeyboard.BackgroundImage = Properties.Resources.keyboard;
        }

      
    }
}

2.5弹出距离计算

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 DamonResign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      
        private void button2_MouseEnter(object sender, EventArgs e)
        {
            Random r = new Random();
            button2.Location = new Point(r.Next() % (this.Width - button2.Width),
                r.Next() % (this.ClientSize.Height - button2.Height));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2  form2 = new Form2 ();
            form2.ShowDialog();
        }
                    } 
        }

三.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;
using System.IO;

namespace DamonMusic
{
    public partial class Form1 : Form
    {
        private String[] songs;
        private String[] lyrics;
        private String[] pics;
        private int songPointer = 0;

        private Label[] lyricLabels = new Label[100];

        public Form1()
        {
            InitializeComponent();
        }

        // type = 1 下一首 type = -1 上一首
        private int checkBound(int songPointer, int type){
            Boolean arrivedMaximum = false;
            Boolean arrivedMinimum = false;
            if(type == 1)
                songPointer += 1;
            else{
                songPointer -= 1;
            }
            if (songPointer == songs.Length) {
                arrivedMaximum = true;
            }
            if (songPointer == -1)
            {
                arrivedMinimum = true;
            }
            if (arrivedMaximum) {
                songPointer = 0;
            }
            if (arrivedMinimum) {
                songPointer = songs.Length - 1;
            }
            return songPointer;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            songs = Directory.GetFiles("song", "*.mp3");
            lyrics = Directory.GetFiles("lyric", "*.lrc");
            pics = Directory.GetFiles("image", "*.jpg");
            //相对路径
            if (songs.Length != 0){
                axWindowsMediaPlayer1.URL = songs[songPointer];
                LyricFiles lyricFiles = new LyricFiles();
                lyricFiles.LoadLyric(lyrics[songPointer]);
                int yStep = 0;
                for (int i = 0; i < lyricFiles.lstLyric.Count; i++)
                {
                    Label lblLyric = new Label();
                    lblLyric.Size = new System.Drawing.Size(500, 30);
                    lblLyric.BackColor = Color.Transparent;
                    lblLyric.ForeColor = Color.White;
                    lblLyric.Font = new System.Drawing.Font("微软雅黑", 14);
                    lblLyric.Text = lyricFiles.lstLyric[i].strLyric;
                    lblLyric.Location = new Point(60, yStep);
                    yStep += 40;
                    lyricLabels[i] = lblLyric;
                    this.Controls.Add(lblLyric);
                }
                this.BackgroundImage = Image.FromFile(pics[songPointer]);
                //songPointer = checkBound(songPointer,1);

             }
            else axWindowsMediaPlayer1.URL = null;

            axWindowsMediaPlayer1.Ctlcontrols.stop();
           
            //初始化 准备工作
            pnlControlBar.BackColor = Color.FromArgb(127, 200, 200, 200);
            
        }

3.1 歌曲播放设定实现

        bool isplay = false;
        private void pictureBox9_Click(object sender, EventArgs e)
        {
            
            isplay = !isplay;
            //播放歌曲
            if (isplay)
            {
                lblShowDuration.Text = axWindowsMediaPlayer1.currentMedia.durationString;
                axWindowsMediaPlayer1.Ctlcontrols.play();
                picPlay.BackgroundImage = Properties.Resources.pause_circle_o;
            }
            else//暂停播放
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                picPlay.BackgroundImage=Properties.Resources.play_circle_o;
            }
            
            //打开文件
         //  Encoding encode = Encoding.GetEncoding("GB2312");
         //  FileStream fs=new FileStream ("音阙诗听-红昭愿.lrc",FileMode .Open );
         //  StreamReader  sr=new StreamReader (fs,encode  );
         // 
         //  string line = sr.ReadLine();
         //  //操作文件
         //  //richTextBox1.Text = sr.ReadToEnd();
         //  //关闭文件
         //  sr.Close();
         //  fs.Close();
        }

        private void pictureBox10_Click(object sender, EventArgs e)
        {
                isplay = true;

                for (int i = 0; i < 100; i++)
                {
                    if (lyricLabels[i] == null) {
                        continue;
                    }
                    this.Controls.Remove(lyricLabels[i]);
                }

3.2歌词显示实现

                songPointer = checkBound(songPointer, 1);
                axWindowsMediaPlayer1.URL = songs[songPointer];
                axWindowsMediaPlayer1.Ctlcontrols.play();
                picPlay.BackgroundImage = Properties.Resources.pause_circle_o;

                LyricFiles lyricFiles = new LyricFiles();
                lyricFiles.LoadLyric(lyrics[songPointer]);
                int yStep = 0;
                for (int i = 0; i < lyricFiles.lstLyric.Count; i++)
                {
                    Label lblLyric = new Label();
                    lblLyric.Size = new System.Drawing.Size(500, 30);
                    lblLyric.BackColor = Color.Transparent;
                    lblLyric.ForeColor = Color.White;
                    lblLyric.Font = new System.Drawing.Font("微软雅黑", 14);
                    lblLyric.Text = lyricFiles.lstLyric[i].strLyric;
                    lblLyric.Location = new Point(60, yStep);
                    yStep += 40;
                    lyricLabels[i] = lblLyric;
                    this.Controls.Add(lblLyric);
                }

                this.BackgroundImage = Image.FromFile(pics[songPointer]);
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            isplay = true;

            for (int i = 0; i < 100; i++)
            {
                if (lyricLabels[i] == null)
                {
                    continue;
                }
                this.Controls.Remove(lyricLabels[i]);
            }

            songPointer = checkBound(songPointer, -1);
            axWindowsMediaPlayer1.URL = songs[songPointer];
            axWindowsMediaPlayer1.Ctlcontrols.play();
            picPlay.BackgroundImage = Properties.Resources.pause_circle_o;

            LyricFiles lyricFiles = new LyricFiles();
            lyricFiles.LoadLyric(lyrics[songPointer]);
            int yStep = 0;
            for (int i = 0; i < lyricFiles.lstLyric.Count; i++)
            {
                Label lblLyric = new Label();
                lblLyric.Size = new System.Drawing.Size(500, 30);
                lblLyric.BackColor = Color.Transparent;
                lblLyric.ForeColor = Color.White;
                lblLyric.Font = new System.Drawing.Font("微软雅黑", 14);
                lblLyric.Text = lyricFiles.lstLyric[i].strLyric;
                lblLyric.Location = new Point(60, yStep);
                yStep += 40;
                lyricLabels[i] = lblLyric;
                this.Controls.Add(lblLyric);
            }
            this.BackgroundImage = Image.FromFile(pics[songPointer]);
        }
    }
}

四.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 ImageProcessDamon
{
    public partial class Form1 : Form
    {
        //图像处理主界面
        public Form1()
        {
            InitializeComponent();
        }

        Image imgOriginal;
       
        //打开一张图片
        private void btnOpenimage_Click(object sender, EventArgs e)
        {
         
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog(); 


            string strFileName = ofd.FileName;

            imgOriginal = Image.FromFile(strFileName);
            pictureBox1 .BackgroundImage = imgOriginal;
        }
        //底片处理
        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap( imgOriginal);
           
            for (int i = 0; i < bmp.Width ; i++) 
            {
                for (int j = 0; j < bmp.Height ; j ++)
                {
                    //获得原来像素点颜色
                    Color c = bmp.GetPixel(i , j);
                    //改变
                    Color cAfter = Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
                    //设置回去
                    bmp.SetPixel(i, j, cAfter);
                }
            }
            picAfter.BackgroundImage = bmp;
        }
        //灰度化
        private void button2_Click_1(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(imgOriginal);

            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获得原来像素点颜色
                    Color c = bmp.GetPixel(i, j);
                    //改变
                    int gray = (c.R + c.G + c.B) / 3;
                    Color cAfter = Color.FromArgb(gray ,gray ,gray );
                    //设置回去
                    bmp.SetPixel(i, j, cAfter);
                }
            }
            picAfter.BackgroundImage = bmp;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //保存图片
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog();

            picAfter.BackgroundImage.Save (sfd .FileName +".bmp");
        }


        private void button3_Click(object sender, EventArgs e)
        {            //以浮雕效果显示图像
            try
            {
                int Height = this.pictureBox1.BackgroundImage.Height;
                int Width = this.pictureBox1.BackgroundImage.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);
                Bitmap oldBitmap = (Bitmap)this.pictureBox1.BackgroundImage;
                Color pixel1, pixel2;
                for (int x = 0; x < Width - 1; x++)
                {
                    for (int y = 0; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        pixel1 = oldBitmap.GetPixel(x, y);
                        pixel2 = oldBitmap.GetPixel(x + 1, y + 1);
                        r = Math.Abs(pixel1.R - pixel2.R + 128);
                        g = Math.Abs(pixel1.G - pixel2.G + 128);
                        b = Math.Abs(pixel1.B - pixel2.B + 128);
                        if (r > 255)
                            r = 255;
                        if (r < 0)
                            r = 0;
                        if (g > 255)
                            g = 255;
                        if (g < 0)
                            g = 0;
                        if (b > 255)
                            b = 255;
                        if (b < 0)
                            b = 0;
                        newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
                this.picAfter.BackgroundImage = newBitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //以锐化效果显示图像
            try
            {
                int Height = this.pictureBox1.BackgroundImage.Height;
                int Width = this.pictureBox1.BackgroundImage.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);
                Bitmap oldBitmap = (Bitmap)this.pictureBox1.BackgroundImage;
                Color pixel;
                //拉普拉斯模板
                int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
                for (int x = 1; x < Width - 1; x++)
                    for (int y = 1; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        int Index = 0;
                        for (int col = -1; col <= 1; col++)
                            for (int row = -1; row <= 1; row++)
                            {
                                pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                                g += pixel.G * Laplacian[Index];
                                b += pixel.B * Laplacian[Index];
                                Index++;
                            }
                        //处理颜色值溢出
                        r = r > 255 ? 255 : r;
                        r = r < 0 ? 0 : r;
                        g = g > 255 ? 255 : g;
                        g = g < 0 ? 0 : g;
                        b = b > 255 ? 255 : b;
                        b = b < 0 ? 0 : b;
                        newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
                this.picAfter.BackgroundImage = newBitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示");
            }
        }
    }
}

C#五.实现模拟PPT绘图功能

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 Damoncreater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义绘图对象
        Graphics g;

        int dashOffset;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        Point clickPoint;
        int w;
        int h;

        bool isDrawRect;
       

        private void timer1_Tick(object sender, EventArgs e)
        {
            dashOffset++;
            this.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //实例化绘图对象
            g = e.Graphics;

            Pen pen = new Pen(Color.Black, 2);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
            pen.DashPattern = new float[] { 4, 4 };
            pen.DashOffset = dashOffset;

            //画一个矩形
            g.DrawRectangle(pen, clickPoint.X, clickPoint.Y, w, h);

            Pen pen2 = new Pen(Color.White, 2);
            pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
            pen2.DashPattern = new float[] { 4, 4 };
            pen2.DashOffset = dashOffset + 4;

            //画一个矩形
            g.DrawRectangle(pen2, clickPoint.X, clickPoint.Y, w, h);

        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawRect = true;
            clickPoint = new Point(e.X, e.Y);
        }


        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawRect)
            {

                w = e.X - clickPoint.X;
                h = e.Y - clickPoint.Y;

                g = pictureBox1.CreateGraphics();

                Pen pen = new Pen(Color.Black, 2);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                pen.DashPattern = new float[] { 4, 4 };
                pen.DashOffset = dashOffset;

                //画一个矩形
                g.DrawRectangle(pen, clickPoint.X, clickPoint.Y, w, h);

                Pen pen2 = new Pen(Color.White, 2);
                pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                pen2.DashPattern = new float[] { 4, 4 };
                pen2.DashOffset = dashOffset + 4;

                //画一个矩形
                g.DrawRectangle(pen2, clickPoint.X, clickPoint.Y, w, h);

            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawRect = false;
            //矩形头像
            Bitmap bmp = new Bitmap(w, h);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    Color c = bmpOriginal.GetPixel(i + clickPoint.X, j + clickPoint.Y);
                    bmp.SetPixel(i, j, c);
                }
            }
            pictureBox2.BackgroundImage = bmp;
            //圆形头像

        }
        //打开图片文件
        Bitmap bmpOriginal;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();

            string imgFileName = ofd.FileName;

            bmpOriginal = new Bitmap(imgFileName);

            pictureBox1.BackgroundImage = bmpOriginal;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //保存图片
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog();

            pictureBox2.BackgroundImage.Save(sfd.FileName + ".bmp");
        }

    }

}

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;
	using System.Drawing.Drawing2D;
	
	namespace Geometry_
	{
	    public partial class Form1 : Form
	    {
	        public Form1()
	        {
	            InitializeComponent();
	        }
	        List<ParentGeometry> lstGeo = new List<ParentGeometry>();
	        Graphics g;
	        bool isDraw;
	        Color frameColor = Color.Black;
	        Color fillColor = Color.Red;
	        private void Form1_Paint(object sender, PaintEventArgs e)
	        {
	            g = e.Graphics;
	            for (int i = 0; i < lstGeo.Count; i++) {
	                lstGeo[i].Draw(g);
	            }                         
	        }
	        private void Form1_MouseDown(object sender, MouseEventArgs e)
	        {
	            //按左键则画线
	            if (e.Button == System.Windows.Forms.MouseButtons.Left) {
	                isDraw = true;
	            }
	            //按右键则清除
	            if (e.Button == System.Windows.Forms.MouseButtons.Right)
	            {
	                lstGeo.Clear();
	                this.Invalidate();
	            }
	            //选中D的是矩形
	            if (rbtnRectangle.Checked) {
	                MyRectangle rectangle = new MyRectangle();
	                rectangle.rect.X = e.X;
	                rectangle.rect.Y = e.Y;
                rectangle.frameColor = frameColor;
	                rectangle.fillColor = fillColor;
	                lstGeo.Add(rectangle);
	            }
	            //选中D的是椭圆
	            else if (rbtnEllipse.Checked)
	            {
	                MyEllipse ellipse = new MyEllipse();
	                ellipse.rect.X = e.X;
	                ellipse.rect.Y = e.Y;
	                ellipse.frameColor = frameColor;
	                ellipse.fillColor = fillColor;
	                lstGeo.Add(ellipse);
            }
	            //选中D的是自由曲线
	            else if (rbtnFreeLine.Checked)
	            {
	                MyFreeLine freeLine = new MyFreeLine();
	                freeLine.lstPoint.Add(new Point(e.X, e.Y));
	                freeLine.frameColor = frameColor;
	                lstGeo.Add(freeLine);
	            }
	            //选中D的是三角形
	            else if (rbtnTriangle.Checked) {
	                MyTriangle triangle = new MyTriangle();
	                triangle.rect.X = e.X;
	                triangle.rect.Y = e.Y;
	                triangle.frameColor = frameColor;
	                triangle.fillColor = fillColor;
	                lstGeo.Add(triangle);
	            }
	            //选中D的是多边形
	            else if (rbtnPolygon.Checked) {
	                MyPolygon polygon = new MyPolygon();
	                polygon.lstPolygon.Add(new Point(e.X, e.Y));
	                polygon.frameColor = frameColor;
	                polygon.fillColor = fillColor;
	                lstGeo.Add(polygon);
	            }
	            //选中D的是箭头
            else if (rbtnArraw.Checked)
	            {
	                MyArrow arraw = new MyArrow();
	                arraw.rect.X = e.X;
	                arraw.rect.Y = e.Y;
	                arraw.frameColor = frameColor;
	                arraw.fillColor = fillColor;
	                lstGeo.Add(arraw);
	            }
	        }
	        private void Form1_MouseMove(object sender, MouseEventArgs e)
	        {
	            if (isDraw) {
	                if (rbtnRectangle.Checked)
	                {
	                    ((MyRectangle)lstGeo[lstGeo.Count - 1]).rect.Width = e.X - ((MyRectangle)lstGeo[lstGeo.Count - 1]).rect.X;
	                    ((MyRectangle)lstGeo[lstGeo.Count - 1]).rect.Height = e.Y - ((MyRectangle)lstGeo[lstGeo.Count - 1]).rect.Y;
	                }
	                else if (rbtnEllipse.Checked) {
                    ((MyEllipse)lstGeo[lstGeo.Count - 1]).rect.Width = e.X - ((MyEllipse)lstGeo[lstGeo.Count - 1]).rect.X;
	                    ((MyEllipse)lstGeo[lstGeo.Count - 1]).rect.Height = e.Y - ((MyEllipse)lstGeo[lstGeo.Count - 1]).rect.Y;
                }else if(rbtnFreeLine.Checked){
	
	                    ((MyFreeLine)lstGeo[lstGeo.Count - 1]).lstPoint.Add(new Point(e.X, e.Y));
	                }
	                else if (rbtnTriangle.Checked) {
	                    ((MyTriangle)lstGeo[lstGeo.Count - 1]).rect.Width = e.X - ((MyTriangle)lstGeo[lstGeo.Count - 1]).rect.X;
	                    ((MyTriangle)lstGeo[lstGeo.Count - 1]).rect.Height = e.Y - ((MyTriangle)lstGeo[lstGeo.Count - 1]).rect.Y;
	                }
	                else if (rbtnPolygon.Checked) {
	                    ((MyPolygon)lstGeo[lstGeo.Count - 1]).lstPolygon.Add(new Point(e.X, e.Y));
	                }
	                else if (rbtnArraw.Checked) {
	                    ((MyArrow)lstGeo[lstGeo.Count - 1]).rect.Width = e.X - ((MyArrow)lstGeo[lstGeo.Count - 1]).rect.X;
	                    ((MyArrow)lstGeo[lstGeo.Count - 1]).rect.Height = e.Y - ((MyArrow)lstGeo[lstGeo.Count - 1]).rect.Y;           
	                }           
	            }           
	            this.Invalidate();
	        }
	        private void Form1_MouseUp(object sender, MouseEventArgs e)
	        {
	            isDraw = false;
	        }        
	        //选择边框颜色
	        private void btnBorderColor_Click(object sender, EventArgs e)
	        {
	            ColorDialog cd = new ColorDialog();
	            cd.ShowDialog();
	            frameColor = cd.Color;
	        }
	        //选择填充颜色
	        private void btnFillColor_Click(object sender, EventArgs e)
        {
	            ColorDialog cd = new ColorDialog();
	            cd.ShowDialog();
	            fillColor = cd.Color;
	        }
	    
	    }
	}

绘图类实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Geometry_
{
    //几何图源的父类
    class ParentGeometry
    {
        public Color frameColor;//边框颜色
        //int framewidth;
        //public DashStyle frameDashStyle;
        public Color fillColor;//填充颜色
        //public Rectangle rectBox;
        //绘制
        public virtual void Draw(Graphics g) { }       
    }
    //矩形
    class MyRectangle : ParentGeometry {
        public Rectangle rect;        
        //绘制
        public override void Draw(Graphics g) {
            Pen pen = new Pen(frameColor, 3);
            SolidBrush sb = new SolidBrush(fillColor);
            g.DrawRectangle(pen, rect);
            g.FillRectangle(sb, rect);
        }
    }
    //椭圆
    class MyEllipse : ParentGeometry
    {
        public Rectangle rect;
        //绘制
        public override void Draw(Graphics g)
        {
            Pen pen = new Pen(frameColor, 3);
            SolidBrush sb = new SolidBrush(fillColor);
            g.DrawEllipse(pen, rect);
            g.FillEllipse(sb, rect);
        }
    }
    //三角形
    class MyTriangle : ParentGeometry
    {
        public Rectangle rect;
        public override void Draw(Graphics g)
        {
            Point[] points = new Point[3];
            //描绘三角形的三个点的坐标
            points[0] = new Point(rect.X + rect.Width / 2, rect.Y);//顶点坐标o
            points[1] = new Point(rect.X, rect.Height + rect.Y);
            points[2] = new Point(rect.X + rect.Width, rect.Height + rect.Y);
         Pen pen = new Pen(frameColor, 3);
            g.DrawPolygon(pen, points);
            SolidBrush sb = new SolidBrush(fillColor);
            g.FillPolygon(sb, points);
        }
    }
    //自由曲线
    class MyFreeLine : ParentGeometry {
        public List<Point> lstPoint =new List<Point>();
        public override void Draw(Graphics g) {
            Pen pen = new Pen(frameColor, 3);
            if (lstPoint.Count >= 2) {
                g.DrawLines(pen, lstPoint.ToArray());
            }           
        }
    }
    //多边形
    class MyPolygon : ParentGeometry
    {
        public List<Point> lstPolygon =new List<Point>();
        
        public override void Draw(Graphics g)
        {
            Pen pen = new Pen(frameColor, 3);
            if (lstPolygon.Count >= 5) {
                g.DrawPolygon(pen, lstPolygon.ToArray());
            }     
        }
    }
    //箭头
    class MyArrow : ParentGeometry {
        public Rectangle rect;      
        public override void Draw(Graphics g)
        {
            Point[] points = new Point[7];
            points[4] = new Point(rect.X + rect.Width * 3 / 4, rect.Y);
            points[6] = new Point(rect.X + rect.Width, rect.Y + rect.Height / 2);
            points[5] = new Point(rect.X + rect.Width * 3 / 4, rect.Y + rect.Height);
            points[3] = new Point(rect.X + rect.Width * 3 / 4, (rect.Y + rect.Height) * 3 / 4);
            points[1] = new Point(rect.X, (rect.Y + rect.Height) * 3 / 4);
            points[0] = new Point(rect.X, (rect.Y + rect.Height) / 4);
            points[2] = new Point((rect.X + rect.Width) * 3 / 4, (rect.Y + rect.Height) / 4);

            Pen pen = new Pen(frameColor, 3);
            SolidBrush sb = new SolidBrush(fillColor);            
            g.DrawPolygon(pen, points);
            g.FillPolygon(sb, points);
        }
    }
}
posted @ 2018-12-06 21:02  俊熙是我真名啊  阅读(446)  评论(0编辑  收藏  举报
Live2D