关于桌面截屏图片分割为16*8块大小一样的图片,然后把截屏的图和原来的图片进行比较,一样就不发送,不同则发送然后覆盖以前的图片。
/*http://home.cnblogs.com/yangleiWPF/*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; using System.Drawing.Imaging; namespace photoText { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { private delegate void UpImageCall(ImageSource Isource); private UpImageCall upimage; Bitmap ALL; public static int x = 16; public static int y = 8; public static int ScreenX; public static int ScreenY; public static int SpX; public static int SpY; //保存所以小张图片流
List<Bitmap> LBitmap = new List<Bitmap>();
public Window1() { InitializeComponent(); upimage = Isource => { image1.Source = Isource; }; //获取桌面大小 ScreenX = GetScreenPixel()[0]; ScreenY = GetScreenPixel()[1]; //获取小张图片的长宽 SpX = ScreenX / x; SpY = ScreenY / y; //初始化图片数组 ALL = CaptureDesktopALL(); CaptureDesktopT(); Thread aa = new Thread(new ThreadStart(() => { while (true) { //每隔300毫秒截一张图 Thread.Sleep(300); CaptureDesktop(); } })); aa.Start(); aa.IsBackground = true; } /// <summary> /// 获取屏幕分辨率 /// </summary> public int[] GetScreenPixel() { System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen; System.Drawing.Rectangle rc = scr.Bounds; int[] sp = new int[2] { rc.Width, rc.Height }; return sp; } /// <summary> /// 截屏 /// </summary> [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Drawing.CopyPixelOperation dwRop); public void CaptureDesktop() { for (int j = 0; j < x; j++) { for (int k = 0; k < y; k++) { try { Bitmap screenBitmap; using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) { //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* screenBitmap = new Bitmap(SpX, SpY, screenGraphic); //建立位图相关Graphics Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); //建立屏幕上下文 IntPtr hdcScreen = screenGraphic.GetHdc(); //建立位图上下文 IntPtr hdcBitmap = bitmapGraphics.GetHdc(); //将屏幕捕获保存在位图中 BitBlt(hdcBitmap, 0, 0, SpX, SpY, hdcScreen, j * SpX, k * SpY, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); //关闭位图句柄 bitmapGraphics.ReleaseHdc(hdcBitmap); //关闭屏幕句柄 screenGraphic.ReleaseHdc(hdcScreen); //释放位图对像 bitmapGraphics.Dispose(); //释放屏幕对像 screenGraphic.Dispose(); //比较之前的图片和现在的图片是否相同,不同则替换 if (ImageCompareString(LBitmap[j * 8 + k], screenBitmap)) { //保存最新的图片 LBitmap[j * 8 + k] = screenBitmap; Graphics g = Graphics.FromImage(ALL); //覆盖原来的图片区域 g.DrawImageUnscaled(LBitmap[j * 8 + k], j * SpX, k * SpY); } } } catch { throw; } } } MemoryStream stream = new MemoryStream(); ALL.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // MStream(ALL); ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); //显示整张图片 image1.Dispatcher.Invoke(upimage, (ImageSource)imageSourceConverter.ConvertFrom(stream)); } //获得压缩文件流 减少像素 private MemoryStream MStream(Bitmap myBitmap) { ImageCodecInfo myImageCodecInfo; System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; MemoryStream ms = new MemoryStream(); //设置Mime类型 myImageCodecInfo = GetEncoderInfo("image/jpeg"); myEncoder = System.Drawing.Imaging.Encoder.Quality; myEncoderParameters = new EncoderParameters(1); //设置图片像素100L为最大 myEncoderParameter = new EncoderParameter(myEncoder, 50L); myEncoderParameters.Param[0] = myEncoderParameter; myBitmap.Save(ms, myImageCodecInfo, myEncoderParameters); return ms; } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; } /// <summary> /// 抓取有鼠标的桌面,并且能抓到透明窗体 /// </summary> /// <returns></returns> public void CaptureDesktopT() { for (int j = 0; j < x; j++) { for (int k = 0; k < y; k++) { try { Bitmap screenBitmap; using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) { //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* //screenBitmap = new Bitmap(1280, 800, screenGraphic); screenBitmap = new Bitmap(SpX, SpY, screenGraphic); //建立位图相关Graphics Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); //建立屏幕上下文 IntPtr hdcScreen = screenGraphic.GetHdc(); //建立位图上下文 IntPtr hdcBitmap = bitmapGraphics.GetHdc(); //将屏幕捕获保存在位图中 BitBlt(hdcBitmap, 0, 0, SpX, SpY, hdcScreen, j * SpX, k * SpY, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); //关闭位图句柄 bitmapGraphics.ReleaseHdc(hdcBitmap); //关闭屏幕句柄 screenGraphic.ReleaseHdc(hdcScreen); //释放位图对像 bitmapGraphics.Dispose(); //释放屏幕对像 screenGraphic.Dispose(); //初始化16*8张图片 LBitmap.Add(screenBitmap); } } catch { } } } //Graphics g = Graphics.FromImage(ALL); //g.DrawImageUnscaled(LBitmap[2], 200, 200); //MemoryStream stream = new MemoryStream(); //ALL.Save(stream, System.Drawing.Imaging.ImageFormat.Png); //ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); //image1.Source = (ImageSource)imageSourceConverter.ConvertFrom(stream); } public Bitmap CaptureDesktopALL() { Bitmap screenBitmap; using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) { //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* //screenBitmap = new Bitmap(1280, 800, screenGraphic); screenBitmap = new Bitmap(GetScreenPixel()[0], GetScreenPixel()[1], screenGraphic); //建立位图相关Graphics Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); //建立屏幕上下文 IntPtr hdcScreen = screenGraphic.GetHdc(); //建立位图上下文 IntPtr hdcBitmap = bitmapGraphics.GetHdc(); //将屏幕捕获保存在位图中 BitBlt(hdcBitmap, 0, 0, GetScreenPixel()[0], GetScreenPixel()[1], hdcScreen, 0, 0, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); //关闭位图句柄 bitmapGraphics.ReleaseHdc(hdcBitmap); //关闭屏幕句柄 screenGraphic.ReleaseHdc(hdcScreen); //释放位图对像 bitmapGraphics.Dispose(); //释放屏幕对像 screenGraphic.Dispose(); MemoryStream stream = new MemoryStream(); screenBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); image1.Source = (ImageSource)imageSourceConverter.ConvertFrom(stream); return screenBitmap; } } //图片比较类 public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage) { MemoryStream ms = new MemoryStream(); firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); String firstBitmap = Convert.ToBase64String(ms.ToArray()); ms.Position = 0; secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); String secondBitmap = Convert.ToBase64String(ms.ToArray()); if (firstBitmap.Equals(secondBitmap)) { return false; } else { return true; } } } }