通过本文主要向大家介绍了c#web开发实例,c#串口编程实例,c#窗口界面设计实例,c#实例视频教程,c#项目实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文采用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());
}
}
}
</div>
感兴趣的朋友可以点此本站下载完整实例代码。
</div>
