软件编程
位置:首页>> 软件编程>> C#编程>> C#实现绘制浮雕图片效果实例

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

作者:shichen2014  发布时间:2023-10-10 04:34:04 

标签:C#,绘制

本文采用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());
}
}
}

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com