C#实现绘制浮雕图片效果实例
本文采用C#实例讲解了处理图片为浮雕效果的实现方法,这在PS中是一个常见的功能,也是C#中的一个简单的图像处理例子。程序先读取原图,然后依次访问每个像素的RGB值,获取相邻两个像素的R、G、B值,计算与左上角像素的RGB分量之差,将计算后的RGB值回写到位图,最后进行图片的浮雕处理。
主要代码如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Drawing.Imaging;
namespaceEmbossColander
{
publicclassForm1:System.Windows.Forms.Form
{
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
InitializeComponent();
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
privatevoidInitializeComponent()
{
this.components=newSystem.ComponentModel.Container();
this.Size=newSystem.Drawing.Size(350,200);
this.Text="Form1";
}
#endregion
protectedoverridevoidOnPaint(PaintEventArgse)
{
base.OnPaint(e);
Graphicsgraphics=e.Graphics;
graphics.Clear(Color.White);
graphics.ScaleTransform(0.7f,0.7f);
Bitmapimage=newBitmap("dog.bmp");
intWidth=image.Width;
intHeight=image.Height;
//image2:进行雕刻处理
Bitmapimage2=image.Clone(newRectangle(0,0,Width,Height),PixelFormat.DontCare);
//绘制原图
graphics.DrawImage(
image,newRectangle(0,0,Width,Height));
Colorcolor,colorTemp,colorLeft;
//进行图片的浮雕处理
//依次访问每个像素的RGB值
for(inti=Width-1;i>0;i--)
{
for(intj=Height-1;j>0;j--)
{
//获取相邻两个像素的R、G、B值
color=image.GetPixel(i,j);
colorLeft=image.GetPixel(i-1,j-1);
//计算与左上角像素的RGB分量之差
//67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
intr=Math.Max(67,Math.Min(255,
Math.Abs(color.R-colorLeft.R+128)));
intg=Math.Max(67,Math.Min(255,
Math.Abs(color.G-colorLeft.G+128)));
intb=Math.Max(67,Math.Min(255,
Math.Abs(color.B-colorLeft.B+128)));
ColorcolorResult=Color.FromArgb(255,r,g,b);
//将计算后的RGB值回写到位图
image.SetPixel(i,j,colorResult);
}
//绘制浮雕图
graphics.DrawImage(
image,newRectangle(Width+10,0,Width,Height));
}
}
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
}
}
感兴趣的朋友可以点此本站下载完整实例代码。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短