C#绘制中国国旗的方法

所属分类: 软件编程 / C#教程 阅读数: 37
收藏 0 赞 0 分享

本文实例讲述了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#学习笔记之状态模式详解

这篇文章主要为大家详细介绍了C#学习笔记之状态模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C# 字符串、数组和List的截取和转换实例

下面小编就为大家分享一篇C# 字符串、数组和List的截取和转换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

c#打包文件解压缩的实例

下面小编就为大家分享一篇c#打包文件解压缩的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助
收藏 0 赞 0 分享

判断一个整数是否是2的N次幂实现方法

下面小编就为大家分享一篇判断一个整数是否是2的N次幂实现方法,实例简洁,具有很好的参考价值。希望对大家有所帮助
收藏 0 赞 0 分享

浅谈c#中config.exe 引发的一些问题

下面小编就为大家分享一篇浅谈c#中config.exe 引发的一些问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于C#开发中的那些编码问题(详谈)

下面小编就为大家分享一篇基于C#开发中的那些编码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C#实现WPS文件转PDF格式的方法示例

这篇文章主要介绍了C#实现WPS文件转PDF格式的方法,涉及C#针对office组件的相关引用与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现的文件上传下载工具类完整实例【上传文件自动命名】

这篇文章主要介绍了C#实现的文件上传下载工具类,结合完整实例形式分析了C#操作文件上传与下载功能,并且还可针对上传文件自动命名以避免服务器中的文件名重复,需要的朋友可以参考下
收藏 0 赞 0 分享

C# Socket 发送&接收&返回 简单应用实例

下面小编就为大家分享一篇C# Socket 发送&接收&返回 简单应用实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多