C#实现图片特效 ( 六)

来源:互联网 发布:防sql注入。asp 编辑:程序博客网 时间:2024/04/30 11:34

C#实现图片特效 ( 六)

七. 光照效果

原理: 对图像中的某一范围内的像素的亮度分别进行处理.

效果图:

实现代码:

光照效果  private void button1_Click(object sender, EventArgs e){//以光照效果显示图像Graphics MyGraphics = this.pictureBox1.CreateGraphics();MyGraphics.Clear(Color.White);Bitmap MyBmp = new Bitmap(this.pictureBox1.Image, this.pictureBox1.Width, this.pictureBox1.Height);int MyWidth = MyBmp.Width;int MyHeight = MyBmp.Height;Bitmap MyImage = MyBmp.Clone(new RectangleF(0, 0, MyWidth, MyHeight), System.Drawing.Imaging.PixelFormat.DontCare);int A = Width / 2;int B = Height / 2;//MyCenter图片中心点,发亮此值会让强光中心发生偏移Point MyCenter = new Point(MyWidth / 2, MyHeight / 2);//R强光照射面的半径,即”光晕” int R = Math.Min(MyWidth / 2, MyHeight / 2);for (int i = MyWidth - 1; i >= 1; i--){for (int j = MyHeight - 1; j >= 1; j--){float MyLength = (float)Math.Sqrt(Math.Pow((i - MyCenter.X), 2) + Math.Pow((j - MyCenter.Y), 2));//如果像素位于”光晕”之内 if (MyLength < R){Color MyColor = MyImage.GetPixel(i, j);int r, g, b;//220亮度增加常量,该值越大,光亮度越强 float MyPixel = 220.0f * (1.0f - MyLength / R);r = MyColor.R + (int)MyPixel;r = Math.Max(0, Math.Min(r, 255));g = MyColor.G + (int)MyPixel;g = Math.Max(0, Math.Min(g, 255));b = MyColor.B + (int)MyPixel;b = Math.Max(0, Math.Min(b, 255));//将增亮后的像素值回写到位图Color MyNewColor = Color.FromArgb(255, r, g, b);MyImage.SetPixel(i, j, MyNewColor);}}//重新绘制图片MyGraphics.DrawImage(MyImage, new Rectangle(0, 0, MyWidth, MyHeight));} }}