C#图像处理2

光照效果

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

效果图:


实现代码:

光照效果

       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(0, 0, MyWidth, MyHeight), System.Drawing.Imaging.PixelFormat.DontCare);

int Width 2;

int Height 2;

//MyCenter图片中心点,发亮此值会让强光中心发生偏移

            Point MyCenter new Point(MyWidth 2, MyHeight 2);

//R强光照射面的半径,即光晕

            int Math.Min(MyWidth 2, MyHeight 2);

for (int MyWidth 1; >= 1; i--)

{

for (int MyHeight 1; >= 1; j--)

{

float MyLength (float)Math.Sqrt(Math.Pow((i MyCenter.X), 2) Math.Pow((j MyCenter.Y), 2));

//如果像素位于光晕之内

                    if (MyLength R)

{

Color MyColor MyImage.GetPixel(i, j);

int r, g, b;

//220亮度增加常量,该值越大,光亮度越强

                        float MyPixel 220.0f (1.0f MyLength R);

MyColor.R (int)MyPixel;

Math.Max(0, Math.Min(r, 255));

MyColor.G (int)MyPixel;

Math.Max(0, Math.Min(g, 255));

MyColor.B (int)MyPixel;

Math.Max(0, Math.Min(b, 255));

//将增亮后的像素值回写到位图

                        Color MyNewColor Color.FromArgb(255, r, g, b);

MyImage.SetPixel(i, j, MyNewColor);

}

}

//重新绘制图片

                MyGraphics.DrawImage(MyImage, new Rectangle(0, 0, MyWidth, MyHeight));

}

}

.百叶窗效果

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


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

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

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

效果图:


     

实现代码:

垂直百叶窗

       private void button1_Click(object sender, EventArgs e)

{

//垂直百叶窗显示图像

            try

{

MyBitmap (Bitmap)this.pictureBox1.Image.Clone();

int dw MyBitmap.Width 30;

int dh MyBitmap.Height;

Graphics this.pictureBox1.CreateGraphics();

g.Clear(Color.Gray);

Point[] MyPoint new Point[30];

for (int 0; 30; x++)

{

MyPoint[x].Y 0;

MyPoint[x].X dw;

}

Bitmap bitmap new Bitmap(MyBitmap.Width, MyBitmap.Height);

for (int 0; dw; i++)

{

for (int 0; 30; j++)

{

for (int 0; 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(100);

}

}

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 20;

int dw MyBitmap.Width;

Graphics this.pictureBox1.CreateGraphics();

g.Clear(Color.Gray);

Point[] MyPoint new Point[20];

for (int 0; 20; y++)

{

MyPoint[y].X 0;

MyPoint[y].Y dh;

}

Bitmap bitmap new Bitmap(MyBitmap.Width, MyBitmap.Height);

for (int 0; dh; i++)

{

for (int 0; 20; j++)

{

for (int 0; 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(100);

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "信息提示");

}

.马赛克效果

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

效果图


实现代码:

马赛克效果

        private void button1_Click(object sender, EventArgs e)

{

//以马赛克效果显示图像

            try

{

int dw MyBitmap.Width 50;

int dh MyBitmap.Height 50;

Graphics this.pictureBox1.CreateGraphics();

g.Clear(Color.Gray);

Point[] MyPoint new Point[2500];

for (int 0; 50; x++)

for (int 0; 50; y++)

{

MyPoint[x 50 y].X dw;

MyPoint[x 50 y].Y dh;

}

Bitmap bitmap new Bitmap(MyBitmap.Width, MyBitmap.Height);

for (int 0; 10000; i++)

{

System.Random MyRandom new Random();

int iPos MyRandom.Next(2500);

for (int 0; dw; m++)

for (int 0; 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 0; 2500; i++)

for (int 0; dw; m++)

for (int 0; 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 this.panel1.CreateGraphics();

//Bitmap bitmap this.MyBitmap;

//取得图片尺寸

            int width MyBitmap.Width;

int height MyBitmap.Height;

RectangleF rect new RectangleF(0, 0, width, height);

Bitmap img MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);

//产生随机数序列

            Random rnd new Random();

//取不同的值决定油画效果的不同程度

            int iModel 2;

int width iModel;

while (i 1)

{

int height iModel;

while (j 1)

{

int iPos rnd.Next(100000) iModel;

//将该点的RGB值设置成附近iModel点之内的任一点

                    Color color img.GetPixel(i iPos, iPos);

img.SetPixel(i, j, color);

1;

}

1;

}

//重新绘制图像

            g.Clear(Color.White);

g.DrawImage(img, new Rectangle(0, 0, width, height)); 

十一扭曲效果

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

效果图:

C图片处理(转) - 刘友斌 - 青春无悔

实现代码:

扭曲效果

        private void button1_Click(object sender, EventArgs e)

{

//以扭曲效果显示图像

            if (h == panel1.Height/2)

{

0;

0;

}

Size offset =new Size (w++,h++);//设置偏移量

            Graphics panel1.CreateGraphics();

Rectangle rect this.panel1.ClientRectangle;

Point[] points new Point[3];

points[0] new Point(rect.Left+offset.Width ,rect.Top +offset .Height);

points[1] new Point(rect.Right, rect.Top offset.Height);

points[2] 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(0, 0, myWidth, myHeight);

Bitmap bitmap MyBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);

0;

while (i myWidth 1)

{

0;

while (j myHeight 1)

{

myColor bitmap.GetPixel(i, j);

iAvg (myColor.R myColor.G myColor.B) 3;

iPixel 0;

if (iAvg >= 128)

iPixel 255;

else

iPixel 0;

myNewColor Color.FromArgb(255, iPixel, iPixel, iPixel);

bitmap.SetPixel(i, j, myNewColor);

1;

}

1;

}

myGraphics.Clear(Color.WhiteSmoke);

myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight));

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "信息提示");

}

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

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

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

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

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

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

posted @ 2011-04-28 21:40  linjianlong  阅读(438)  评论(0)    收藏  举报