/// <summary> /// 執行程式主體 /// </summary> /// <param name="text"></param> private static void DoMaskProccess(string text) { MaskModel result = new MaskModel(); MaskClass tool = new MaskClass(text); result = tool.SwMaskLeftToRight(4); Console.WriteLine("結尾遮罩(LeftToRight): 字串由左至右, 指定第n個字開始遮罩, "); Console.WriteLine("如 SwMaskLeftToRight (4) = " + ShowResult(result)); result = tool.SwMaskRightToLeft(4); Console.WriteLine("起始遮罩(RightToLeft): 字串由右至左, 指定第n個字往前開始遮罩, "); Console.WriteLine("如 SwMaskRightToLeft (4) = " + ShowResult(result)); result = tool.SwMaskRange(3,7); Console.WriteLine("範圍遮罩(Range): 字串指定一個範圍區間(n~m)內遮罩, "); Console.WriteLine("如 SwMaskRange (3,7) = " + ShowResult(result)); result = tool.SwMaskRandom(3); Console.WriteLine("亂數遮罩(Random):字串指定需要保留幾個值, 其餘字元遮罩, "); Console.WriteLine("如 SwMaskRandom (3) = " + ShowResult(result)); result = tool.SwMaskMiddle(1,3); Console.WriteLine("中間遮罩(Middle): 字串指定前後各保留幾碼顯示, 其餘字元遮罩,"); Console.WriteLine("如 SwMaskMiddle (1,3) = " + ShowResult(result)); }
/// <summary> /// 顯示呼叫方法結果字串 /// </summary> /// <param name="result"></param> /// <returns></returns> private static string ShowResult(MaskModel result) { if (!result.isSuccess) { return result.message; } return result.maskResult; }
private byte[] CreateNewMask(string filePath) { var imageModel = _unit.ImageRepository.Find(i => i.Path == filePath).FirstOrDefault(); if (imageModel == null) { imageModel = new ImageModel() { Path = filePath }; _unit.ImageRepository.Add(imageModel); _unit.Commit(); } using (Bitmap image = (Bitmap)Image.FromFile(filePath)) { byte[] mask = new byte[image.Width * image.Height]; int padding = _maskSize / 2; for (int i = 0; i < mask.Length; i++) { mask[i] = 0; } for (int i = 0; i < padding; i++) { for (int j = 0; j < image.Width; j++) { mask[i * image.Width + j] = 1; mask[(image.Height - 1 - i) * image.Width + j] = 1; } } for (int i = padding; i < image.Height - padding; i++) { for (int j = 0; j < padding; j++) { mask[i * image.Width + j] = 1; mask[i * image.Width + (image.Width - j - 1)] = 1; } } MaskModel maskModel = new MaskModel() { ImageId = imageModel.Id, MaskArray = mask, MaskHeight = image.Height, MaskWidth = image.Width }; _unit.MaskRepository.Add(maskModel); _unit.Commit(); return(mask); } }