No 69 · 以光照效果显示图像
来源:互联网 发布:win8系统盘克隆软件 编辑:程序博客网 时间:2024/05/19 06:37
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Ex13_13{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { //打图像文件 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp|Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像文件(*.png)| *.png |所有文件(*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { Bitmap MyBitmap = new Bitmap(openFileDialog.FileName); this.pictureBox1.Image = MyBitmap; } } 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)); } } }}