如果您觉得C#制作的艺术字比较好玩, 但是还觉得没看够,不过瘾,那么我今天就让您一饱眼福, 看看C#如何制作的效果超酷的图像.

(注: 我之前曾写过类似的文章, 但没有原理说明, 代码注释不够详细, 也没有附相应的 Demo...因此如果您觉得好像哪看过类似的文章可以看看我之前写的...)

为了演示后面的效果, 这里有必要先让大家看看今天的原始图片: ISINBAEVA ~~~~~~~~

一. 底片效果
原理: GetPixel方法获得每一点像素的值, 然后再使用SetPixel方法将取反后的颜色值设置到对应的点.
效果图: 

代码实现:

二. 浮雕效果

原理: 对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值.

效果图:

代码实现:

private void button1_Click(object sender, EventArgs e)
{
//以底片效果显示图像
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newbitmap = new Bitmap(Width, Height);
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x = ; x < Width; x++)
{
for (int y = ; y < Height; y++)
{
int r, g, b;
pixel = oldbitmap.GetPixel(x, y);
r = - pixel.R;
g = - pixel.G;
b = - pixel.B;
newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
this.pictureBox1.Image = newbitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

三. 黑白效果

原理: 彩色图像处理成黑白效果通常有3种算法;

(1).最大值法: 使每个像素点的 R, G, B 值等于原像素点的 RGB (颜色值) 中最大的一个;

(2).平均值法: 使用每个像素点的 R,G,B值等于原像素点的RGB值的平均值;

(3).加权平均值法: 对每个像素点的 R, G, B值进行加权

---自认为第三种方法做出来的黑白效果图像最 "真实".

效果图:

代码实现:

 private void button1_Click(object sender, EventArgs e)
{
//以黑白效果显示图像
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap = new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x = ; x < Width; x++)
for (int y = ; y < Height; y++)
{
pixel = oldBitmap.GetPixel(x, y);
int r, g, b, Result = ;
r = pixel.R;
g = pixel.G;
b = pixel.B;
//实例程序以加权平均值法产生黑白图像
int iType =;
switch (iType)
{
case ://平均值法
Result = ((r + g + b) / );
break;
case ://最大值法
Result = r > g ? r : g;
Result = Result > b ? Result : b;
break;
case ://加权平均值法
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;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

四. 柔化效果

原理: 当前像素点与周围像素点的颜色差距较大时取其平均值.

效果图:

代码实现:

 private void button1_Click(object sender, EventArgs e)
{
//以柔化效果显示图像
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height);
Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
//高斯模板
int[] Gauss ={ , , , , , , , , };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
int r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; row++)
{
pixel = MyBitmap.GetPixel(x + row, y + col);
r += pixel.R * Gauss[Index];
g += pixel.G * Gauss[Index];
b += pixel.B * Gauss[Index];
Index++;
}
r /= ;
g /= ;
b /= ;
//处理颜色值溢出
r = r > ? : r;
r = r < ? : r;
g = g > ? : g;
g = g < ? : g;
b = b > ? : b;
b = b < ? : b;
bitmap.SetPixel(x - , y - , Color.FromArgb(r, g, b));
}
this.pictureBox1.Image = bitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

五.锐化效果

原理:突出显示颜色值大(即形成形体边缘)的像素点.

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以锐化效果显示图像
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap = new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
//拉普拉斯模板
int[] Laplacian ={ -, -, -, -, , -, -, -, - };
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
int r = , g = , b = ;
int Index = ;
for (int col = -; col <= ; col++)
for (int row = -; row <= ; row++)
{
pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
g += pixel.G * Laplacian[Index];
b += pixel.B * Laplacian[Index];
Index++;
}
//处理颜色值溢出
r = r > ? : r;
r = r < ? : r;
g = g > ? : g;
g = g < ? : g;
b = b > ? : b;
b = b < ? : b;
newBitmap.SetPixel(x - , y - , Color.FromArgb(r, g, b));
}
this.pictureBox1.Image = newBitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

六. 雾化效果

原理: 在图像中引入一定的随机值, 打乱图像中的像素值

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以雾化效果显示图像
try
{
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newBitmap = new Bitmap(Width, Height);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int x = ; x < Width - ; x++)
for (int y = ; y < Height - ; y++)
{
System.Random MyRandom = new Random();
int k = MyRandom.Next();
//像素块大小
int dx = x + k % ;
int dy = y + k % ;
if (dx >= Width)
dx = Width - ;
if (dy >= Height)
dy = Height - ;
pixel = oldBitmap.GetPixel(dx, dy);
newBitmap.SetPixel(x, y, pixel);
}
this.pictureBox1.Image = newBitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

七. 光照效果

原理: 对图像中的某一范围内的像素的亮度分别进行处理.

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以光照效果显示图像
Graphics MyGraphics = this.pictureBox1.CreateGraphics();
MyGraphics.Clear(Color.White);
Bitmap MyBmp = new Bitmap(this.pictureBox1.Image, this.pictureBox1.Width, this.pictureBox1.Height);
int MyWidth = MyBmp.Width;
int MyHeight = MyBmp.Height;
Bitmap MyImage = MyBmp.Clone(new RectangleF(, , MyWidth, MyHeight), System.Drawing.Imaging.PixelFormat.DontCare);
int A = Width / ;
int B = Height / ;
//MyCenter图片中心点,发亮此值会让强光中心发生偏移
Point MyCenter = new Point(MyWidth / , MyHeight / );
//R强光照射面的半径,即”光晕”
int R = Math.Min(MyWidth / , MyHeight / );
for (int i = MyWidth - ; i >= ; i--)
{
for (int j = MyHeight - ; j >= ; j--)
{
float MyLength = (float)Math.Sqrt(Math.Pow((i - MyCenter.X), ) + Math.Pow((j - MyCenter.Y), ));
//如果像素位于”光晕”之内
if (MyLength < R)
{
Color MyColor = MyImage.GetPixel(i, j);
int r, g, b;
//220亮度增加常量,该值越大,光亮度越强
float MyPixel = 220.0f * (1.0f - MyLength / R);
r = MyColor.R + (int)MyPixel;
r = Math.Max(, Math.Min(r, ));
g = MyColor.G + (int)MyPixel;
g = Math.Max(, Math.Min(g, ));
b = MyColor.B + (int)MyPixel;
b = Math.Max(, Math.Min(b, ));
//将增亮后的像素值回写到位图
Color MyNewColor = Color.FromArgb(, r, g, b);
MyImage.SetPixel(i, j, MyNewColor);
}
}
//重新绘制图片
MyGraphics.DrawImage(MyImage, new Rectangle(, , MyWidth, MyHeight));
} }
}

八.百叶窗效果

原理:(1).垂直百叶窗效果:

根据窗口或图像的高度或宽度和定制的百叶窗显示条宽度计算百叶窗显示的条数量 ;

根据窗口或图像的高度或宽度定制百叶窗显示条数量计算百窗显示的条宽度.

(2).水平百叶窗效果: 原理同上,只是绘制像素点开始的坐标不同.

效果图:

     

实现代码:
垂直百叶窗

private void button1_Click(object sender, EventArgs e)
{
//垂直百叶窗显示图像
try
{
MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
int dw = MyBitmap.Width / ;
int dh = MyBitmap.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint = new Point[];
for (int x = ; x < ; x++)
{
MyPoint[x].Y = ;
MyPoint[x].X = x * dw;
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = ; i < dw; i++)
{
for (int j = ; j < ; j++)
{
for (int k = ; k < dh; k++)
{
bitmap.SetPixel(MyPoint[j].X + i, MyPoint[j].Y + k,
MyBitmap.GetPixel(MyPoint[j].X + i, MyPoint[j].Y + k));
}
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
} }

水平百叶窗

private void button3_Click(object sender, EventArgs e)
{
//水平百叶窗显示图像
try
{
MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
int dh = MyBitmap.Height / ;
int dw = MyBitmap.Width;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint = new Point[];
for (int y = ; y < ; y++)
{
MyPoint[y].X = ;
MyPoint[y].Y = y * dh;
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = ; i < dh; i++)
{
for (int j = ; j < ; j++)
{
for (int k = ; k < dw; k++)
{
bitmap.SetPixel(MyPoint[j].X + k, MyPoint[j].Y + i, MyBitmap.GetPixel(MyPoint[j].X + k, MyPoint[j].Y + i));
}
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

九.马赛克效果

原理: 确定图像的随机位置点和确定马赛克块的大小,然后马赛克块图像覆盖随机点即可.

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以马赛克效果显示图像
try
{
int dw = MyBitmap.Width / ;
int dh = MyBitmap.Height / ;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint = new Point[];
for (int x = ; x < ; x++)
for (int y = ; y < ; y++)
{
MyPoint[x * + y].X = x * dw;
MyPoint[x * + y].Y = y * dh;
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = ; i < ; i++)
{
System.Random MyRandom = new Random();
int iPos = MyRandom.Next();
for (int m = ; m < dw; m++)
for (int n = ; n < dh; n++)
{
bitmap.SetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n, MyBitmap.GetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n));
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
}
for (int i = ; i < ; i++)
for (int m = ; m < dw; m++)
for (int n = ; n < dh; n++)
{
bitmap.SetPixel(MyPoint[i].X + m, MyPoint[i].Y + n, MyBitmap.GetPixel(MyPoint[i].X + m, MyPoint[i].Y + n));
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

十. 油画效果

原理: 对图像中某一范围内的像素引入随机值.

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以油画效果显示图像
Graphics g = this.panel1.CreateGraphics();
//Bitmap bitmap = this.MyBitmap;
//取得图片尺寸
int width = MyBitmap.Width;
int height = MyBitmap.Height;
RectangleF rect = new RectangleF(, , width, height);
Bitmap img = MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
//产生随机数序列
Random rnd = new Random();
//取不同的值决定油画效果的不同程度
int iModel = ;
int i = width - iModel;
while (i > )
{
int j = height - iModel;
while (j > )
{
int iPos = rnd.Next() % iModel;
//将该点的RGB值设置成附近iModel点之内的任一点
Color color = img.GetPixel(i + iPos, j + iPos);
img.SetPixel(i, j, color);
j = j - ;
}
i = i - ;
}
//重新绘制图像
g.Clear(Color.White);
g.DrawImage(img, new Rectangle(, , width, height));
}

十一: 扭曲效果

原理: 将图像缩放为一个非矩形的平等四边形即可

效果图:

实现代码:

        private void button1_Click(object sender, EventArgs e)
{
//以扭曲效果显示图像
if (h == panel1.Height/)
{
w = ;
h = ;
}
Size offset =new Size (w++,h++);//设置偏移量
Graphics g = panel1.CreateGraphics();
Rectangle rect = this.panel1.ClientRectangle;
Point[] points = new Point[];
points[] = new Point(rect.Left+offset.Width ,rect.Top +offset .Height);
points[] = new Point(rect.Right, rect.Top + offset.Height);
points[] = new Point(rect.Left, rect.Bottom - offset.Height);
g.Clear(Color.White);
g.DrawImage(MyBitmap, points);
}

十二.积木效果

原理: 对图像中的各个像素点着重(即加大分像素的颜色值)着色.

效果图:

实现代码:

private void button1_Click(object sender, EventArgs e)
{
//以积木效果显示图像
try
{
Graphics myGraphics = this.panel1.CreateGraphics ();
//Bitmap myBitmap = new Bitmap(this.BackgroundImage);
int myWidth, myHeight, i, j, iAvg, iPixel;
Color myColor, myNewColor;
RectangleF myRect;
myWidth = MyBitmap.Width;
myHeight = MyBitmap.Height;
myRect = new RectangleF(, , myWidth, myHeight);
Bitmap bitmap = MyBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);
i = ;
while (i < myWidth - )
{
j = ;
while (j < myHeight - )
{
myColor = bitmap.GetPixel(i, j);
iAvg = (myColor.R + myColor.G + myColor.B) / ;
iPixel = ;
if (iAvg >= )
iPixel = ;
else
iPixel = ;
myNewColor = Color.FromArgb(, iPixel, iPixel, iPixel);
bitmap.SetPixel(i, j, myNewColor);
j = j + ;
}
i = i + ;
}
myGraphics.Clear(Color.WhiteSmoke);
myGraphics.DrawImage(bitmap, new Rectangle(, , myWidth, myHeight));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}

说明.这些大多为静态图. 后面会有图像的动态显示. 如分块合成图像, 四周扩散显示图像, 上下对接显示图像等.

这些也许能说明一下 PPT或者手机中的图片效果处理程序是如果做出来的.原理应该是相通的.

制作图像一般常用的类有: Bitmap; Graphics; Rectangle;Color; 用到的方法是 Graphics类的DrawImage;

此方法共有30个版本, 我习惯用 DrawImage("图像", "图框") 版本.

因为这个版本的思想是最简单的----把一张**地图像装在一个**地框里! (**代表某种效果的图像和某种效果的框)

如. g.DrawImage(new Bitmap("myPicture"), new Rectangle(0, 0, myWidth, myHeight));

呵呵, 到此

希望对大家有所帮助.

出处:http://www.cnblogs.com/ziyiFly/archive/2008/09/23/1296815.html

C#完成超酷的图像效果 (附demo)的更多相关文章

  1. 【超酷超实用】CSS3可滑动跳转的分页插件制作教程

    原文:[超酷超实用]CSS3可滑动跳转的分页插件制作教程 今天我要向大家分享一款很特别的CSS3分页插件,这款分页插件不仅可以点击分页按钮来实现分页,而且可以滑动滑杆来实现任意页面的跳转,看看都非常酷 ...

  2. jquery+html5制作超酷的圆盘时钟表

    自己封装的一个用HTML5+jQuery写的时钟表 代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...

  3. android实现超酷的腾讯视频首页和垂直水平网格瀑布流一揽子效果

    代码地址如下:http://www.demodashi.com/demo/13381.html 先来一波demo截图 实现ListView.GridView.瀑布流 1.导入RecyclerView的 ...

  4. HTML页面中5种超酷的伪类选择器:hover效果

    想在自己的网站中应用超酷的hover效果吗?也许你可以从如下的这些实例中获得一些灵感,如果你喜欢这些效果,也可以直接拷贝代码并应用到你的站点. 给平淡的站点带来活力 hover效果能给网页增加一些动态 ...

  5. 基于socket的客户端和服务端聊天简单使用 附Demo

    功能使用 服务端 分离一个不停接受客户端请求的线程 接受不客户端请求的线程中,再分离就收消息的线程 几大对象分别是 IPEndPoint IP终结点 服务端Socket,绑定终结点Bind,启动监听L ...

  6. jQuery超酷下拉插件6种效果演示

    原始的下拉框很丑啦, 给大家一款jQuery超酷下拉插件6种效果 效果预览 下载地址 实例代码 <div class="container"> <section ...

  7. 超酷HTML5 Canvas图表应用Chart.js自定义提示折线图

    超酷HTML5 Canvas图表应用Chart.js自定义提示折线图 效果预览 实例代码 <div class="htmleaf-container"> <div ...

  8. 10个你必须掌握的超酷VI命令技巧

    摘要:大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. ...

  9. 使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果

    360度的全景图片效果常常可以用到给客户做产品展示,今天这里我们推荐一个非常不错的来自Robert Pataki的360全景幻灯实现教程,这里教程中将使用javascript来打造一个超酷的全景幻灯实 ...

随机推荐

  1. for-in遍历json数据

    1.for遍历json数据 ','fun':'前端开发'} for(var attr in json){ alert(json[attr]) //遍历json属性的数据 alert(json['nam ...

  2. 自定义View(二)--继承自ViewGroup

    自定义View包括很多种,上一次随笔中的那一种是完全继承自View,这次写的这个小Demo是继承自ViewGroup的,主要是将自定义View继承自ViewGroup的这个流程来梳理一下,这次的Dem ...

  3. struts2中错误处理

    定义一个 package,然后其他package都继承 这个package struts-global 就 有了这个错误处理功能了 然后再自己写个类 struts.xml <constant n ...

  4. mysql安装与配置

    想在个人电脑上安装mysql学习用.在此做下记录 步骤一: MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大 ...

  5. cocos2dx 2.x 版本+Windows+ADT Bundle 配置

    昨天解决了cocos2dx 3.x版本+Windows+ADT Bundle的配置,今天来解决cocos2dx 2.x版本的配置. 整体来说,2.x的配置相对麻烦一点,不过一旦解决了,就一路畅通无阻了 ...

  6. Drupal 7.23:函数drupal_alter()注释

    /** * Passes alterable variables to specific hook_TYPE_alter() implementations. * * This dispatch fu ...

  7. 小数点输出精度控制问题 .xml

    pre{ line-height:1; color:#9f1d66; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#5d57ff; ...

  8. 写在阿里去IOE一周年

    [文/ 任英杰] 去年5月17日,阿里巴巴支付宝最后一台IBM小型机在下线,标志着阿里完成去IOE.随后一场去IOE运动不断发酵,甚至传闻IBM中国去年损失了20%的合同额. 去了IOE,奔向何方?阿 ...

  9. mysql根据时间查询前一天数据

    MySql数据库如何根据时间查询前一天的数据?本文整理了几个解决方法,有需要的朋友参考下.   本节内容:用MySql怎么根据时间查询前一天的数据. 例1: 代码示例: select * from t ...

  10. 【原创】MapReduce编程系列之二元排序

    普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...