private async Task GetCode(HttpContext context) { Random rd = new Random(); _PositionX = rd.Next(_MinRangeX, _MaxRangeX); _PositionY = rd.Next(_MinRangeY, _MaxRangeY); context.Session.SetInt32("code", _PositionX); context.Session.SetInt32("code_errornum", 0); int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; int[] array = a.OrderBy(x => Guid.NewGuid()).ToArray(); Bitmap bmp; var capchaPic = Path.Combine(path, (new Random()).Next(0, _ImgNum - 1) + ".jpg"); var filename = Path.Combine(environment.ContentRootPath, capchaPic); if (!File.Exists(filename)) { var embeddedFileProvider = new Microsoft.Extensions.FileProviders.EmbeddedFileProvider(this.GetType().Assembly, this.GetType().Assembly.GetName().Name); var allResources = embeddedFileProvider.GetDirectoryContents(string.Empty); var embedFile = embeddedFileProvider.GetFileInfo(capchaPic.Replace(Path.DirectorySeparatorChar, '.')); if (!embedFile.Exists) { throw new ArgumentNullException("can not found captcha picture"); } bmp = new Bitmap(embedFile.CreateReadStream()); } else { bmp = new Bitmap(filename); } string ls_small = "data:image/jpg;base64," + ImgToBase64String(cutImage(bmp, _shearSize, _shearSize, _PositionX, _PositionY)); Bitmap lb_normal = GetNewBitMap(bmp, _shearSize, _shearSize, _PositionX, _PositionY); string ls_confusion = "data:image/jpg;base64," + ImgToBase64String(ConfusionImage(array, lb_normal)); var captchaInfo = new CaptchaInfo { Errcode = 0, Y = _PositionY, Array = string.Join(",", array), ImgX = _ImgWidth, ImgY = _ImgHeight, Small = ls_small, Normal = ls_confusion }; /* errcode: 状态值 成功为0 * y:裁剪图片y轴位置 * small:小图字符串 * normal:剪切小图后的原图并按无序数组重新排列后的图 * array:无序数组 * imgx:原图宽 * imgy:原图高 */ var viewResult = viewEngine.GetView("~/", "~/Views/Shared/SlideCaptcha.cshtml", true); if (viewResult.Success) { //创建临时的StringWriter实例,用来配置到视图上下文中 using (var output = new StringWriter()) { //视图上下文对于视图渲染来说很重要,视图中的前后台交互都需要它 var viewContext = new ViewContext() { HttpContext = context, Writer = output, RouteData = new Microsoft.AspNetCore.Routing.RouteData() { }, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = captchaInfo }, TempData = new TempDataDictionary(context, tempDataProvider), //ViewData View = viewResult.View, FormContext = new FormContext(), ActionDescriptor = new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor() }; //渲染 await viewResult.View.RenderAsync(viewContext); var html = output.ToString(); context.Response.ContentType = "text/html"; //输出到响应体 await context.Response.WriteAsync(html); } } else { await context.Response.WriteAsync(jsonHelper.Serialize(captchaInfo).ToString()); } }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OaDisplayUx)); this.components = new System.ComponentModel.Container(); this.buttonCaptureScreen = new System.Windows.Forms.Button(); this.screenCapture = new System.Windows.Forms.PictureBox(); this.statusTextBox = new System.Windows.Forms.TextBox(); this.spoolForward = new System.Windows.Forms.Button(); this.spoolBack = new System.Windows.Forms.Button(); this.resumeMirror = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.screenCapture)).BeginInit(); this.SuspendLayout(); // // buttonCaptureScreen // this.buttonCaptureScreen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonCaptureScreen.Location = new System.Drawing.Point(1133, 611); this.buttonCaptureScreen.Name = "buttonCaptureScreen"; this.buttonCaptureScreen.Size = new System.Drawing.Size(50, 47); this.buttonCaptureScreen.TabIndex = 0; this.buttonCaptureScreen.Text = "📌"; this.buttonCaptureScreen.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.buttonCaptureScreen.UseVisualStyleBackColor = true; this.buttonCaptureScreen.Click += CaptureClick_EventHandler; // // screenCapture // this.screenCapture.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.screenCapture.Location = new System.Drawing.Point(12, 12); this.screenCapture.Name = "screenCapture"; this.screenCapture.Size = new System.Drawing.Size(1176, 593); this.screenCapture.TabIndex = 1; this.screenCapture.TabStop = false; this.screenCapture.SizeMode = PictureBoxSizeMode.Zoom; // // statusTextBox // this.statusTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.statusTextBox.Enabled = false; this.statusTextBox.Location = new System.Drawing.Point(12, 611); this.statusTextBox.Name = "statusTextBox"; this.statusTextBox.Size = new System.Drawing.Size(969, 26); this.statusTextBox.TabIndex = 3; // spoolForward // this.spoolForward.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.spoolForward.Location = new System.Drawing.Point(1037, 611); this.spoolForward.Name = "spoolForward"; this.spoolForward.Size = new System.Drawing.Size(37, 47); this.spoolForward.TabIndex = 4; this.spoolForward.Text = "▶️"; this.spoolForward.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.spoolForward.UseVisualStyleBackColor = true; this.spoolForward.Click += SpoolForwardClick_EventHandler; // // spoolBack // this.spoolBack.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.spoolBack.Location = new System.Drawing.Point(994, 611); this.spoolBack.Name = "spoolBack"; this.spoolBack.Size = new System.Drawing.Size(37, 47); this.spoolBack.TabIndex = 5; this.spoolBack.Text = "◀️"; this.spoolBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.spoolBack.UseVisualStyleBackColor = true; this.spoolBack.Click += SpoolBackClick_EventHandler; // // resumeMirror // this.resumeMirror.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.resumeMirror.Location = new System.Drawing.Point(1080, 611); this.resumeMirror.Name = "resumeMirror"; this.resumeMirror.Size = new System.Drawing.Size(37, 47); this.resumeMirror.TabIndex = 6; this.resumeMirror.Text = "▶️▶️"; this.resumeMirror.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.resumeMirror.UseVisualStyleBackColor = true; this.resumeMirror.Click += ResumeMirrorClick_EventHandler; notifyIcon1 = new NotifyIcon(this.components); var embeddedProvider = new Microsoft.Extensions.FileProviders.EmbeddedFileProvider(System.Reflection.Assembly.GetExecutingAssembly()); using (var reader = embeddedProvider.GetFileInfo("resources\\PDF.ico").CreateReadStream()) { notifyIcon1.Icon = new Icon(reader); } notifyIcon1.Visible = true; notifyIcon1.Text = "Observational Assistance"; notifyIcon1.Click += OnNotify_Click; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1200, 670); this.Controls.Add(this.resumeMirror); this.Controls.Add(this.spoolBack); this.Controls.Add(this.spoolForward); this.Controls.Add(this.statusTextBox); this.Controls.Add(this.screenCapture); this.Controls.Add(this.buttonCaptureScreen); this.Name = "Observational Assistance"; this.Text = "Observational Assistance"; ((System.ComponentModel.ISupportInitialize)(this.screenCapture)).EndInit(); this.ResumeLayout(false); }