本篇文章给大家分享的是有关如何在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; }
C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
以上就是如何在C#中处理图像,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。