{"id":25649594,"url":"https://github.com/tuyile006/cshpargdiparticle","last_synced_at":"2025-07-26T01:05:07.500Z","repository":{"id":278573056,"uuid":"936026989","full_name":"tuyile006/CShparGDIParticle","owner":"tuyile006","description":"使用C#Winform程序实现的一系列粒子效果，主要是实验一下C#+GDI写动画效果的能力。使用了双缓存和GDI+，界面还是很丝滑的。一共写了6个例子特效动画，欢迎大家补充。","archived":false,"fork":false,"pushed_at":"2025-02-20T13:38:25.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-20T14:34:06.486Z","etag":null,"topics":["csharp","gdi"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuyile006.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-20T12:11:28.000Z","updated_at":"2025-02-20T13:43:43.000Z","dependencies_parsed_at":"2025-02-20T14:44:32.922Z","dependency_job_id":null,"html_url":"https://github.com/tuyile006/CShparGDIParticle","commit_stats":null,"previous_names":["tuyile006/cshpargdiparticle"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuyile006%2FCShparGDIParticle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuyile006%2FCShparGDIParticle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuyile006%2FCShparGDIParticle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuyile006%2FCShparGDIParticle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuyile006","download_url":"https://codeload.github.com/tuyile006/CShparGDIParticle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240325207,"owners_count":19783634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","gdi"],"created_at":"2025-02-23T14:22:39.122Z","updated_at":"2025-07-26T01:05:07.482Z","avatar_url":"https://github.com/tuyile006.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CShparGDIParticle\n\n使用C#Winform程序实现的一系列粒子效果，主要是实验一下C#+GDI写动画效果的能力。使用了双缓存和GDI+，界面还是很丝滑的。一共写了6个例子特效动画，欢迎大家补充。\n\n部分界面如下：\n\n![界面1](https://img2024.cnblogs.com/blog/15080/202502/15080-20250204185052815-1030962580.png)\n![鼓泡泡](https://img2024.cnblogs.com/blog/15080/202502/15080-20250204190540726-1568052893.gif)\n![文字粒子](https://img2024.cnblogs.com/blog/15080/202502/15080-20250204190508480-1559521088.gif)\n![爱心](https://img2024.cnblogs.com/blog/15080/202502/15080-20250204190711142-724705712.gif)\n\n写完之后感受就是，C#也是可以写出炫酷的粒子效果的，而且不卡顿很丝滑。\n\n其中几个关键点：\n\n**1. 窗体设置双缓存：**\n\n```C#\npublic Form1()\n {\n     DoubleBuffered = true;  //设置双缓冲\n     InitializeComponent();\n }\n```\n\n**2. 在Paint事件中重绘粒子，不要在While（true）之类的循环里无间隔调用**。Winform中的Paint事件就相当于JavaScript中的requestAnimationFrame事件。\n\n**3. 每次重绘调用的方法统一返回一张Bitmap图片**，换句话说就是把全部的粒子画到一张Bitmap中，不能直接用pictureBox1.CreateGraphics()的Graphics对象来画粒子。否则会出现卡顿。而且这个**Bitmap**要用公共变量，不能每次调用都重新创建，否则**内存会疯涨**。\n\n**4.取像素点的数据要用内存拷贝法**，不可直接调用img.GetPixel(i ,j).R   ，否则性能极差，粒子一多也会出现卡顿。\n\n内存拷贝法取像素点代码如下：注意其中的Data.Stride属性，用LockBits返回的像素数据每一行会有个补全操作，若采用Format24bppRgb格式也就是每个像素占用24位，3个字节分别表示B，G，R，且每行长度为Stride，不足Stride的会补全。\n\n```C#\nBitmap bmp=new Bitmap(w,h, PixelFormat.Format24bppRgb);\n BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);\n int length = h * data.Stride;\n byte[] RGB = new byte[length];\n System.IntPtr Scan0 = data.Scan0;\n System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length);\n \n for (int y = 0; y \u003c h; y++)\n {\n     int index = y * data.Stride;\n     for (int x = 0; x \u003c w; x++)\n     {\n         if (x % span == 0 \u0026\u0026 y % span == 0)\n         {\n             particles.Add(new ParticleObj()\n             {\n                 x = x,\n                 y = y,\n                 vx = x,\n                 vy = y,\n                 vspeed = 2\n             });\n             //改变颜色。\n             RGB[index + 3 * x] = 255;\n             RGB[index + 3 * x+1] = 255;\n             RGB[index + 3 * x+2] = 255;\n         }\n     }\n }\n System.Runtime.InteropServices.Marshal.Copy(RGB, 0, Scan0, length);\n bmp.UnlockBits(data);\n g.DrawImage(bmp,0,0);\n```\n\n欢迎大家补充，实现更多有想象力的粒子效果。\n\n效果见：[更多效果图](https://www.cnblogs.com/tuyile006/p/18698404)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuyile006%2Fcshpargdiparticle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuyile006%2Fcshpargdiparticle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuyile006%2Fcshpargdiparticle/lists"}