C#实现绘制浮雕图片效果实例

所属分类: 软件编程 / C#教程 阅读数: 71
收藏 0 赞 0 分享

本文采用C#实例讲解了处理图片为浮雕效果的实现方法,这在PS中是一个常见的功能,也是C#中的一个简单的图像处理例子。程序先读取原图,然后依次访问每个像素的RGB值,获取相邻两个像素的R、G、B值,计算与左上角像素的RGB分量之差,将计算后的RGB值回写到位图,最后进行图片的浮雕处理。

主要代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace EmbossColander
{
 public class Form1 : System.Windows.Forms.Form
 {
 private System.ComponentModel.Container components = null;
 public Form1()
 {
  InitializeComponent();
 }
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
  if (components != null)
  {
   components.Dispose();
  }
  }
  base.Dispose( disposing );
 }
 #region Windows 窗体设计器生成的代码
 private void InitializeComponent()
 {
  this.components = new System.ComponentModel.Container();
  this.Size = new System.Drawing.Size(350,200);
  this.Text = "Form1";
 }
 #endregion
 protected override void OnPaint(PaintEventArgs e)
 {
  base.OnPaint (e);
  Graphics graphics = e.Graphics;
  graphics.Clear(Color.White);
  graphics.ScaleTransform(0.7f,0.7f);
  Bitmap image = new Bitmap("dog.bmp");
  int Width = image.Width;
  int Height = image.Height;
  //image2:进行雕刻处理
  Bitmap image2 = image.Clone(new Rectangle(0,0,Width,Height),PixelFormat.DontCare );
  //绘制原图
  graphics.DrawImage(
  image, new Rectangle(0, 0, Width, Height));
  Color color, colorTemp,colorLeft;
  //进行图片的浮雕处理
  //依次访问每个像素的RGB值
  for(int i=Width-1; i>0;i--)
  {
  for( int j=Height-1; j>0;j--)
  {
   //获取相邻两个像素的R、G、B值
   color =image.GetPixel(i, j);
   colorLeft=image.GetPixel(i-1, j-1);
   //计算与左上角像素的RGB分量之差
   //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
   int r = Math.Max(67,Math.Min(255,
   Math.Abs(color.R-colorLeft.R+128)));
   int g = Math.Max(67,Math.Min(255,
   Math.Abs(color.G-colorLeft.G+128)));
   int b = Math.Max(67,Math.Min(255,
   Math.Abs(color.B-colorLeft.B+128)));
   Color colorResult=Color.FromArgb(255,r,g,b);
   //将计算后的RGB值回写到位图
   image.SetPixel(i, j,colorResult);
  }
  //绘制浮雕图
  graphics.DrawImage(
   image, new Rectangle(Width+10, 0, Width, Height));
  }
 }
 [STAThread]
 static void Main()
 {
  Application.Run(new Form1());
 }
 }
}

感兴趣的朋友可以点此本站下载完整实例代码。

更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多