/// <summary> /// 添加 /// </summary> /// <param name="data"></param> /// <param name="expire"></param> public void Add(SlideVerificationInfo data, TimeSpan expire) { HashEntry[] entries = new HashEntry[] { new HashEntry("BackgroudImage", data.BackgroundImg), new HashEntry("SlideImage", data.SlideImg), new HashEntry("OffsetX", data.PositionX), new HashEntry("OffsetY", data.PositionY), }; this.store.HashSet($"{SlideVerificationRedisOptions.Default.KeyPrefix}{data.Key}", entries); this.store.KeyExpire($"{SlideVerificationRedisOptions.Default.KeyPrefix}{data.Key}", expire); }
/// <summary> /// 图片由byte[]转换为base64字符串 /// </summary> private SlideVerificationPlainInfo ConvertToBase64PlainInfo(SlideVerificationInfo data) { SlideVerificationPlainInfo result = SlideVerificationPlainInfo.From(data); if (result != null) { result.BackgroundImg = ImageUtil.ImageToBase64(data.BackgroundImg, ImageFormat.Jpeg); result.SlideImg = ImageUtil.ImageToBase64(data.SlideImg, ImageFormat.Png); //PositionX 不能输出到前端 result.PositionX = 0; } ; return(result); }
/// <summary> /// 创建图片滑动数据 /// </summary> public SlideVerificationInfo Create() { SlideVerificationParam param = new SlideVerificationParam() { BackgroundImage = this.backgrouondImageService.GetRandomOne(), SlideImage = this.slideImageService.GetRandomOne(), Edge = SlideVerificationOptions.Default.Edge, MixedCount = SlideVerificationOptions.Default.MixedCount, }; SlideVerificationInfo result = SlideVerificationCreater.Instance.Create(param); this.store.Add(result, SlideVerificationOptions.Default.Expire); return(result); }
/// <summary> /// 获取 /// </summary> /// <param name="key"></param> /// <returns></returns> public SlideVerificationInfo Get(string key) { SlideVerificationInfo result = null; HashEntry[] entries = this.store.HashGetAll($"{SlideVerificationRedisOptions.Default.KeyPrefix}{key}"); if (entries != null && entries.Length > 0) { result = new SlideVerificationInfo(); result.BackgroundImg = entries.First(a => a.Name == "BackgroudImage").Value; result.SlideImg = entries.First(a => a.Name == "SlideImage").Value; result.PositionX = entries.First(a => a.Name == "OffsetX").Value.ToString().ToInt(); result.PositionY = entries.First(a => a.Name == "OffsetY").Value.ToString().ToInt(); } return(result); }
/// <summary> /// 创建图片滑动数据 /// </summary> public SlideVerificationInfo Create(SlideVerificationParam param) { SlideVerificationInfo result = new SlideVerificationInfo(); result.Key = Guid.NewGuid().ToString("N"); using (Bitmap coverImage = ImageUtil.GetImage(param.SlideImage)) using (Bitmap sourceImage = ImageUtil.GetImage(param.BackgroundImage)) { result.BgWidth = sourceImage.Width; result.BgHeight = sourceImage.Height; result.SlideWidth = coverImage.Width; result.SlideHeight = coverImage.Height; result.PositionX = random.Next(coverImage.Width, sourceImage.Width - coverImage.Width - param.Edge); result.PositionY = random.Next(coverImage.Height, sourceImage.Height - coverImage.Height); //滑块图片 result.SlideImg = CaptureImage(sourceImage, coverImage, result.PositionX, result.PositionY); //背景图片 result.BackgroundImg = DrawBackground(sourceImage, coverImage, result.PositionX, result.PositionY, param.MixedCount); } return(result); }
/// <summary> /// 添加 /// </summary> /// <param name="data"></param> /// <param name="expire"></param> public void Add(SlideVerificationInfo data, TimeSpan expire) { this.store.Set(data.Key, data, expire); }