zoukankan      html  css  js  c++  java
  • C/S love自编程序

    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.Imaging;
    using System.Drawing.Drawing2D;

    namespace LOVE
    {
        public partial class Form1 : Form
        {
            private int screenWidth = 0;
            private int screenHeight = 0;
            private int imgWidth = 255;
            private int imgHeight = 240;
            private Random random = new Random();
            private Dictionary<int, Bitmap> imgs = new Dictionary<int, Bitmap>();
            public Form1()
            {
                InitializeComponent();
                this.TransparencyKey = this.BackColor;

                screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
                screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
                this.WindowState = FormWindowState.Maximized;

                imgs.Add(1, global::LOVE.Properties.Resources.heart01);
                imgs.Add(2, global::LOVE.Properties.Resources.heart02);
                imgs.Add(3, global::LOVE.Properties.Resources.heart03);
                imgs.Add(4, global::LOVE.Properties.Resources.heart04);
                imgs.Add(5, global::LOVE.Properties.Resources.heart05);
                imgs.Add(6, global::LOVE.Properties.Resources.heart06);
                imgs.Add(7, global::LOVE.Properties.Resources.heart07);
                imgs.Add(8, global::LOVE.Properties.Resources.heart08);
                imgs.Add(9, global::LOVE.Properties.Resources.heart09);

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                Timer _timer = new Timer();
                _timer.Tick += _timer_Tick;
                _timer.Interval = 500;
                _timer.Start();
            }

            void _timer_Tick(object sender, EventArgs e)
            {

                Bitmap bitmap = imgs[random.Next(1, 9)];
                GraphicsPath g = subGraphicsPath(bitmap);

                PictureBox pcb = new PictureBox();
                pcb.BackColor = System.Drawing.Color.Transparent;
                //pcb.Image =
                pcb.Image = bitmap;
                int x = random.Next(imgWidth, screenWidth) - imgWidth;
                int y = random.Next(imgHeight, screenHeight) - imgHeight;
                pcb.Size = new Size(imgWidth, imgHeight);
                pcb.Location = new Point(x, y);

                pcb.Region = new Region(g);
                this.Controls.Add(pcb);
            }

            /// <summary>
            /// 根据图片计算GraphicsPath路径
            /// </summary>
            /// <param name="img">gif或者png图片</param>
            /// <returns>图片不透明区域路径</returns>
            public unsafe static GraphicsPath subGraphicsPath(Image img)
            {
                if (img == null) return null;

                // 建立GraphicsPath, 给我们的位图路径计算使用
                GraphicsPath g = new GraphicsPath(FillMode.Alternate);

                Bitmap bitmap = new Bitmap(img);

                int width = bitmap.Width;
                int height = bitmap.Height;
                BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                byte* p = (byte*)bmData.Scan0;
                int offset = bmData.Stride - width * 3;
                int p0, p1, p2;         // 记录左上角0,0座标的颜色值
                p0 = p[0];
                p1 = p[1];
                p2 = p[2];

                int start = -1;
                // 行座标 ( Y col )
                for (int Y = 0; Y < height; Y++)
                {
                    // 列座标 ( X row )
                    for (int X = 0; X < width; X++)
                    {
                        if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2))     //如果 之前的点没有不透明 且 不透明
                        {
                            start = X;                            //记录这个点
                        }
                        else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2))      //如果 之前的点是不透明 且 透明
                        {
                            g.AddRectangle(new Rectangle(start, Y, X - start, 1));    //添加之前的矩形到
                            start = -1;
                        }

                        if (X == width - 1 && start > -1)        //如果 之前的点是不透明 且 是最后一个点
                        {
                            g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1));      //添加之前的矩形到
                            start = -1;
                        }
                        //if (p[0] != p0 || p[1] != p1 || p[2] != p2)
                        //    g.AddRectangle(new Rectangle(X, Y, 1, 1));
                        p += 3;                                   //下一个内存地址
                    }
                    p += offset;
                }
                bitmap.UnlockBits(bmData);
                bitmap.Dispose();
                // 返回计算出来的不透明图片路径
                return g;
            }

            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                    this.Dispose();
                }
            }
        }
    }

  • 相关阅读:
    jquery 获取easyui combobox选中的值
    一个多余逗号引起的麻烦
    Microsoft.Office.Interop.Excel 放到B/S客户端失败问题 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。
    自己收藏-javascript用window.open的子窗口关闭自己并且刷新父窗口
    easyUI datagrid 不刷新问题
    水晶报表中公式字段if else 语句无法正常执行的问题
    SQL SERVER 察看数据库连接池情况
    Data Table 转 List<Type>
    .Net 调用SAP RFC
    VS2017 插件介绍
  • 原文地址:https://www.cnblogs.com/markli/p/3573740.html
Copyright © 2011-2022 走看看