当前位置: 首页 > 图文教程 > .Net技术 > ASP.NET > ASP.NET:ASP.NET中把彩色图片变成黑白图片
最大值法:使每个像素点的R、G、B值等于原像素点的RGB值中的最大的一个。
平均值法:使每个像素点的R、G、B值等于原像素点的RGB值的平均值。
加权平均值法:根据需要指定每个像素点R、G、B值的权数,并取其加权平均值。
为了验证自己的想法正确与否,用ASP.NET写了如下代码,运行结果显示成功。通过程序可以把彩色图像编程黑白效果。
Private void button1_Click(object sender,EventArgs e)
{
//以黑白效果显示图像
Try{
Int Height = this,pictureBox1.Image.Heigh;
Int Width = this.pictureBox1.Image,Width;
Bitmap newBitmap =new Bitmap(Width,Height);
Bitmap oldBitmap= (Bitmap)this.pictureBox1.Image;
Color pixe1;
For(int x=0; x<Width; x++)
For(int y=0;y<Height;y++)
{
Pixe1=oldBitmap.GetPixe1(x,y);
Int r,g,b,result =0;
R=pixel.R;
G=pixel.G;
B=pixel.B;
//实例程序以加权平均值法产生黑白图像
Int iType=2;
Switch(iType)
{
Case 0: //平均值法
Result=(r+g+b)/3;
Break;
Case 1: //最大值法
Result=r>g?r:g;
Result = Result >b?Result:b;
Break;
Case 2://加权平均值法
Result=((int)(0.7*r)+(int)(0.2*g)+(int)(0.1*b));
Break;
}
newBitmap.SetPixe1(x,y,Color.FromArgb(Result,Result,Result))
}
This,pictureBox1.Image =newBitmap;
}
Catch(Exception ex)
{
MessageBox.Show(ex,Message,”信息提示”);
}
}
评论 (0) All