public async Task <IActionResult> Captcha() { string captchaCode = ImageFactory.CreateCode(LoginVM.CAPTCHA_CODE_LENGTH); await HttpContext.Session.LoadAsync(); HttpContext.Session.SetString(CAPTCHA, captchaCode); using var stream = ImageFactory.BuildImage(captchaCode, 50, 100, 20, 3); return(Ok("data:image/jpeg; base64, " + Convert.ToBase64String(stream.ToArray()))); }
static void Main(string[] args) { using (FileStream fs = File.OpenWrite("d:/1.jpg")) using (Stream picStream = ImageFactory.BuildImage("AB123", 50, 100, 20, 10, ImageFormatType.Jpeg)) { picStream.CopyTo(fs); } Console.ReadKey(); }
public IActionResult GetValidCode() { string res = ""; string code = ValidCodeHelper.CreateVerifyCode(out res); TempData[ConstList.LOGINSESSION] = res; Stream stream = ImageFactory.BuildImage(code, 40, 120, 12, 2, ImageFormatType.Jpeg); return(File(stream, "image/jpeg")); }
public IActionResult CreateCaptCha() { Random rd = new Random(); string code = rd.Next(1000, 9999).ToString(); TempData["CaptCha"] = code; Stream picstream = ImageFactory.BuildImage(code, 50, 100, 20, 10); return(File(picstream, "image/png")); }
static void Main(string[] args) { string code = ImageFactory.CreateCode(5); using (FileStream fs = File.OpenWrite("d:/1.jpg")) using (Stream picStream = ImageFactory.BuildImage(code, 50, 100, 20, 10)) { picStream.CopyTo(fs); } //Console.ReadKey(); }
public IActionResult CreateCaptCha() { Random rand = new Random(); string code = rand.Next(1000, 9999).ToString(); TempData["CaptCha"] = code; //.net core 2.0的System.Drawing只有几个简单的类,不全面 //还要 https://www.nuget.org/packages/CoreCompat.System.Drawing.v2 Stream picStream = ImageFactory.BuildImage(code, 50, 100, 20, 10); return(File(picStream, "image/png")); }
public static async Task SendCaptchaAsync(SocketUser u) { string captchaCode = ImageFactory.CreateCode(6); await Task.Yield(); Task save = verificationDatabase.Captcha.SetCaptchaAsync(captchaCode, u); MemoryStream imageStream = ImageFactory.BuildImage(captchaCode, 60, 160, 24, 14); imageStream.Position = 0; Image image = Image.FromStream(imageStream); image.Save($"{u.Id}.png", ImageFormat.Png); await Task.WhenAll ( save, u.SendFileAsync($"{u.Id}.png", $"Please type `\\verify` followed by a space and this captcha code to continue{((u as SocketGuildUser) != null ? $" to {(u as SocketGuildUser)!.Guild.Name}" : "")}.\n") ); image.Dispose(); File.Delete($"{u.Id}.png"); List <Task> commands = new(); foreach (SocketGuild g in u.MutualGuilds) { if (await verificationDatabase.Roles.GetVerificationRoleAsync(g) == null) { continue; } SocketGuildUser user = g.GetUser(u.Id); commands.Add(SendToCaptchaLog.SendToCaptchaLogAsync(SendToCaptchaLog.CaptchaType.Requested, user, captchaCode)); } await Task.WhenAll(commands); }