原文:使用GDI+生成KnownColor列表

在写这篇“GDI+与WPF中的颜色简析”之前,我试着使用GDI+生成KnownColor列表。现将关键代码贴出来吧。

最终效果图:

现将关键代码:
         Bitmap m_Bitmap = null;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (m_Bitmap != null)
            {
                Graphics g = e.Graphics;
                g.DrawImage(m_Bitmap, new Point(0, 0));
            }
        }

        private void btnTestColor_Click(object sender, EventArgs e)
        {
            int count = 0;
            foreach (string s in Enum.GetNames(typeof(KnownColor)))
            {
                count++;
            }

            int cols = 4;
            int rows = count / cols;
            if (count % rows > 0) rows++;

            int rectWidth = 100;
            int rectHeight = 30;
            int wordSpaceFromRect = 10;
            int rowSpace = 10;
            int marginTop = 20;
            int marginBottom = 20;

            int width = 300 * cols + 50;
            int height = (rectHeight + rowSpace) * rows + marginTop + marginBottom ;
            m_Bitmap = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(m_Bitmap);
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            g.Clear(Color.White);
            Color someColor = Color.FromArgb(0);
            Color redShade = Color.FromArgb(255, 200, 0, 100);

            SolidBrush myBrush1;
            Font myFont = new Font("Arial", 12);
            int x = 20;
            int y = marginTop;

            for (int i = 0; i < count; i++ )
            {
                someColor = Color.FromKnownColor((KnownColor)i);
                myBrush1.Color = someColor;
                g.FillRectangle(myBrush1, x, y, rectWidth, rectHeight);
                g.DrawRectangle(Pens.Black, x, y, rectWidth, rectHeight);
                g.DrawString(someColor.ToString().Replace("Color [","").Replace("]",""), myFont, Brushes.Black, x + rectWidth + wordSpaceFromRect, y);
                if (i % cols == cols - 1)
                {
                    y += rectHeight + rowSpace;
                    x -= 300 * (cols - 1);
                }
                if (i % cols < cols - 1)
                {
                    x += 300;
                }
            }

            g.Dispose();

            this.Invalidate();
        }

使用GDI+生成KnownColor列表的更多相关文章

  1. T-SQL Recipes之生成动态列表数据

    Problem 首先什么是动态列表?举个示例,假设你想输出以逗号分隔的IDs,如: 1,45,67,199,298 Solution 生成动态列表数据在我们生活场景中很常见,比如在 Dynamic P ...

  2. 根据BOM和已存在的文件生成文件列表

    在BOM中记录中有物料编码,物料名称,物料规格等,而且依据BOM已经生成了相应的文件,如采购规格书,检验规格书等,这个时候需要获得这些文件的标题,并且生成一个列表,可以使用下面的VBA代码,具体代码如 ...

  3. 学习笔记:利用GDI+生成简单的验证码图片

    学习笔记:利用GDI+生成简单的验证码图片 /// <summary> /// 单击图片时切换图片 /// </summary> /// <param name=&quo ...

  4. js动态生成数据列表

    我们通常会使用table标签来展示数据内容,由于需要展示的数据内容是随时更换的,所以不可能将展示的数据列表写死在html写死在页面中,而是需要我们根据后台传来的数据随时更换,这个时候就需要我们使用js ...

  5. DOS命令生成文件列表

    DOS命令窗口,生成文件列表命令格式:dir xmlFiles /b >list.txt dir 文件列表相关命令 xmlFiles 待生成文件所在文件夹,在dos命令窗口根目录下,省略前缀.别 ...

  6. react将表格动态生成视频列表【代码】【案例】

    只需要创建一个表格,id为videos,react就能将这个表格转换成视频列表,并点击自动播放 index.html <!DOCTYPE html> <html> <he ...

  7. 开源 免费 java CMS - FreeCMS1.9 移动APP生成网站列表数据

    项目地址:http://www.freeteam.cn/ 生成网站列表数据 提取同意移动APP訪问的网站列表,生成json数据到/mobile/index.html页面. 从左側管理菜单点击生成网站列 ...

  8. 开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据

    项目地址:http://www.freeteam.cn/ 生成栏目列表数据 提取当前管理网站下同意移动APP訪问的栏目列表,生成json数据到/site/网站文件夹/mobile/channels.h ...

  9. Java生成随机数列表

    生成随机数列表 1.Java8以前 (1)Math.random private List<UserEntity> random1() { ArrayList<UserEntity& ...

随机推荐

  1. ocx中调用ocx

    BOOL CXXXApp::InitInstance()中加入一句AfxEnableControlContainer();

  2. 【u246】卫星照片

    Time Limit: 1 second Memory Limit: 64 MB [问题描述] 农夫 John 正在研究他的农场的卫星照片.照片为一个R (1<= R <= 75) 行C ...

  3. [RxJS] Replace zip with combineLatest when combining sources of data

    This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. ...

  4. 【心情】Priority_queue容器的用法

    所给的代码最顶端是最小的元素 要改为最顶端是最大的则只需把 friend bool operator<(Node a, Node b) { return a.val > b.val; } ...

  5. [AngularFire2] Signup and logout

    import {AuthProviders, FirebaseAuthState, FirebaseAuth, AuthMethods} from "angularfire2";i ...

  6. linux ps命令,查看某进程cpu和内存占用率情况, linux ps命令,查看进程cpu和内存占用率排序。 不指定

    背景:有时需要单看某个进程的CPU及占用情况,有时需要看整体进程的一个占用情况.一. linux ps命令,查看某进程cpu和内存占用率情况[root@test vhost]# ps auxUSER  ...

  7. 在ArcEngine中使用Geoprocessing工具-执行工具

    转自原文在ArcEngine中使用Geoprocessing工具-执行工具 来解析一下Geoprocessor类的Execute方法,他有两种重载,Execute(IGPProcess, ITrack ...

  8. Android 软键盘监听事件

    Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入.     那么,弹出软键盘后必然会造成原有布局高度的减少 ...

  9. AngularJS之ng-class指令

    ng-class是AngularJS预设的一个指令,用于动态自定义dom元素的css class name. 在angular中为我们提供了3种方案处理class: 1:scope变量绑定. < ...

  10. &quot;Swift&quot;编程语言

    来自英文文档.百度翻译以及自己没过4级的渣渣英语功底,为了自己以后看起来方便 About Swift 关于"海燕" IMPORTANT 重要 This is a prelimina ...