以下有兩種方法複製 BitmapData,一個是使用 unsafe 方法,一個一個 byte 複製,另外一個是複製記憶體區塊,較為快速。
目前測試為,第二種方法比第一種方法快四倍。
using (Bitmap bmp = new Bitmap("檔案路徑"))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
Stopwatch sw = new Stopwatch();
sw.Start();
for (int xx = 0; xx < 1000; xx++)
{
//一個一個byte複製
int dataIndex = 0, height = bmpData.Height, width = bmpData.Width;
byte[] data = new byte[width * height * 3];
unsafe
{
byte* p = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
data[dataIndex++] = p[0];
data[dataIndex++] = p[1];
data[dataIndex++] = p[2];
p += 3;
}
}
}
}
sw.Stop();
Console.WriteLine("Time1:" + sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
for (int xx = 0; xx < 1000; xx++)
{
byte[] data = new byte[bmpData.Width * bmpData.Height * 3];
Marshal.Copy(bmpData.Scan0, data, 0, data.Length); //複製記憶體區塊
}
sw.Stop();
Console.WriteLine("Time2:" + sw.ElapsedMilliseconds.ToString());
bmp.UnlockBits(bmpData);
}Task 與 Thread 的基本使用方法。
執行結果:
之前有一篇 簡體 繁體 轉換,現在多了大小寫轉換,方式差不多,依樣是呼叫 Microsoft.VisualBasic 即可達成。
程式碼如下:
using System;
using System.Text;
using Microsoft.VisualBasic;
namespace StrConv
{
public partial class Default : System.Web.UI.Page
{
public StringBuilder SB = new StringBuilder();
protected string TestStr = "測試文字abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_+~`-=[]\\{}:\";'<>?,./|";
protected void Page_Load(object sender, EventArgs e)
{
ConvertStrConv();
}
private void ConvertStrConv()
{
//不執行轉換
DisplayList(VbStrConv.None.ToString(), Strings.StrConv(TestStr, VbStrConv.None));
//將字串轉換為大寫字元。
DisplayList(VbStrConv.Uppercase.ToString(), Strings.StrConv(TestStr, VbStrConv.Uppercase));
//將字串轉換為小寫字元。
DisplayList(VbStrConv.Lowercase.ToString(), Strings.StrConv(TestStr, VbStrConv.Lowercase));
//將字串中每個單字的第一個字母轉換為大寫。
DisplayList(VbStrConv.ProperCase.ToString(), Strings.StrConv(TestStr, VbStrConv.ProperCase));
//將字串中的半形字元轉換成全形字元。
DisplayList(VbStrConv.Wide.ToString(), Strings.StrConv(TestStr, VbStrConv.Wide).Replace("\\", "\"));
//將字串中的全形字元轉換成半形字元。
DisplayList(VbStrConv.Narrow.ToString(), Strings.StrConv(TestStr, VbStrConv.Narrow));
//將繁體中文字元轉換成簡體中文。
DisplayList(VbStrConv.SimplifiedChinese.ToString(), Strings.StrConv(TestStr, VbStrConv.SimplifiedChinese, 2052));
//將簡體中文字元轉換成繁體中文。
DisplayList(VbStrConv.TraditionalChinese.ToString(), Strings.StrConv(TestStr, VbStrConv.TraditionalChinese, 1028));
}
private void DisplayList(string strVbStrConvName, string strInValue)
{
SB.AppendLine(String.Format("VbStrConv:{0} 轉換後 {1} <BR/><BR/>", strVbStrConvName, strInValue));
}
}
}為了清楚的了解NO-SQL和SQL的效能差異,所以進行了寫入及讀取的效能測試,測試用的電腦則為我的筆電,規格如下:
CPU:Intel Core i7-2630QM
RAM:8GB
OS:Windows 7 Home Premium
HD:Memoright SSD 240G
測試結果可以發現差異非常大喔!MongoDB比MS-SQL速度快上10倍,不過還是需要看個人需求選擇適當的資料庫,以下提供參考。
MongoDB 測試結果:
前一篇已經寫過,使用Path.InvalidPathChars即可達到效果,此篇加強了一些功能,程式如下:
static void Main(string[] args)
{
string FileName = "a:\\abc//~!@#$%^&*()_+}{\":?><|.txt...";
Console.WriteLine(MakeFilenameValid(FileName));
string FolderName = @"c:\aaa\bbb\ccc\aaa:abc//~!@#$%^&*()_+}{:?><|....";
Console.WriteLine(MakeFoldernameValid(FolderName));
Console.Read();
}
static string MakeFilenameValid(string FN)
{
if (FN == null) throw new ArgumentNullException();
if (FN.EndsWith(".")) FN = Regex.Replace(FN, @"\.+$", "");
if (FN.Length == 0) throw new ArgumentException();
if (FN.Length > 245) throw new PathTooLongException();
foreach (char c in System.IO.Path.GetInvalidFileNameChars()) FN = FN.Replace(c, '_');
return FN;
}
static string MakeFoldernameValid(string FN)
{
if (String.IsNullOrEmpty(FN)) throw new ArgumentNullException();
if (FN.EndsWith(".")) FN = Regex.Replace(FN, @"\.+$", "");
if (FN.Length == 0) throw new ArgumentException();
if (FN.Length > 245) throw new PathTooLongException();
foreach (char c in System.IO.Path.GetInvalidPathChars()) FN = FN.Replace(c, '_');
return FN.Replace("/", @"\");
}需先下載 DotNetZipLib
官方網站:http://dotnetzip.codeplex.com/
//壓縮檔案
using (ZipFile zip = new ZipFile("壓縮檔案路徑 c:\a.zip"))
{
//壓縮進度顯示
zip.SaveProgress += new EventHandler<SaveProgressEventArgs>((s, e) =>
{
double Total = e.TotalBytesToTransfer, Now = e.BytesTransferred;
Console.WriteLine("壓縮進度... ( " + String.Format("{0:0.00}", Math.Round(Now / Total * 100, 2)) + "% )");
});
zip.Comment = "壓縮檔註解";
zip.Password = "壓縮檔密碼";
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level9; //壓縮等級
zip.MaxOutputSegmentSize = SegmentSize; //壓縮檔分割大小 最多只能分成100個
zip.AddFile("需壓縮的檔案路徑 c:\aaa.txt", String.Empty);
zip.Save();
}
//列出壓縮檔
using (ZipFile zip = new ZipFile("壓縮檔案路徑 c:\a.zip"))
{
foreach (ZipEntry detail in zip)
{
Console.WriteLine(detail.FileName);
}
}
當使用Cache常會發現,不知道何時要清除快取的窘境,太快清除就無法達到Cache效果,太慢又無法即時更新資訊! 本篇利用CacheDependency偵測檔案變更時,自動清除Cache,達到隨時都是最新資訊。
以下方式即可偵測檔案XXX.dat,如果檔案變更了,就會自動清除Cache:
CacheDependency CD = new CacheDependency(Server.MapPath("~/XXX.dat"));
HttpRuntime.Cache.Add("cache key", "cache body", CD, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);如果要釋放快取時呼叫指定Function,並且超過20秒未使用也會釋放,如下:
HttpRuntime.Cache.Add("cache key", "cache body", CD, DateTime.Now.AddSeconds(20), TimeSpan.Zero, CacheItemPriority.High, RemovedCallback);
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
{
var key = k;
var value = v;
}抓取 try cache Exception 錯誤編號 方法如下:
try
{
}
catch (Exception ex)
{
int ErrorCode = 0;
Win32Exception W32EX = ex as Win32Exception;
if (W32EX == null) W32EX = ex.InnerException as Win32Exception;
if (W32EX != null) ErrorCode = W32EX.ErrorCode;
Console.WriteLine(ErrorCode.ToString());
}未壓縮:
已壓縮:
程式明明好好的,為什麼切換到IE10就會出現錯誤呢?
原因為 .Net 無法識別 IE 10 的瀏覽器 Header,所以會判斷無法使用指令,此時只要讓 .Net 認得即可。
首先將 .Net Framework 都更新到最新狀態。
利用 cmd 將 browsers 重置,進入 .Net Framework 2.0 或 4.0 目錄。
