ScreenOper
Posted Pete-Jones
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ScreenOper相关的知识,希望对你有一定的参考价值。
/// <summary> /// 屏幕操作类 /// Add by 2017-07-25 /// 1、屏幕生成Image 方法 /// 2、Image按百分比压缩 方法 /// 3、Image根据指定像素生成方法 /// </summary> public class ScreenOper { [DllImport("user32.dll")] static extern bool GetCursorInfo(out CURSORINFO pci); private const Int32 CURSOR_SHOWING = 0x00000001; [StructLayout(LayoutKind.Sequential)] struct POINT { public Int32 x; public Int32 y; } [StructLayout(LayoutKind.Sequential)] struct CURSORINFO { public Int32 cbSize; public Int32 flags; public IntPtr hCursor; public POINT ptScreenPos; } /// <summary> /// 屏幕截图方法 /// 1、将整个屏幕截图 /// 2、并画出鼠标形状 /// </summary> /// <returns></returns> public static Image ScreenToImg() { Image myimage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(myimage); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new Rectangle(pci.ptScreenPos.x - 10, pci.ptScreenPos.y - 10, cur.Size.Width, cur.Size.Height)); return myimage; } /// <summary> /// 将图片按百分比压缩,flag取值1到100,越小压缩比越大 /// </summary> /// <param name="iSource"></param> /// <param name="outPath"></param> /// <param name="flag"></param> /// <returns></returns> public static bool ImgZip(Image iSource, string outPath, int flag) { ImageFormat tFormat = iSource.RawFormat; EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag; EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo jpegICIinfo = null; jpegICIinfo = arrayICI.Where(c => c.FormatDescription.Equals("JPEG")).FirstOrDefault(); if (jpegICIinfo != null) iSource.Save(outPath, jpegICIinfo, ep); else iSource.Save(outPath, tFormat); } catch { return false; } iSource.Dispose(); return true; } /// <summary> /// 将图片按百分比压缩,flag取值1到100,越小压缩比越大 /// </summary> /// <param name="iSource">原图片</param> /// <param name="flag">1-100</param> /// <returns></returns> public static Image ImgZip(Image iSource, int flag) { Image img; MemoryStream mStream = new MemoryStream(); ImageFormat tFormat = iSource.RawFormat; EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag; EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo jpegICIinfo = null; jpegICIinfo = arrayICI.Where(c => c.FormatDescription.Equals("JPEG")).FirstOrDefault(); if (jpegICIinfo != null) iSource.Save(mStream, jpegICIinfo, ep); img = Image.FromStream(mStream); mStream.Close(); } catch { return iSource; } iSource.Dispose(); return img; } /// <summary> /// 将图片根据像素生成 /// </summary> /// <param name="srcImage"></param> /// <returns></returns> public static Image SetImageSize(Image srcImage, int dWidth, int dHeight) { Image ob; try { //按比例缩放 Size tem_size = new Size(srcImage.Width, srcImage.Height); int sW = 0, sH = 0; if (tem_size.Width > dHeight || tem_size.Width > dWidth) { if ((tem_size.Width * dHeight) > (tem_size.Height * 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; } 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(srcImage, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel); g.Dispose(); } catch (Exception) { return null; } return ob; } }
以上是关于ScreenOper的主要内容,如果未能解决你的问题,请参考以下文章