C#图片的特效设置

   

在form1里面设置按钮下可以弹出
图片特效窗口
form1
{
设置button
{
from2 ff=new from2();
ff.ig=picture.image;
ff.show();
}
 
}
from2
{
public Image ig;
//窗体加载
private void t图片特效_Load(object sender, EventArgs e)
{
toolStripComboBox1.Text = "浮雕";
pictureBox1.Image = ig;
pictureBox2.Image = ig;
//comboxlist1里面的数组功能
string a = "浮雕";
string b = "积木";
string c = "底片";
string d = "雾化";
string[] sj = { a, b ,c,d};
for (int i = 0; i < sj.Length; i++)
{
toolStripComboBox1.Items.Add(sj[i]);

}

}
//效果选择listbox
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
 
pictureBox2.Image = ig; //设置pictureBox1里面的图片
string cb = toolStripComboBox1.Text;
switch (cb)
{
#region 浮雕效果
case "浮雕":
Bitmap mybitmap;//申明Bitmap变量
Image img = pictureBox2.Image;//声明img变量
mybitmap = new Bitmap(img);//实例化Bitmap
//使用for语句设置像素点的颜色
for (int i = 0; i < mybitmap.Width - 1; i++)
{
for (int j = 0; j < mybitmap.Height - 1; j++)
{
//获取像素点的颜色
Color COLOR1 = mybitmap.GetPixel(i, j);
Color color2 = mybitmap.GetPixel(i + 1, j + 1);
//获取个像素点的R G B值
int red = Math.Abs(COLOR1.R - color2.R + 128);
int green = Math.Abs(COLOR1.G - color2.G + 128);
int blue = Math.Abs(COLOR1.B - color2.B + 128);
//颜色处理
if (red > 255) red = 255;
if (red < 0) red = 0;
if (green > 255) green = 255;
if (green < 0) green = 0;
if (blue > 255) blue = 255;
if (blue < 0) blue = 0;
mybitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));

}
pictureBox2.Image = mybitmap;
}
break;
#endregion
#region 积木
case "积木":
//实例化Graphics对象

Graphics mygraphsic = this.CreateGraphics();
//实例化bitmap
Bitmap tmap = new Bitmap(pictureBox2.Image);
//声明变量
int myheight, mywidth, m, n, iAvg, ipixel;
Color mycolor, newcolor;
RectangleF myRect;
//设置变量值
mywidth = tmap.Width;
myheight = tmap.Height;
myRect = new RectangleF(0, 0, mywidth, myheight);
//实例化Bitmap对象
Bitmap mybit = tmap.Clone(myRect, PixelFormat.DontCare);
m = 0;
//使用while语句设置像素点的颜色
while (m < mywidth - 1)
{
n = 0;
while (n < myheight - 1)
{
//获取像素点的颜色
mycolor = mybit.GetPixel(m, n);
//重新设置R G B元素值
iAvg = (mycolor.R + mycolor.G + mycolor.B) / 3;
ipixel = 0;
if (iAvg >= 128)
//如果大于等于128则ipixel=255;
ipixel = 255;
else
ipixel = 0;
newcolor = Color.FromArgb(255, ipixel, ipixel, ipixel);
//重新设置各像素点的颜色
mybit.SetPixel(m, n, newcolor);
n = n + 1;
}
m = m + 1;
}
mygraphsic.Clear(Color.WhiteSmoke);
//重绘对象
mygraphsic.DrawImage(mybit, new Rectangle(0, 0, mywidth, myheight));
pictureBox2.Image = mybit;
break;
#endregion
#region 底片
case "底片":
 
int hg = pictureBox2.Image.Height;//获取图片的高
int wd = pictureBox2.Image.Width;//获取图片的宽
Bitmap wbitmap = new Bitmap(wd, hg);//实例化Bitmap对象
Bitmap bit = (Bitmap)pictureBox2.Image;//实例化Bitmap对象
Color wpixel;//声明一个COLOR变量类型
for (int i = 0; i < wd; i++)//使用双重循设置像素点的颜色
{
for (int j = 0; j < hg; j++)
{
int red, green, blue;//申明三个点的颜色
wpixel = bit.GetPixel(i, j);
red = 255 - wpixel.R;
green = 255 - wpixel.G;
blue = 255 - wpixel.B;
//重绘各个点的像素
wbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));

}
}
//重构图片
pictureBox2.Image = wbitmap;
break;

#endregion
#region 雾化
case "雾化":
int p2wb = pictureBox2.Image.Width;//获取图片的长宽高
int p2ht = pictureBox2.Image.Height;
Bitmap newbit = new Bitmap(p2wb,p2ht);//实例化对象
Bitmap btt = (Bitmap)pictureBox2.Image ;//实例化对象
Color pixel;//声明一个COlor类型对象
for (int i = 0; i < p2wb; i++)
{
for (int j = 0; j < p2ht; j++)
{
Random wmyrandom = new Random();//实例化Randow对象
int wk = wmyrandom.Next(123456);//获取随机数组
int wdx = i + wk % 19;
int wdy = j + wk % 19;
if (wdx >= p2wb)
{
wdx = p2wb - 1;
}
if (wdy >= p2ht)
{
wdy = p2ht - 1;
}
pixel = btt.GetPixel(wdx,wdy);
//重新绘制像素点
newbit.SetPixel(i,j,pixel);
}
}
pictureBox2.Image = newbit;//设置image属性
break;
#endregion
}
}


 

posted on 2012-07-01 09:27  枫叶㈱≌海】  阅读(408)  评论(0编辑  收藏  举报

导航