利用ASP.NET寫一支透由圖片來產生文字排列圖片的程式

這個想法是小弟在讀書時期..教授用來追女朋友的idea....分享給大家呀...
如果是常常在家寫程式的人..沒時間陪女朋友....就做一個送女朋友呀...
在此要感謝提供美美測試照片的朋友..eVonne...

這個想法是小弟在讀書時期..教授用來追女朋友的idea....分享給大家呀...

如果是常常在家寫程式的人..沒時間陪女朋友....就做一個送女朋友呀...

在此要感謝提供美美測試照片的朋友..eVonne...

聽說越遠看越清楚喔...

eVonneImageProcess.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="eVonneImageProcess.aspx.cs" Inherits="eVonneImageProcess" %>

<!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>eVonneImageProcess</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Selected="True" Value="灰階">灰階</asp:ListItem>
            <asp:ListItem Value="彩色">彩色</asp:ListItem>
        </asp:RadioButtonList><asp:TextBox ID="TextBox1" runat="server">eVonne </asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="執行" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="字串不可以為空"></asp:RequiredFieldValidator><br />
        <asp:Image ID="Image1" runat="server" ImageUrl="~/evonne.jpg" /><br />
        <asp:Literal ID="Literal1" runat="server"></asp:Literal></div>
    </form>
</body>
</html>

eVonneImageProcess.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;


public partial class eVonneImageProcess : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        //載入你要處理的圖片
        Bitmap b = new Bitmap(Server.MapPath("evonne.jpg"));

        int cnt = 0;

        sb.Append("<pre>");

        //x=x+2,如果產生的圖片太高可以調整這個,可以+1,+2,+3
        for (int x = 0; x < b.Height; x = x + 2)
        {
            for (int y = 0; y < b.Width; y++)
            {

                Color pixel = b.GetPixel(y, x);

                //顯示的格式可以自行調整
                if (this.RadioButtonList1.SelectedIndex == 0)
                {
                    sb.AppendFormat("<span style='background-color:black;'><font color='{0}'>{1}</font></span>", ColorTranslator.ToHtml(ToGray(pixel)), GetVal(cnt));
                }

                else
                {
                    sb.AppendFormat("<span style='background-color:black;'><font color='{0}'>{1}</font></span>", ColorTranslator.ToHtml(pixel), GetVal(cnt));
                }

                cnt++;
            }

            sb.Append("<br/>");
        }

        sb.Append("</pre>");

        this.Literal1.Text = sb.ToString();
    }



    protected string GetVal(int cnt)
    {
        char[] str = this.TextBox1.Text.ToCharArray();

        return str[cnt % str.Length].ToString();
    }


    protected Color ToGray(Color pixel)
    {
        int val = (pixel.R + pixel.G + pixel.B) / 3;

        return Color.FromArgb(val, val, val);
    }

}

執行結果:

原始程式碼下載:evonne.rar