using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//Author: Larry Nung
//Date: 2009/6/2
//File:
//Memo:
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// <summary>
///
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//***************************************************************************
//Author: Larry Nung
//Date: 2009/6/2
//Purpose:
//Memo:
//***************************************************************************
/// <summary>
/// Handles the Click event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = GetGrayBitmap(openFileDialog1.FileName);
}
} '更多.net源码和教程,来自[乐博网 www.lob.cn]
//***************************************************************************
//Author: Larry Nung
//Date: 2009/6/2
//Purpose:
//Memo:
//***************************************************************************
/// <summary>
/// Gets the gray bitmap.
/// </summary>
/// <param name="file">The file.</param>
/// <returns></returns>
private Bitmap GetGrayBitmap(string file)
{
Bitmap bmp = new Bitmap(file);
for (int x=0; x < bmp.Width; x++)
{
for (int y=0; y < bmp.Height; y++)
{
Color color = bmp.GetPixel(x, y);
int gray = (color.R + color.G + color.B) / 3;
bmp.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
}
}
return bmp;
}
}
}