2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 【C#】图片处理——浮雕 黑白 油画效果源码

【C#】图片处理——浮雕 黑白 油画效果源码

时间:2018-11-08 17:15:59

相关推荐

【C#】图片处理——浮雕 黑白 油画效果源码

本例应用场合:对图片进行基本的翻转操作,图片浮雕、黑白、油画效果的实现。

本例编译环境VS及以上版本编译通过。

运行效果:

运行动态效果

主要功能:

图片浮雕、黑白、油画效果处理;

图片翻转处理——旋转180°,顺时针、逆时针旋转90°,垂直、水平翻转;

图片保存、另存为。

主要代码:

/// <summary/// 浮雕效果/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnFudiao_Click(object sender, EventArgs e){label1.Text = "正在完成图片操作,可能会出现程序未响应的情况。请稍等一段时间,如果等待时间过长,请重新启动程序!";shangyibu = new Bitmap(pictureBox1.Image);Thread.Sleep(500);try{Bitmap newBitmap = new Bitmap(intWidth, intHeight);Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;Color pixel1, pixel2;for (int x = 0; x < intWidth - 1; x++){for (int y = 0; y < intHeight - 1; y++){int r = 0, g = 0, b = 0;pixel1 = oldBitmap.GetPixel(x, y);pixel2 = oldBitmap.GetPixel(x + 1, y + 1);r = Math.Abs(pixel1.R - pixel2.R + 128);g = Math.Abs(pixel1.G - pixel2.G + 128);b = Math.Abs(pixel1.B - pixel2.B + 128);if (r > 255){r = 255;}if (r < 0){r = 0;}if (g > 255){g = 255;}if (g < 0){g = 0;}if (b > 255){b = 255;}if (b < 0){b = 0;}newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));}}this.pictureBox1.Image = newBitmap;label1.Text = "已完成图片操作:“浮雕”!";save = 1;this.Text = "*图片处理——" + open;}catch (Exception ex){MessageBox.Show(ex.Message);}}

/// <summary>/// 黑白效果/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnHeibai_Click(object sender, EventArgs e){label1.Text = "正在完成图片操作,可能会出现程序未响应的情况。请稍等一段时间,如果等待时间过长,请重新启动程序!";shangyibu = new Bitmap(pictureBox1.Image);Thread.Sleep(500);try{Bitmap newBitmap = new Bitmap(intWidth, intHeight);Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;Color pixel;for (int x = 0; x < intWidth; x++){for (int y = 0; y < intHeight; y++){pixel = oldBitmap.GetPixel(x, y);int r, g, b, Result = 0;r = pixel.R;g = pixel.G;b = pixel.B;int iType = 2;switch (iType){case 0:Result = ((r + g + b) / 3);break;case 1:Result = r > g ? r : g;Result = Result > b ? Result : b;break;case 2:Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));break;}newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));}}this.pictureBox1.Image = newBitmap;label1.Text = "已完成图片操作:“黑白”!";save = 1;this.Text = "*图片处理——" + open;}catch (Exception ex){MessageBox.Show(ex.Message);}}

/// <summary>/// 保存图片/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void saveToolStripMenuItem_Click(object sender, EventArgs e){System.IO.FileStream picture = null;try{Bitmap bmp = new Bitmap(pictureBox1.Image);s = new Bitmap(pictureBox1.Image);bmp.Save(open);MessageBox.Show("保存成功");label1.Text = "保存成功!";this.Text = "图片处理——" + open;baocun = new Bitmap(pictureBox1.Image);save = 0;save = 0;this.Text = "图片处理——" + open;}catch (Exception ex){MessageBox.Show(ex.Message);label1.Text = "保存失败!";}finally{if (picture != null){picture.Close();picture.Dispose();}}}

/// <summary>/// 还原图片/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnHuanyuan_Click(object sender, EventArgs e){DialogResult aaa = MessageBox.Show("你确定要还原为未修改过的原照片吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);if (aaa == DialogResult.Yes){pictureBox1.Image=huanyuan;if (pictureBox1.Image != s){save = 1;this.Text = "*图片处理——" + open;}else{save = 0;this.Text = "图片处理——" + open;}}}

图片的打开、保存、翻转功能详见源代码。

源代码免费下载地址请关注公众号【几行简码】后回复【图片处理】获取。

原创文章,转载请阅读公众号内转载须知。

点关注,不迷路。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。