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();
            }
        }
    }
}

原文地址:https://www.cnblogs.com/markli/p/3573740.html