本文实例为大家分享了C#图像处理的具体代码,供大家参考,具体内容如下
(1)在Form1窗体中的PictureBox1控件中显示通过OpenFileDialog指定的图像文件内容。
将SizeMode设置成StretchImage
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.Filter = "所有文件|*.*"; if (open.ShowDialog() == DialogResult.OK) { Bitmap im = new Bitmap(open.FileName); pictureBox1.Image = im; //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } }
(2)编制一个图像格式转换的应用程序,实现BMP图像与JPEG图像的格式转换。
private void bMPToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap box = new Bitmap(pictureBox1.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "bmp|*.bmp"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Bmp); } private void jPEGToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap box = new Bitmap(pictureBox1.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "jpeg文件|*.jpeg"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Jpeg); }
(3)在窗体中添加两个PictureBox,然后利用OpenFileDialog控件选择一彩色图像文件并在pictureBox1中加载,然后将这一彩色图像转换为灰度图像,并在pictureBox2中进行显示,将此图像保存为a.bmp。
private void 灰度ToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; Color c = new Color(); Bitmap b1 = new Bitmap(pictureBox1.Image); int rr, gg, bb, cc; for (int i = 0; i < pictureBox1.Image.Width; i++) { for (int j = 0; j < pictureBox1.Image.Height; j++) { c = b1.GetPixel(i, j); rr = c.R; gg = c.G; bb = c.B; cc = (int)((rr + gg + bb) / 3); if (cc < 0) cc = 0; if (cc > 255) cc = 255; Color nc = Color.FromArgb(cc, cc, cc); b1.SetPixel(i, j, nc); } } pictureBox2.Refresh(); pictureBox2.Image = b1; } private void 灰度图像存储ToolStripMenuItem_Click(object sender, EventArgs e) { if (pictureBox2.Image != null) { Bitmap box = new Bitmap(pictureBox2.Image); SaveFileDialog sv1 = new SaveFileDialog(); sv1.Filter = "bmp文件|*.bmp"; sv1.ShowDialog(); string str = sv1.FileName; box.Save(str, System.Drawing.Imaging.ImageFormat.Bmp); //pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; } else MessageBox.Show("没有生成灰度图像"); }
(4)将(3)中制作的a.bmp图像加载到PictureBox1,把a.bmp转换为二值图像,并在pictureBox2中进行显示。
private void 转化ToolStripMenuItem_Click(object sender, EventArgs e) { //Bitmap m = new Bitmap(pictureBox1.Width, pictureBox1.Height); Bitmap map=new Bitmap(pictureBox1.Image); Color c; for (int i = 0; i < map.Height; i++) { for (int j = 0; j < map.Width; j++) { c = map.GetPixel(j, i); if (c.R > 100) c = Color.FromArgb(255, 255, 255); //红色分量值大于100则设成白色 else c = Color.Black; map.SetPixel(j, i, c); } } pictureBox2.Image = map; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; }
(5)建立对比度增强算法,并对a.bmp图像进行对比度增强,显示在pictureBox2中显示出来,与PictureBox1中的a.bmp图像进行比较。
private void 对比ToolStripMenuItem_Click(object sender, EventArgs e) { Color c = new Color(); Bitmap box1 = new Bitmap(pictureBox1.Image); int lev=30, wid=170; int[] map = new int[256]; for(int i=0;i<=lev;i++) { map[i]=0; } for (int i=lev;i<=wid;i++) //对比度增强 { map[i] = (int)((i * 1.0 - lev) / wid * 255); } for (int i = lev + wid; i < 256; i++) { map[i] = 255; } int gray; for (int i = 0; i < box1.Width; i++) { for (int j = 0; j < box1.Height; j++) { c = box1.GetPixel(i, j); gray = c.R; c = Color.FromArgb(map[gray], map[gray], map[gray]); box1.SetPixel(i, j, c); } } pictureBox2.Image = box1; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; }
灰度图像:
二值图像:
对比度增强:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。