C#绘制中国国旗的方法

 更新时间:2015年08月13日 09:16:57   作者:北风其凉  
这篇文章主要介绍了C#绘制中国国旗的方法,以实例形式较为详细的分析了C#图形绘制的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#绘制中国国旗的方法。分享给大家供大家参考。具体如下:

程序运行截图:

中国国旗被定义在《GB:12982-2004》中,以下是从维基百科条目中华人民共和国国旗中截的一张图,标出了五颗星大致的位置。

建立一个空的C# Windows窗体应用程序,窗体取名FormMain,在窗体中放一个PictureBox,取名为picFlagOfChina,并将Dock属性设置为Fill。程序代码中用到了窗体事件Load和Resize,程序代码如下:

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 ChineseFlag
{
 public partial class FormMain : Form
 {
  public FormMain()
  {
   InitializeComponent();
  }
  private void FormMain_Load(object sender, EventArgs e)
  {
   PaintFlag();
  }
  //修改窗体大小时发生
  private void FormMain_Resize(object sender, EventArgs e)
  {
   PaintFlag();
  }
  /// <summary>
  /// 在图片框 picFlagOfChina 中绘制国旗
  /// </summary>
  private void PaintFlag()
  {
   picFlagOfChina.BackColor = Color.Red;
   picFlagOfChina.Image = new Bitmap(
    picFlagOfChina.Width, picFlagOfChina.Height);
   Graphics g = Graphics.FromImage(picFlagOfChina.Image);
   Point[] p = new Point[] { };
   p = PentacleA(this.Width, this.Height);
   g.FillPolygon(Brushes.Yellow, p);
   p = PentacleB(this.Width, this.Height);
   g.FillPolygon(Brushes.Yellow, p);
   p = PentacleC(this.Width, this.Height);
   g.FillPolygon(Brushes.Yellow, p);
   p = PentacleD(this.Width, this.Height);
   g.FillPolygon(Brushes.Yellow, p);
   p = PentacleE(this.Width, this.Height);
   g.FillPolygon(Brushes.Yellow, p);
  }
  //大星
  private Point[] PentacleA(int width, int height)
  {
   return new Point[] 
   { 
    new Point((int)(width / 30.0 * 5), (int)(height / 20.0 * 2)),
    new Point((int)(width / 30.0 * 5.7), (int)(height / 20.0 * 4.1)),
    new Point((int)(width / 30.0 * 8), (int)(height / 20.0 * 4.1)),
    new Point((int)(width / 30.0 * 6), (int)(height / 20.0 * 5.3)),
    new Point((int)(width / 30.0 * 6.8), (int)(height / 20.0 * 7.3)),
    new Point((int)(width / 30.0 * 5), (int)(height / 20.0 * 6.1)),
    new Point((int)(width / 30.0 * 3.2), (int)(height / 20.0 * 7.3)),
    new Point((int)(width / 30.0 * 4), (int)(height / 20.0 * 5.3)),
    new Point((int)(width / 30.0 * 2), (int)(height / 20.0 * 4.1)),
    new Point((int)(width / 30.0 * 4.3), (int)(height / 20.0 * 4.1)),
   };
  }
  //工人星
  private Point[] PentacleB(int width, int height)
  {
   return new Point[] 
   { 
    new Point((int)(width / 30.0 * 9.2), (int)(height / 20.0 * 2.5)),
    new Point((int)(width / 30.0 * 9.6), (int)(height / 20.0 * 2)),
    new Point((int)(width / 30.0 * 9.3), (int)(height / 20.0 * 1.4)),
    new Point((int)(width / 30.0 * 9.95), (int)(height / 20.0 * 1.7)),
    new Point((int)(width / 30.0 * 10.45), (int)(height / 20.0 * 1.1)),
    new Point((int)(width / 30.0 * 10.36), (int)(height / 20.0 * 1.85)),
    new Point((int)(width / 30.0 * 11), (int)(height / 20.0 * 2.1)),
    new Point((int)(width / 30.0 * 10.34), (int)(height / 20.0 * 2.25)),
    new Point((int)(width / 30.0 * 10.3), (int)(height / 20.0 * 2.95)),
    new Point((int)(width / 30.0 * 9.9), (int)(height / 20.0 * 2.3))
   };
  }
  //农民星
  private Point[] PentacleC(int width, int height)
  {
   return new Point[] 
   { 
    new Point((int)(width / 30.0 * 11), (int)(height / 20.0 * 4.1)),
    new Point((int)(width / 30.0 * 11.65), (int)(height / 20.0 * 3.8)),
    new Point((int)(width / 30.0 * 11.55), (int)(height / 20.0 * 3.05)),
    new Point((int)(width / 30.0 * 12.05), (int)(height / 20.0 * 3.6)),
    new Point((int)(width / 30.0 * 12.7), (int)(height / 20.0 * 3.3)),
    new Point((int)(width / 30.0 * 12.35), (int)(height / 20.0 * 3.98)),
    new Point((int)(width / 30.0 * 12.9), (int)(height / 20.0 * 4.5)),
    new Point((int)(width / 30.0 * 12.1), (int)(height / 20.0 * 4.3)),
    new Point((int)(width / 30.0 * 11.8), (int)(height / 20.0 * 5)),
    new Point((int)(width / 30.0 * 11.7), (int)(height / 20.0 * 4.2))
   };
  }
  //小资星
  private Point[] PentacleD(int width, int height)
  {
   return new Point[] 
   { 
    new Point((int)(width / 30.0 * 11.1), (int)(height / 20.0 * 6.7)),
    new Point((int)(width / 30.0 * 11.8), (int)(height / 20.0 * 6.7)),
    new Point((int)(width / 30.0 * 12), (int)(height / 20.0 * 6)),
    new Point((int)(width / 30.0 * 12.2), (int)(height / 20.0 * 6.7)),
    new Point((int)(width / 30.0 * 12.9), (int)(height / 20.0 * 6.7)),
    new Point((int)(width / 30.0 * 12.35), (int)(height / 20.0 * 7.1)),
    new Point((int)(width / 30.0 * 12.55), (int)(height / 20.0 * 7.8)),
    new Point((int)(width / 30.0 * 12), (int)(height / 20.0 * 7.4)),
    new Point((int)(width / 30.0 * 11.45), (int)(height / 20.0 * 7.8)),
    new Point((int)(width / 30.0 * 11.65), (int)(height / 20.0 * 7.1))
   };
  }
  //民资星(工人星向下平移7个单位)
  private Point[] PentacleE(int width, int height)
  {
   return new Point[] 
   { 
    new Point((int)(width / 30.0 * 9.2), (int)(height / 20.0 * 9.5)),
    new Point((int)(width / 30.0 * 9.6), (int)(height / 20.0 * 9)),
    new Point((int)(width / 30.0 * 9.3), (int)(height / 20.0 * 8.4)),
    new Point((int)(width / 30.0 * 9.95), (int)(height / 20.0 * 8.7)),
    new Point((int)(width / 30.0 * 10.45), (int)(height / 20.0 * 8.1)),
    new Point((int)(width / 30.0 * 10.36), (int)(height / 20.0 * 8.85)),
    new Point((int)(width / 30.0 * 11), (int)(height / 20.0 * 9.1)),
    new Point((int)(width / 30.0 * 10.34), (int)(height / 20.0 * 9.25)),
    new Point((int)(width / 30.0 * 10.3), (int)(height / 20.0 * 9.95)),
    new Point((int)(width / 30.0 * 9.9), (int)(height / 20.0 * 9.3))
   };
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • 基于C# wpf 实现Grid内控件拖动详情

    基于C# wpf 实现Grid内控件拖动详情

    这篇文章主要介绍了基于C# wpf 实现Grid内控件拖动,有一些业务场景中我们需要拖动控件,在Grid中就可以实现控件拖动,通过设置Margin属性即可,下面文章我们来看看具体的实现内容
    2021-11-11
  • C#反射调用拓展类方法实例代码

    C#反射调用拓展类方法实例代码

    这篇文章主要给大家介绍了关于C#反射调用拓展类方法的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-01-01
  • C#利用ZXing.Net生成条形码和二维码

    C#利用ZXing.Net生成条形码和二维码

    这篇文章主要为大家详细介绍了C#利用ZXing.Net生成条形码和二维码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C#程序最小化到托盘图标操作步骤与实现代码

    C#程序最小化到托盘图标操作步骤与实现代码

    设置窗体属性showinTask=false;加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标;添加窗体最小化事件(首先需要添加事件引用)接下来介绍实现代码,感兴趣的朋友可以研究下
    2012-12-12
  • C#.NET 图片水印添加代码

    C#.NET 图片水印添加代码

    这篇文章主要为大家详细介绍了C#.NET 图片水印添加代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • C#插入图片到Excel表格单元格代码详解

    C#插入图片到Excel表格单元格代码详解

    在本篇文章里小编给大家整理了关于C#插入图片到Excel表格单元格的具体方法和实例代码,需要的朋友们可以学习下。
    2019-07-07
  • C#读写共享文件夹的方法

    C#读写共享文件夹的方法

    这篇文章主要为大家详细介绍了C#读写共享文件夹的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • C# 利用StringBuilder提升字符串拼接性能的小例子

    C# 利用StringBuilder提升字符串拼接性能的小例子

    一个项目中有数据图表呈现,数据量稍大时显得很慢,在使用了StringBuilder后效果提升很明显,下面有例子
    2013-07-07
  • C#中的那些常用加密算法

    C#中的那些常用加密算法

    本文主要讲解一下C#常用的那些加密算法,包括MD5加密、SHA1加密、Base64加密、Des加密、RSA加密等,需要的朋友可以参考下
    2020-11-11
  • C#中Timer使用及解决重入问题

    C#中Timer使用及解决重入问题

    本文主要介绍了C#中Timer使用及解决重入问题的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02

最新评论