public static void SaveScreenshot(Stream stream, FormatImage format) { System.Diagnostics.Process p = Warframe.GetProcess(); if (p == null) { throw new Exception(); } IntPtr ptr = p.MainWindowHandle; User32.Rect rect = new User32.Rect(); User32.GetWindowRect(ptr, ref rect); int width = rect.Right - rect.Left; int height = rect.Bottom - rect.Top; if (height == 0 || width == 0) { return; } using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { using (var graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(rect.Left, rect.Top, 0, 0, new Size(width, height)); graphics.Save(); graphics.Dispose(); Bitmap bit; switch (format) { case FormatImage.TIFF: bit = MakeGrayscale3(bitmap); bit.Save(stream, System.Drawing.Imaging.ImageFormat.Tiff); bit.Dispose(); break; case FormatImage.PNG: default: bit = MakeGrayscale3(bitmap); bit.Save(stream, System.Drawing.Imaging.ImageFormat.Png); bit.Dispose(); #if DEBUG //using (FileStream file = new FileStream("shot_" + DateTime.Now.ToString("HH_mm_ss") + ".png", FileMode.Create, FileAccess.Write)) //{ // ((MemoryStream)stream).WriteTo(file); //} #endif break; } } } }
//上传图片 public async Task <IActionResult> Picture([FromServices] IWebHostEnvironment env, [FromForm] IFormFile imgFile) { if (imgFile != null && imgFile.Length > 0 && imgFile.Length < 20971520) { var fileExt = Path.GetExtension(imgFile.FileName); var filePath = Path.Combine(env.WebRootPath, Guid.NewGuid().ToString()); var picture = new EditPictureViewModel { Origin = $"{filePath}_Origin{fileExt}", Big = $"{filePath}_Big{fileExt}", Small = $"{filePath}_Small{fileExt}" }; using (var stream = imgFile.OpenReadStream()) { FormatImage.AutoToSmall(stream, picture.Big, 1080); FormatImage.AutoToSmall(stream, picture.Small, 260); } await _pictureService.AddPicture(picture); } return(View()); }
//上传图片 public async Task <IActionResult> Picture([FromServices] IWebHostEnvironment env, [FromForm] IFormFile imgFile) { //图片最大<18M if (imgFile != null && imgFile.Length > 0 && imgFile.Length < 18874368) { //图片扩展名 var fileExt = Path.GetExtension(imgFile.FileName); var rootPath = env.WebRootPath; //按年月归档图片 var now = DateTime.Now; var timePath = Path.Combine(now.Year.ToString(), now.Month.ToString()); var filePath = Path.Combine("photo", timePath); //创建图片目录 if (!Directory.Exists(Path.Combine(rootPath, filePath))) { Directory.CreateDirectory(Path.Combine(rootPath, filePath)); } filePath = Path.Combine(filePath, Guid.NewGuid().ToString()); var picture = new EditPictureViewModel { //原图可能太大,暂时不保存 Origin = $"{filePath}_origin{fileExt}", Big = $"{filePath}_big{fileExt}", Small = $"{filePath}_small{fileExt}" }; using (var stream = imgFile.OpenReadStream()) { //为了不影响图片观感,建议图片质量值设置为80以上 await FormatImage.AutoToSmall(stream, Path.Combine(rootPath, picture.Big), 1920, 99); await FormatImage.AutoToSmall(stream, Path.Combine(rootPath, picture.Small), 1080, 99); } await _pictureService.AddPicture(picture); } return(View()); }