.Net 利用 Graphics 自動產生 透明背景 及 文字 的PNG圖檔

本篇利用C#內建的繪圖Graphics,將文字轉為圖片!

可以用於網站上的特殊文字,解決對方也需安裝字形才能顯示問題。

首先產生一個空白網頁,加上 img 的 tag,路徑為另外一個aspx,如下圖:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!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">
    <style type="text/css">
    body {
	    background-color: #000;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <img src="ImageString.aspx" alt="" />
    </form>
</body>
</html>

  2012-09-10      ez      .NET
.Net 利用 Microsoft Office Document Imaging (MODI) 製作 光學字元辨識(OCR)

OCR 全名為「Optical Character Recognition」,即是「光學字元辨識」的意思。

本文利用 Microsoft Office Document Imaging (MODI) 製作 光學字元辨識(OCR) 首先必須先安裝Microsoft Office Document Imaging (MODI),請使用Office 2003 或 Office 2007 安裝光碟才有內建此元件,記住Office 2010未包含此元件喔!

如果沒有Office光碟片,也可以下載 SharePoint Designer 2007 裡面也有內建MODI。

SharePoint Designer 2007

下載網址:http://www.microsoft.com/downloads/zh-tw/details.aspx?FamilyID=BAA3AD86-BFC1-4BD4-9812-D9E710D44F42

安裝方式: 放入Office光碟進行安裝,選擇自訂安裝,並且依照底下框選項目進行選擇。


  2012-06-20      ez      .NET
.Net 抓取 IE 網址,自動轉址

使用 Interop.SHDocVw 抓取 IE 目前正在瀏覽的網址,如果網址為yahoo.com即自動轉址!

Interop.SHDocVw下載:Interop.SHDocVw

using System;
using System.IO;

namespace Test
{
    class Program
    {
        static SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
        static void Main(string[] args)
        {
            while (true)
            {
                foreach (SHDocVw.InternetExplorer ie in shellWindows)
                {
                    if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals("iexplore"))
                    {
                        string url = ie.LocationURL.ToString().ToLower();
                        //判斷網址如果為yahoo.com
                        if (url.Contains("yahoo.com"))
                        {
                            object missing = Type.Missing;
                            //自動將網址倒到
                            ie.Navigate("http://www.cscworm.net/", ref missing, ref missing, ref missing, ref missing);
                        }
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

  2012-06-20      ez      .NET
.Net 抓取 IE 目前正在瀏覽的網頁內容

使用 Interop.SHDocVw 抓取 IE 目前正在瀏覽的網頁內容 Interop.SHDocVw下載:

Interop.SHDocVw 需先加入以下參考項目:

  1. Interop.SHDocVw
  2. Microsoft.mshtml
using System;
using System.IO;
using mshtml;

namespace Test
{
    class Program
    {
        static SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
        static void Main(string[] args)
        {
            while (true)
            {
                foreach (SHDocVw.InternetExplorer ie in shellWindows)
                {
                    if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals("iexplore"))
                    {
                        HTMLDocument currentPage = (HTMLDocument)ie.Document;
                        string url = ie.LocationURL.ToString().ToLower(); //網頁網址
                        string body = currentPage.body.outerHTML.ToString(); //網頁內容
                        Console.WriteLine(url + "\r\n" + body);
                    }
                }
                Console.WriteLine();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

  2012-06-20      ez      .NET
Base64 轉換

使用Base64可以做到基礎的加密,可以將文字轉換為一串亂碼,並且也可以將亂碼轉回文字。

但是因為沒有金鎖,所以大家都可以解密,只能用在一般用途。

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //將文字轉換為Base64
            byte[] bytes = Encoding.Default.GetBytes("中文字");
            string Base64 = Convert.ToBase64String(bytes);
            Console.WriteLine(Base64);

            //將base64轉為byte
            bytes = Convert.FromBase64String(Base64);
            string str = Encoding.Default.GetString(bytes);
            Console.WriteLine(str);            

            Console.ReadLine();
        }
    }
}

  2012-06-20      ez      .NET
stream 轉換 string

使用StreamReader將stream讀取出來,然後轉為string。

using System;
using System.IO;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] b = { 0, 1, 2, 3, 4 };
            Stream stream = new MemoryStream(b);
            using (StreamReader reader = new StreamReader(stream))
            {
               string str = reader.ReadToEnd(); //將stream轉為string
            }
        }
    }
}

  2012-06-20      ez      .NET
Dictionary 列舉及轉換

如何將 Dictionary 裡的資料列舉出來,使用 KeyValuePair。

using System;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static Dictionary<string, string> D = new Dictionary<string, string>();

        /// <summary>
        /// 產生測試資料
        /// </summary>
        static void testdata()
        {
            int num = 1000000;
            for (int i = 0; i < num; i ++)
            {
                D.Add((i * 2).ToString(), (i * 2).ToString());
            }
        }

        static void Main(string[] args)
        {
            //產生測試資料
            testdata();
            //列舉資料
            foreach (KeyValuePair<string, string> Items in D)
            {
                Console.WriteLine(String.Format("key: {0} \t value: {1}", Items.Key, Items.Value));
            }
            Console.ReadLine();
        }
    }
}

  2012-06-04      ez      .NET
Dictionary 效能測試

首先測試索引查詢及內容查詢,所需的時間差距。

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static Dictionary<string, string> D = new Dictionary<string, string>();

        /// <summary>
        /// 產生測試資料
        /// </summary>
        static void testdata()
        {
            int num = 1000000, x = 0;
            D.Clear();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < num; i ++)
            {
                x++;
                D.Add((i * 2).ToString(), (i * 2).ToString());
            }
            sw.Stop();
            Console.WriteLine("產生資料" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
        }

        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            //產生測試資料
            testdata();

            //搜尋索引值
            {
                sw.Reset();
                int num = 1000000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    if (D.ContainsKey(i.ToString())) { }
                }
                sw.Stop();
                Console.WriteLine("搜尋索引" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
            }            

            //搜尋內容
            {
                sw.Reset();
                int num = 1000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    if (D.ContainsValue(i.ToString())) { }
                }
                sw.Stop();
                Console.WriteLine("搜尋內容" + num + "筆\t\t時間:" + sw.ElapsedMilliseconds.ToString());
            }

            Console.ReadLine();
        }
    }
}


  2012-06-04      ez      .NET
Dictionary 基本運用

Dictionary 泛型類別提供從一組索引鍵至一組值的對應。

加入字典中的每一個項目都是由值及其關聯索引鍵所組成。

使用其索引鍵擷取值的速度非常快 (接近 O(1)),這是因為 Dictionary 類別是實作為雜湊資料表。

擷取速度取決於為 TKey 指定之型別的雜湊演算法品質。

只要物件用做 Dictionary 中的索引鍵,就無法以影響其雜湊值 (Hash Value) 的任何方式對其進行變更。

Dictionary 中的每個索引鍵都必須是唯一的 (根據字典的相等比較子 (Comparer))。

索引鍵不能是 Null 參照 (即 Visual Basic 中的 Nothing),但是,如果值型別 TValue 是參考型別時,值就可以是 Null。

using System;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //產生Dictionary
            Dictionary<string, string> D0 = new Dictionary<string, string>();

            //產生Dictionary並帶入值
            Dictionary<string, string> D = new Dictionary<string, string>()
            {
                { "a" , "1" } ,
                { "b" , "2" } ,
                { "c" , "3" }
            };

            //加入值
            D.Add("d", "4");

            //取得值
            string value = D["a"];

            //修改值
            D["a"] = "1";

            //刪除值
            D.Remove("d");

            //判斷索引是否存在
            if (D.ContainsKey("a"))
                Console.WriteLine("Value = " + D["a"]);
            else
                Console.WriteLine("Key is not found.");

            Console.ReadLine();
        }
    }
}

  2011-12-01      ez      .NET
Dictionary 不分大小寫

Dictionary 可以不經過大小寫轉換就可以查詢摟!

只需要加上StringComparer.OrdinalIgnoreCase

using System;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> d = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "AbCdEfG" , "123456" }
            };
            Console.WriteLine(d["abcdefg"]);
        }
    }
}

  2011-11-30      ez      .NET