c# 光学三原色混合,颜色叠加-dong
东的备注:
光的三原色:红、绿、蓝
红+绿=黄
红+蓝=品红
蓝+绿=青
红+绿+蓝=白
无颜色为黑
下看代码
Bitmap image1 = new Bitmap(500, 500);//红 Bitmap image2 = new Bitmap(500, 500);//绿 Bitmap image3 = new Bitmap(500, 500);//蓝 Bitmap image4 = new Bitmap(500, 500);//混合 Random ran = new Random(); Graphics g = Graphics.FromImage(image1); SolidBrush brush = new SolidBrush(Color.FromArgb(255,255,0,0));//请勿使用Color.Red 混合后存在色差 SolidBrush brushBackGroud = new SolidBrush(Color.FromArgb(255, 0, 0, 0)); g.FillRectangle(brushBackGroud, new Rectangle(0, 0, 500, 500)); for (int i = 0; i < 10; i++) { int x= ran.Next(10, 490); int y = ran.Next(10, 490); int width = 80; int height = 80; g.FillEllipse(brush,x,y,width,height); } g = Graphics.FromImage(image2); brush = new SolidBrush(Color.FromArgb(255, 0, 255, 0));//请勿使用Color.Green 混合后存在色差 brushBackGroud = new SolidBrush(Color.FromArgb(255, 0, 0, 0)); g.FillRectangle(brushBackGroud, new Rectangle(0, 0, 500, 500)); for (int i = 0; i < 10; i++) { int x = ran.Next(10, 490); int y = ran.Next(10, 490); int width = 80; int height = 80; g.FillEllipse(brush, x, y, width, height); } g = Graphics.FromImage(image3); brush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));//请勿使用Color.Blue 混合后存在色差 brushBackGroud = new SolidBrush(Color.FromArgb(255, 0, 0, 0)); g.FillRectangle(brushBackGroud, new Rectangle(0, 0, 500, 500)); for (int i = 0; i < 10; i++) { int x = ran.Next(10, 490); int y = ran.Next(10, 490); int width = 80; int height = 80; g.FillEllipse(brush, x, y, width, height); } //混合 for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { image4.SetPixel(i, j, Color.FromArgb(255, image1.GetPixel(i, j).R > 128 ? 255 : 0, image2.GetPixel(i, j).G > 128 ? 255 : 0, image3.GetPixel(i, j).B > 128 ? 255 : 0)); } } pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.Image = image1; pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; pictureBox2.Image = image2; pictureBox3.SizeMode = PictureBoxSizeMode.Zoom; pictureBox3.Image = image3; pictureBox4.SizeMode = PictureBoxSizeMode.Zoom; pictureBox4.Image = image4;
最终实现效果

浙公网安备 33010602011771号