1.图片缩放

using System;
using System.Windows.Forms;
using System.Drawing;
class haha : Form
{
    static void Main()
    {
        Application.Run(new haha());
    }
    Bitmap bit;
    int x, y;
    haha()
    {
        ClientSize = new Size(400, 400);
        bit = new Bitmap(800, 800);
        Graphics.FromImage(bit).DrawImage(Image.FromFile("beauty.jpg"),new Rectangle(new Point(0,0),new Size(bit.Width,bit.Height)));
        KeyDown += roll;
        MouseWheel += zoom;
        x = y = 0;
    }
    void zoom(object o, MouseEventArgs e)
    {
        if ((bit.Width < 100||bit.Height<100)&&e.Delta<0) return;
        if (bit.Width + e.Delta < 0 || bit.Height + e.Delta < 0) return;
        Bitmap b = new Bitmap(bit.Width + e.Delta, bit.Height + e.Delta);
        Graphics.FromImage(b).DrawImage(bit, new Rectangle(0, 0, b.Width, b.Height));
        bit = b;
        draw();
    }
    void draw()
    {
        if (x >= bit.Width - ClientSize.Width) x = bit.Width - 1 - ClientSize.Width;
        if (y >= bit.Height - ClientSize.Height) y = bit.Height - ClientSize.Height - 1;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        Bitmap b = new Bitmap(ClientSize.Width, ClientSize.Height);
        int i, j;
        for (i = 0; i < ClientSize.Width; i++)
            for (j = 0; j < ClientSize.Height; j++)
            {
                if (x + i >= bit.Width || y + j >= bit.Height)
                    b.SetPixel(i, j, Color.AliceBlue);
                else
                    b.SetPixel(i, j, bit.GetPixel(x + i, y + j));
            }
        CreateGraphics().DrawImage(b, 0, 0);
    }
    void roll(object o, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up: y -= 10; break;
            case Keys.Down: y += 10; break;
            case Keys.Right: x += 10; break;
            case Keys.Left: x -= 10; break;
        }
        draw();
    }
}

2.位图操作-乱画

using System.Drawing;
using System.Windows.Forms;
using System;
class haha:Form
{
    Bitmap bit;
    Random r = new Random();
    static void Main()
    {
        Application.Run(new haha());
    }
    void circle(int xx, int yy, int r,Color c)
    {
        int x; double y;
        for (x = xx - r; x < xx + r; x++)
        {
            y = Math.Sqrt(r * r - (xx - x) * (xx - x));
            if (x < 0 || yy - y < 0 || x >= bit.Width || yy + y >= bit.Height) continue;
            bit.SetPixel(x, (int)(y+yy), c);
            bit.SetPixel(x, (int)( yy-y), c);
        }
    }
    void line(int x1,int y1,int x2,int y2,Color c)
    {
        int fx, fy, tx, ty;
        if(x1==x2){
             if(y1>y2){
                fx=x2;fy=y2;
                tx=x1;ty=y1;
            }
            else {
                fx=x1;fy=y1;
                tx=x2;ty=y2;
            }
            int i;
            for (i = fy; i < ty; i++)
            {
                bit.SetPixel(x1, i, c);
            }
            CreateGraphics().DrawImage(bit,0,0);
            return;
        }
        if (x1 > x2)
        {
            fx = x2; fy = y2;
            tx = x1; ty = y1;
        }
        else
        {
            fx = x1; fy = y1;
            tx = x2; ty = y2;
        }
        double k = (ty - fy) / (tx - fx);
        double y;
        int x;
        for (x = fx; x < tx; x++)
        {
            y = k * x + ((double)y1 - k * x1);
            bit.SetPixel(x, (int)y, c);
        }
        CreateGraphics().DrawImage(bit, 0, 0);
    }
    haha()
    {
        ClientSize = new Size(500, 500);
        bit = new Bitmap(ClientSize.Width, ClientSize.Height);
        Graphics.FromImage(bit).Clear(Color.White);
        Timer t1 = new Timer();
        t1.Interval = 5000;
        t1.Tick += draw_line;
        //t1.Start();
        Timer t2 = new Timer();
        t2.Interval = 10;
        t2.Tick += draw_circle;
        t2.Start();
    }
    void draw_line(object o,EventArgs e)
    {
        line(r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, r.Next()%ClientSize.Width, r.Next()%ClientSize.Height, Color.FromArgb(r.Next()));
    }
    void draw_circle(object o, EventArgs e)
    {
        int rr=r.Next() % 100 + 1;
        int xx=r.Next() % ClientSize.Width;
        int yy=r.Next() % ClientSize.Height;
        for (int i = 0; i < 16&&rr>i;i++ )
            circle(xx,yy , rr-i, Color.FromArgb(r.Next()));
        CreateGraphics().DrawImage(bit, 0, 0);
    }
}

3.图片旋转

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyDown += draw;
            bitmap = new Bitmap(Image.FromFile("0.jpg"));
            ClientSize = new Size(700,700);
        }
        Bitmap rotate(Bitmap source)
        {
            Bitmap bit = new Bitmap(source.Height, source.Width);
            for (int x = 0; x < bit.Width; x++)
            {
                for (int y = 0; y < bit.Height; y++)
                {
                    bit.SetPixel(x, y, source.GetPixel(bit.Height- y-1, x));
                }
            }
            return bit;
        }
        Bitmap bitmap;
        void draw(object o, EventArgs e)
        {
            CreateGraphics().Clear(Color.Black);
            bitmap = rotate(bitmap);
            CreateGraphics().DrawImage(bitmap, 0, 0);
        }
    }

4.鼠标单击拖动图片

using System;
using System.Windows.Forms;
using System.Drawing;
class window : Form
{
    Rectangle rec=new Rectangle(23,23,23,23);
    Bitmap b=new Bitmap("beauty.jpg");
    int x, y;
    int dx, dy;
    bool move;
    public window(){
        MouseClick +=click;
        MouseMove += OnMove;
        Paint += paint;
        Size = new Size(800,800);
        x = y = 0;
    }
    void OnMove(object o, MouseEventArgs e)
    {
        if (move)
        {
            Bitmap bit = new Bitmap(Width,Height);
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.AliceBlue);
            g.DrawImage(b, e.X+dx, e.Y+dy);
            for (int i = 10; i < bit.Width; i++)
                bit.SetPixel(i, 100, Color.Red);
            CreateGraphics().DrawImage(bit, 0, 0);
        }
    }
    void paint(object o, EventArgs e)
    {
        CreateGraphics().DrawImage(b,0,0);
    }
    void click(object o, MouseEventArgs e)
    {
        if (e.X > x && e.X < x + b.Width && e.Y > y && e.Y < y + b.Height && move == false) { move = true; dx = x - e.X; dy = y - e.Y; }
        else if (move == true)
        {
            x = e.X+dx; y = e.Y+dy;
            move = false;
        }
    }

    public static void Main()
    {
        Application.Run(new window());
    }
}

5.没有边框的窗口

    public partial class Form1 : Form
    {
        public Form1()
        {
            ClientSize = Screen.PrimaryScreen.Bounds.Size;
            FormBorderStyle = FormBorderStyle.None;
            Paint += draw;
            TransparencyKey = DefaultBackColor;
            ShowInTaskbar = false;
        }
        void draw(object o, EventArgs e)
        {
            CreateGraphics().Clear(DefaultBackColor);
            Bitmap bit=new Bitmap(ClientSize.Width,ClientSize.Height);
            bit.MakeTransparent(Color.White);
            Graphics.FromImage(bit).DrawString("东大微雕\n文韬武略\n天下第一", new Font(new FontFamily("Consolas"),200,GraphicsUnit.Pixel),new SolidBrush(Color.Red),new Rectangle(30,30,ClientRectangle.Right,ClientRectangle.Bottom));
            //BackgroundImage = bit;
            //释放上面这句就会导致闪烁
            CreateGraphics().DrawImage(bit,ClientRectangle);
        }
    }

6.全屏的窗口

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Size = MaximumSize;
            WindowState = FormWindowState.Maximized;
            FormBorderStyle = FormBorderStyle.None;
            Paint += paint;
            files= Directory.GetFiles(Directory.GetCurrentDirectory(),"*.jpg");
            KeyDown += down;
        }
        void down(object o, KeyEventArgs e)
        {
            index++;
            Invalidate();
        }
        string[] files;
        int index=0;
        void paint(object o, PaintEventArgs e)
        {
            Bitmap bit = new Bitmap(ClientSize.Width,ClientSize.Height);
            Graphics.FromImage(bit).DrawImage(Image.FromFile(files[index%files.Length]), ClientRectangle);
            e.Graphics.DrawImage(bit,0,0);
        }
    }

7.分工演示器

    public partial class Form1 : Form
    {
        SpeechSynthesizer teller = new SpeechSynthesizer();
        void tell(string s)
        {
            teller.Speak(s);
        }
        void crossLine(int from, int to)
        {
            int w = bit.Width / (size + 1);
            int h = bit.Height / (size + 1);
            Graphics g = Graphics.FromImage(bit);
            Pen p = new Pen(new SolidBrush(Color.Gold),4);
            g.DrawLine(p, work[from] * w + w + w / 2, from * h + h + h / 2, work[to] * w + w + w / 2, to * h + h + h / 2);
            g.DrawLine(p, work[to] * w + w + w / 2, from * h + h + h / 2, work[from] * w + w + w / 2, to * h + h + h / 2);
            CreateGraphics().DrawImage(bit,0,0);
        }
        void go()
        {
            int i, j;
            bool changed = true;
            while (changed)
            {
                tell("只要有变化,那就接着调整.调整哇调整,一直到没法再调整了");
                changed = false;
                for (i = 0; i < size; i++)
                {
                    for (j = size - 1; j >= 0; j--)
                    {
                        if (a[i, work[j]] + a[j, work[i]] < a[i, work[i]] + a[j, work[j]])
                        {
                            crossLine(i, j);
                            tell("工人" + man[i] + "干任务" + (char)(work[i] + 'A') + "需要" + a[i, work[i]] + "的时间");
                            tell("工人" + man[j] + "干任务" + (char)(work[j] + 'A') + "需要" + a[j, work[j]] + "的时间");
                            tell("他们交换一下任务,岂不是更快");
                            int temp = work[i];
                            work[i] = work[j];
                            work[j] = temp;
                            draw();
                            changed = true;
                        }
                    }
                }
            }
        }
        int[] work;
        public Form1()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            Text = "分工演示器-written for jiege,made by weidiao";
            var set = teller.GetInstalledVoices();
            teller.SelectVoice(set[0].VoiceInfo.Name);
            init();
            Paint += delegate
            {
                draw();
                go();
                tell("杰哥,你可以百度一下模拟淬火算法,这种方法适用于解决NP问题,但它的缺点就是最后所求的结果不一定对");
                tell("这种方法能够保证得到局部最优解,至于有多么接近最优解,这取决于你所选择的初始值");
                tell("于是,我们可以大量的用很多初始值进行迭代优化,从中选取最好的");
            };
            Resize += delegate
            {
                bit = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
                draw();
            };
        }
        Bitmap bit;
        char[] man = "甲乙丙丁戊己庚辛壬癸".ToCharArray();
        void draw()
        {
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.Black);
            int i, j;
            int w = bit.Width / (size + 1);
            int h = bit.Height / (size + 1);
            g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, w - 2, h - 2));
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), 0, 0, w, h);
            g.DrawString("任务", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.Crimson), new RectangleF(w / 2, 0, w / 2, h / 2));
            g.DrawString("人物", new Font("楷体", 20, FontStyle.Bold), new SolidBrush(Color.DarkOrange), new RectangleF(0, h / 2, w / 2, h / 2));
            for (i = 0; i < size; i++)
            {
                RectangleF rec = new RectangleF(w + w * i, 0, w - 2, h - 2);
                g.FillRectangle(new SolidBrush(Color.Gray), rec);
                g.DrawString((char)(i + 'A') + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Crimson), rec);
            }
            for (i = 0; i < size; i++)
            {
                RectangleF rec = new RectangleF(0, h + i * h, w - 2, h - 2);
                g.FillRectangle(new SolidBrush(Color.Gray), rec);
                g.DrawString(man[i] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.DarkOrange), rec);
            }
            for (i = 0; i < size; i++)
            {
                for (j = 0; j < size; j++)
                {
                    RectangleF rec = new RectangleF(j * w + w, h + i * h, w - 2, h - 2);
                    Color back = work[i] == j ? Color.Gainsboro : Color.Gray;
                    g.FillRectangle(new SolidBrush(back), rec);
                    g.DrawString(a[i, j] + "", new Font("Consolas", 40, FontStyle.Bold), new SolidBrush(Color.Red), rec);
                }
            }
            CreateGraphics().DrawImage(bit, 0, 0);
        }
        int[,] a;
        Random r = new Random();
        int size;
        void init()
        {
            size = 4;
            a = new int[size, size];
            work = new int[size];
            int i, j;
            for (i = 0; i < size; i++) work[i] = i;
            for (i = 0; i < size; i++)
            {
                for (j = 0; j < size; j++)
                {
                    a[i, j] = r.Next() % 20 + 2;
                }
            }
        }
    }

C# Winform代码片段-大二下学期的垃圾代码的更多相关文章

  1. 此文记录了我从研二下学期到研三上学期的找工历程,包括百度、腾讯、网易、移动、电信、华为、中兴、IBM八家企业的面试总结和心得--转

    感谢电子通讯工程的研究生学长为大家整理了这么全面的求职总结,希望进入通信公司和互联网公司做非技术类岗位的学弟学妹们千万不要错过哦~ ---------------------------原文分割线-- ...

  2. springboot+bootstrap实现图书商城管理(大三下学期课程设计)

    在csdn上记一次自己的课程设计过程(已经实习两个月了.感觉这个很容易做.支付可能需要多花点时间.): 在此框架基础之上权限认证管理设置成功:https://blog.csdn.net/weixin_ ...

  3. PHP实用代码片段(二)

    1. 转换 URL:从字符串变成超链接 如果你正在开发论坛,博客或者是一个常规的表单提交,很多时候都要用户访问一个网站.使用这个函数,URL 字符串就可以自动的转换为超链接. function mak ...

  4. 直接拿来用 九个超实用的PHP代码片段(二)

    每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ...

  5. python超实用的30 个简短的代码片段(二)

    Python是目前最流行的语言之一,它在数据科学.机器学习.web开发.脚本编写.自动化方面被许多人广泛使用. 它的简单和易用性造就了它如此流行的原因. 如果你正在阅读本文,那么你或多或少已经使用过P ...

  6. 研二下学期做的第一个项目(主要关于datagridview的一些笔记)

    首先是行标题列rowheader dataGridView1.TopLeftHeaderCell.Value = "details"; ______________________ ...

  7. 大二上学期Javaweb阶段性学习总结

    本学期主要学了h5,css3,js,Java,SQL server数据库基本操作等相关知识,学会了简单web系统的制作. 这个学期总的来说学到了很多东西. 前期Java学习因为有了暑期学习及pta上5 ...

  8. Idea Live Template代码片段总结

    目录 Idea Live Template总结 一.演示 二.详细介绍 2.1 类型 2.2设置(win默认快捷键win+alt+s) 2.3 快捷键 2.4 实战 Idea Live Templat ...

  9. 微信小程序代码片段

    微信小程序代码片段是一种可分享的小项目,可用于分享小程序和小游戏的开发经验.展示组件和 API 的使用.复现开发问题等等.分享代码片段会得到一个链接,所有拥有此分享链接的人可以在工具中导入此代码片段. ...

随机推荐

  1. Rafy 领域实体框架 - 公司内部培训视频

    本月给公司内部一个项目做架构重构,其中使用到了 Rafy 框架.所以我培训了 Rafy 领域实体框架的使用方法,过程中录制了视频,方便其他同事查看.现在把视频放到园里来分享下,有兴趣的朋友可以看看,有 ...

  2. 修复 Visual Studio Error “No exports were found that match the constraint”

    清空Visual Studio 文件缓存目录 Just delete or rename this folder: %AppData%\..\Local\Microsoft\VisualStudio\ ...

  3. C#开发微信门户及应用(6)--微信门户菜单的管理操作

    前面几篇继续了我自己对于C#开发微信门户及应用的技术探索和相关的经验总结,继续探索微信API并分享相关的技术,一方面是为了和大家对这方面进行互动沟通,另一方面也是专心做好微信应用的底层技术开发,把基础 ...

  4. MyBatis通过JDBC生成的执行语句问题

    我们编程的过程中大部分使用了很出色的ORM框架,例如:MyBatis,Hibernate,SpringJDBC,但是这些都离不开数据驱动JDBC的支持.虽然使用起来很方便,但是碰到一些问题确实很棘手, ...

  5. python推荐淘宝物美价廉商品

    完成的目标: 输入搜索的商品 以及 淘宝的已评价数目.店铺的商品描述(包括如实描述.服务态度.快递的5.0打分): 按要求,晒选出要求数量的结果,并按"物美价廉算法"排序后输出 思 ...

  6. 关于Java泛型的使用

    在目前我遇到的java项目中,泛型应用的最多的就属集合了.当要从数据库取出多个对象或者说是多条记录时,往往都要使用集合,那么为什么这么使用,或者使用时有什么要注意的地方,请关注以下内容. 感谢Wind ...

  7. 关于i++引出的线程不安全性的分析以及解决措施

    Q:i++是线程安全的吗? A:如果是局部变量,那么i++是线程安全. 如果是全局变量,那么i++不是线程安全的. 理由:如果是局部变量,那么i++是线程安全:局部变量其他线程访问不到,所以根本不存在 ...

  8. 9.2.1 .net framework下的MVC 控件的封装(上)

    在写.net core下mvc控件的编写之前,我先说一下.net framework下我们MVC控件的做法. MVC下控件的写法,主要有如下三种,最后一种是泛型的写法,mvc提供的控件都是基本控件. ...

  9. elasticsearch高级配置二----线程池设置

    一个Elasticsearch节点会有多个线程池,但重要的是下面四个: 索引(index):主要是索引数据和删除数据操作(默认是cached类型) 搜索(search):主要是获取,统计和搜索操作(默 ...

  10. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...