我正在处理一些图像处理项目。我设法找到了一种如何将原始图像转换为灰度等级的方法。消极的,但仍然无法找到转变成棕褐色的方法。有人可以帮帮我吗?
以下是代码: -
1)GreyScaleConverter.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GreyScaleConverter.aspx.cs"
Inherits="GreyScaleConverter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Grey Scale Converter</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<h1>
Image Converter</h1>
Select Image:<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
<br />
<asp:Button ID="btnGreyScale" runat="server" OnClick="btnGreyScale_Click" Text="Convert To Grey Scale" />
<asp:Button ID="btnSepia" runat="server" OnClick="btnSepia_Click" Text="Convert To Negative" />
<br />
<asp:Image ID="Img" runat="server" Height="400px" Width="500px" />
<br />
</center>
</div>
</form>
2)GreyScaleConverter.aspx.cs
using System;
using System.Drawing;
public partial class GreyScaleConverter : System.Web.UI.Page
{
private Bitmap _current;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["filepath"] != null)
{
Img.ImageUrl = Session["filepath"].ToString();
_current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filePath = Server.MapPath("Images/" + FileUpload1.FileName);
FileUpload1.SaveAs(filePath);
Img.ImageUrl = "Images/" + FileUpload1.FileName;
Session["filepath"] = "Images/" + FileUpload1.FileName;
}
else
{
Response.Write("Please Select An Image!");
}
}
protected void btnGreyScale_Click(object sender, EventArgs e)
{
if (Session["filepath"] != null)
{
Bitmap temp = (Bitmap)_current;
Bitmap bmap = (Bitmap)temp.Clone();
Color col;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
col = bmap.GetPixel(i, j);
byte grey = (byte)(.299 * col.R + .587 * col.G + .114 * col.B);
bmap.SetPixel(i, j, Color.FromArgb(grey, grey, grey));
}
}
_current = (Bitmap)bmap.Clone();
Random rnd = new Random();
int a = rnd.Next();
_current.Save(Server.MapPath("Images/" + a + ".png"));
Img.ImageUrl = "Images/" + a + ".png";
}
}
protected void btnSepia_Click(object sender, EventArgs e)
{
if (Session["filepath"] != null)
{
Bitmap temp = (Bitmap)_current;
Bitmap bmap = (Bitmap)temp.Clone();
Color col;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
col = bmap.GetPixel(i, j);
col = Color.FromArgb(255 - col.R, 255 - col.G, 255 - col.B);
bmap.SetPixel(i, j, col);
}
}
_current = (Bitmap)bmap.Clone();
Random rnd = new Random();
int a = rnd.Next();
_current.Save(Server.MapPath("Images/" + a + ".png"));
Img.ImageUrl = "Images/" + a + ".png";
}
}
}