png图片叠加 如果使用picturebox控件无法实现透明效果叠加,即便将背景设置为Translate,只会让控件颜色和背景颜色一样,并不会真的透明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var image1 = Image.FromFile("image1.png" );var image2 = Image.FromFile("image2.png" );var bitmap = new Bitmap(this .Width, this .Height);using (var graphics = Graphics.FromImage(bitmap)){ graphics.DrawImage(image1, new Rectangle(0 , 0 , this .Width, this .Height)); graphics.DrawImage(image2, new Rectangle(0 , 0 , this .Width, this .Height)); } this .BackgroundImage = bitmap;
第2张图片实现动态效果 错误代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Drawing;namespace UIDesign { public partial class CrystallizerUI : Form { private Image _image1; private Image _image2; private int _image2Height = 0 ; Timer timer = new Timer(); private Bitmap bitmap; public CrystallizerUI () { InitializeComponent(); _image1 = Image.FromFile("Resources/zj.png" ); _image2 = Image.FromFile("Resources/1.png" ); bitmap = new Bitmap(this .Width, this .Height); this .Size = new Size(_image1.Width, _image1.Height); this .BackColor = Color.White; timer.Interval = 20 ; timer.Tick += new EventHandler(OnTimerTick); timer.Start(); } private void OnTimerTick (object sender, EventArgs e ) { if (_image2Height > 300 ) { timer1.Stop(); timer1.Enabled = false ; timer1.Dispose(); } _image2Height += 10 ; if (_image2Height >= _image2.Height) { _image2Height = _image2.Height; } using (var graphics = Graphics.FromHwnd(this .Handle)) { graphics.DrawImage(_image1, new Rectangle(0 , 0 , this .Width, this .Height)); var rect = new Rectangle(0 , 0 , _image2.Width, _image2Height); graphics.DrawImage(_image2, rect, rect, GraphicsUnit.Pixel); } } } }
如果你将图片放在 Visual Studio 项目的资源文件夹中,可以使用 Properties.Resources 类来引用图片资源。