您的位置:首页 > 其它

实验一彩色图像转灰度

2014-04-17 13:10 204 查看
使用VS2008 开发工具,c# 编程语言条件下,进行图像显示,将真彩色图转化为灰度图。创建一个基本窗体应用程序,在窗体上添加一个openFileDialog 和pictureBox 控件,两个button 控件,一个label 标签,在窗体代码中添加变量声明:
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;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;

namespace TextOne
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Bitmap bp_1;
public static Bitmap bp_grey;
public static MemoryStream ms_bmp;
public static long bmp_file_len;//文件长度变量
public static IntPtr main_wnd_handle;//主窗体句柄
//定义消息常数
public const int TRAN_FINISHED = 0x500;
//动态链接库引入
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);

private void Form1_Load(object sender, EventArgs e)
{
ms_bmp = new MemoryStream(5000000);
main_wnd_handle = this.Handle;
}
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case TRAN_FINISHED:
label1.Text = " 图片灰度处理完成";
pictureBox1.Image = bp_grey;
break;
default:
base.DefWndProc(ref m);
break;
}
}

private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{//选择文件
bp_1 = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = bp_1;
}
}

static void thread_bmp_2_grey()
{
//线程流程
if (bp_1!=null)
{
ms_bmp.Seek(0, SeekOrigin.Begin);
bp_1.Save(ms_bmp, System.Drawing.Imaging.ImageFormat.Bmp);
bmp_file_len = ms_bmp.Position;
//RGB2GRAY(r,g,b) (((b)*117 + (g)*601 + (r)*306) >> 10)
byte[] buf_ms = ms_bmp.GetBuffer();
byte r_1, g_1, b_1, grey_val;
int scan_line_len;
switch (bp_1.PixelFormat)
{
case PixelFormat.Format32bppRgb:
{
scan_line_len = bp_1.Width * 4;
for (int i_height = 0; i_height < bp_1.Height; i_height++)
{
for (int i_width = 0; i_width < bp_1.Width; i_width++)
{
b_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4];
g_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4 + 1];
r_1 = buf_ms[54 + i_height * scan_line_len + i_width * 4 + 2];
grey_val = (byte)(((b_1) * 117 + (g_1) * 601 + (r_1) * 306) >> 10);
buf_ms[54 + i_height * scan_line_len + i_width * 4] = grey_val;
buf_ms[54 + i_height * scan_line_len + i_width * 4 + 1] = grey_val;
buf_ms[54 + i_height * scan_line_len + i_width * 4 + 2] = grey_val;
}
}
break;
}
case PixelFormat.Format24bppRgb:
{
int i_bmp_width = bp_1.Width;
int line_byte_count;
//line_byte_count=i_bmp_width*3;24 位需要处理
line_byte_count = i_bmp_width * 3;//32 位直接计算
if ((line_byte_count % 4) == 0)
{
scan_line_len = line_byte_count;
}
else
{
scan_line_len = (line_byte_count / 4) * 4 + 4;
}
//24 位位图,因此每像素使用24 字节,没有alpha 字节
for (int i_height = 0; i_height < bp_1.Height; i_height++)
{
for (int i_width = 0; i_width < bp_1.Width; i_width++)
{
b_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3];
g_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3 + 1];
r_1 = buf_ms[54 + i_height * scan_line_len + i_width * 3 + 2];
grey_val = (byte)(((b_1) * 117 + (g_1) * 601 + (r_1) * 306) >> 10);
buf_ms[54 + i_height * scan_line_len + i_width * 3] = grey_val;
buf_ms[54 + i_height * scan_line_len + i_width * 3 + 1] = grey_val;
buf_ms[54 + i_height * scan_line_len + i_width * 3 + 2] = grey_val;
}//each pixel in one line

}
break;
}
}
bp_grey = (Bitmap)Bitmap.FromStream(ms_bmp);
SendMessage(main_wnd_handle, TRAN_FINISHED, 100, 100);
}
}

private void button2_Click(object sender, EventArgs e)
{
Thread workThread = new Thread(new ThreadStart(thread_bmp_2_grey));
workThread.IsBackground = true;
workThread.Start();
}
}
}


效果图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: