七. 光照效果
原理: 對圖像中的某一范圍內的像素的亮度分別進行處理.
效果圖:
實現代碼:
光照效果
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));
}
}
}