asp.net(c#)编程实现将彩色图片变灰阶图片的方法示例

时间:2022-05-26 05:08:15

本文实例讲述了asp.net(c#)编程实现将彩色图片变灰阶图片的方法。分享给大家供大家参考,具体如下:

代码如下:

?
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
27
28
29
30
31
32
33
34
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
namespace WebApplication2
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Bitmap b = new Bitmap(Server.MapPath("01.jpg"));
      for (int x = 0; x < b.Width; x++)
      {
        for (int y = 0; y < b.Height; y++)
        {
          Color pixel = b.GetPixel(x, y);
          int val = (pixel.R + pixel.G + pixel.B) / 3;
          b.SetPixel(x, y, Color.FromArgb(val, val, val));
        }
      }
      b.Save(Server.MapPath("1.jpg"));
    }
  }
}

希望本文所述对大家C#程序设计有所帮助。