随笔-10  评论-4  文章-11  trackbacks-0
  2006年5月10日
現在有很多系統都提供讓使用者加入或上傳圖片的功能,
有時候為了限制上傳圖片的尺寸,
避免使用者上傳太大的檔案,造成系統的負擔,
或是像很多照片分享網站(例如:Flickr),也會提供不同的圖片尺寸功能
如果也有類似需求,可以參考以下的範例。

.NET裡的Image這個類別,對於Image檔案的存取提供了很強大的功能,
另外再配合Graphics,就可以很容易的就去做些圖片內容的修改,
有興趣的研究看看。

public void ReSizeImage(string SourceImageFileName, string NewImageFileName)
{
    
double maxHeight = 600D;
    
double maxWidth = 800D;

    Image img1 
= Image.FromFile(SourceImageFileName);

    
double r = System.Math.Min(Convert.ToDouble(maxWidth / img1.Width), (maxHeight / img1.Height));
    
if (r >= 1)
      
throw new Exception("圖片尺寸已經小於限制尺寸");

    Size s 
= new Size();
    s.Height 
= Convert.ToInt32(img1.Height * r);
    s.Width 
= Convert.ToInt32(img1.Width * r);

    Image img2 
= new Bitmap(img1, s);
    img2.Save(NewImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}

參考資料:
Merging Images in .NET - The Code Project

posted @ 2007-09-06 11:58 Jason Cheng 阅读(131) | 评论 (0)编辑
雜湊碼的應用範圍很廣,舉凡密碼的加密,或是檔案傳輸過程的驗證,或是替一組數據(或一個檔案)產生一個短一點的認證碼,.NET Framework已經提供有現成的類別,支援最常用的MD5跟SHA等演算法,下面程式碼簡單示範叫用的程序。

 1 <%@ Page Language="C#" %>
 2 <%@ Import Namespace="System.Security.Cryptography" %>
 3 <%@ Import Namespace="System.Configuration.Provider" %>
 4 
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 6 
 7 <script runat="server">
 8     
 9     
10     protected string ComputeHash(byte[] data, string hashAlgorithmType)
11     {
12         byte[] result;
13         HashAlgorithm s = HashAlgorithm.Create(hashAlgorithmType);
14         if (s == null)
15             throw new ProviderException("Could not create a hash algorithm");
16         result = s.ComputeHash(data);
17         return Convert.ToBase64String(result);
18     }
19 
20     protected void Button1_Click(object sender, EventArgs e)
21     {
22         if (FileUpload1.HasFile)
23         {
24             StringBuilder sb = new StringBuilder();
25             sb.AppendFormat("FileName = {0}<br />", FileUpload1.FileName);
26             sb.AppendFormat("FileSize = {0:#,##0} Bytes<br />", FileUpload1.FileBytes.Length);
27 
28             string[] hashAlgorithms = new string[] { "MD5""SHA1""SHA256""SHA512" };
29             for (int i = 0; i < hashAlgorithms.Length; i++)
30             {
31                 long startTick = DateTime.Now.Ticks;
32                 string smd5 = ComputeHash(FileUpload1.FileBytes, hashAlgorithms[i]);
33                 long endTick = DateTime.Now.Ticks;
34                 double times = (endTick - startTick) / 10000D;
35                 sb.AppendFormat("{0} = {1} ({2}bytes, {3:f4}ms)<br />",hashAlgorithms[i], smd5, smd5.Length, times);
36             }
37             sb.Append("<hr/>");
38 
39             laMessage.Text += sb.ToString();
40         }
41     }
42     protected void Button2_Click(object sender, EventArgs e)
43     {
44         laMessage.Text = "";
45     }
46     
47 </script>
48 
49 <html xmlns="http://www.w3.org/1999/xhtml">
50 <head runat="server">
51     <title>Compute Hash Values</title>
52 </head>
53 <body>
54     <form id="form1" runat="server">
55         <div>
56             <asp:Label ID="Label1" runat="server" Text="Select a file:"></asp:Label>
57             <asp:FileUpload ID="FileUpload1" runat="server" Width="500px" />
58             <br />
59             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Compute" />
60             <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clean" /><br />
61             <br />
62             <asp:Literal ID="laMessage" runat="server"></asp:Literal>
63         </div>
64     </form>
65 </body>
66 </html>
67 


posted @ 2006-11-01 12:06 Jason Cheng 阅读(187) | 评论 (0)编辑

在.NET裡要如何調用外部程式呢??


在.net裡,提供了Process類,提供我們強大的調用外部工具功能,並透過重新導向輸入與輸出,可以取得執行結果,下面就用一個例子來示範在一個WinForm裡輸入一個Dos命令,然後呼叫CMD.EXE來執行,並取回執行的結果。

[程式畫面]


[程式碼]
 1         private string RunCmd(string command)
 2         {
 3             //實例一個Process類,啟動一個獨立進程
 4             Process p = new Process();
 5 
 6             //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:
 7 
 8             p.StartInfo.FileName = "cmd.exe";           //設定程序名
 9             p.StartInfo.Arguments = "/c " + command;    //設定程式執行參數
10             p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
11             p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
12             p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
13             p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
14             p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
15 
16             p.Start();   //啟動
17             
18             //p.StandardInput.WriteLine(command);       //也可以用這種方式輸入要執行的命令
19             //p.StandardInput.WriteLine("exit");        //不過要記得加上Exit要不然下一行程式執行的時候會當機
20             
21             return p.StandardOutput.ReadToEnd();        //從輸出流取得命令執行結果
22 
23         }

[範例程式碼下載]

posted @ 2006-05-10 16:40 Jason Cheng 阅读(1103) | 评论 (2)编辑