1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO;
namespace SchoolClient.Utils { internal class ZScreenUtil2 { public static bool CaptureScreenSave(string filePath) { int lastindex = filePath.LastIndexOf(Path.DirectorySeparatorChar); if (lastindex < filePath.Length) { string fileFolder = filePath.Substring(0, lastindex); string filename = filePath.Substring(lastindex + 1); string tempFilePath = fileFolder + Path.DirectorySeparatorChar + "_temp_" + filename;
string basePath = AppDomain.CurrentDomain.BaseDirectory; string toolsPath = System.IO.Path.Combine(basePath, "Tools");
Process mps = new Process(); ProcessStartInfo mpsi = new ProcessStartInfo("nircmd.exe", "savescreenshot \"" + tempFilePath+"\"" ) { WorkingDirectory = toolsPath, UseShellExecute = true, RedirectStandardInput = false, RedirectStandardOutput = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }; mps.StartInfo = mpsi; mps.Start(); mps.WaitForExit(); mps.Close();
bool result = GetPicThumbnail(tempFilePath, filePath, 1920, 1080, 80, true); return result; } else { return false; } }
public static bool GetPicThumbnail(string sFile, string dFile, int dWidth, int dHeight, int ratio, bool deleteSFile) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat;
Size tem_size = new Size(iSource.Width, iSource.Height); int sW; int sH; if (tem_size.Width > dHeight || tem_size.Width > dWidth) { if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth)) { sW = dWidth; sH = (dWidth * tem_size.Height) / tem_size.Width; } else { sH = dHeight; sW = (tem_size.Width * dHeight) / tem_size.Height; } } else { sW = tem_size.Width; sH = tem_size.Height; }
Bitmap ob = new Bitmap(dWidth, dHeight); Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage( iSource, new Rectangle((dWidth - sW) / 2,(dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel );
g.Dispose(); EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = ratio; EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { ob.Save(dFile, jpegICIinfo, ep); } else { ob.Save(dFile, tFormat); } return true; } catch { return false; } finally { iSource.Dispose(); ob.Dispose();
if (deleteSFile) { FileInfo di = new FileInfo(sFile); if (di.Exists) { try { di.Delete(); } catch (Exception) { } } } } } } }
|