网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

c#分割桌面截屏,进行图片流比较,不同的图片进行原图片重绘【附示例代码下载】

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

关于桌面截屏图片分割为16*8块大小一样的图片,然后把截屏的图和原来的图片进行比较,一样就不发送,不同则发送然后覆盖以前的图片。完整示例代码下载

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Data; 
  8. using System.Windows.Documents; 
  9. using System.Windows.Input; 
  10. using System.Windows.Media; 
  11. using System.Windows.Media.Imaging; 
  12. using System.Windows.Navigation; 
  13. using System.Windows.Shapes; 
  14. using System.IO; 
  15. using System.Drawing; 
  16. using System.Runtime.InteropServices; 
  17. using System.Threading; 
  18. using System.Drawing.Imaging; 
  19.  
  20. namespace photoText 
  21.     /// <summary> 
  22.     /// Window1.xaml 的交互逻辑 
  23.     /// </summary> 
  24.     public partial class Window1 : Window 
  25.     { 
  26.         private delegate void UpImageCall(ImageSource Isource); 
  27.         private UpImageCall upimage; 
  28.         Bitmap ALL; 
  29.         public static int x = 16; 
  30.         public static int y = 8; 
  31.         public static int ScreenX; 
  32.         public static int ScreenY; 
  33.         public static int SpX; 
  34.         public static int SpY; 
  35. //保存所以小张图片流 
  36.  List<Bitmap> LBitmap = new List<Bitmap>(); 
  37.  
  38.         public Window1() 
  39.         { 
  40.             InitializeComponent(); 
  41.             upimage = Isource => 
  42.             { 
  43.                 image1.Source = Isource; 
  44.             }; 
  45.                  //获取桌面大小 
  46.             ScreenX = GetScreenPixel()[0]; 
  47.             ScreenY = GetScreenPixel()[1]; 
  48.             //获取小张图片的长宽 
  49.             SpX = ScreenX / x; 
  50.             SpY = ScreenY / y; 
  51.             //初始化图片数组 
  52.             ALL = CaptureDesktopALL(); 
  53.             CaptureDesktopT(); 
  54.             Thread aa = new Thread(new ThreadStart(() => 
  55.             { 
  56.                 while (true
  57.                 { 
  58.                     //每隔300毫秒截一张图 
  59.                     Thread.Sleep(300); 
  60.                     CaptureDesktop(); 
  61.  
  62.                 } 
  63.             })); 
  64.             aa.Start(); 
  65.             aa.IsBackground = true
  66.         } 
  67.         /// <summary> 
  68.         /// 获取屏幕分辨率 
  69.         /// </summary> 
  70.         public int[] GetScreenPixel() 
  71.         { 
  72.             System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen; 
  73.             System.Drawing.Rectangle rc = scr.Bounds; 
  74.             int[] sp = new int[2] { rc.Width, rc.Height }; 
  75.             return sp; 
  76.         } 
  77.         /// <summary> 
  78.         /// 截屏 
  79.         /// </summary> 
  80.         [DllImport("gdi32.dll")] 
  81.         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); 
  82.          
  83.         public void CaptureDesktop() 
  84.         { 
  85.             for (int j = 0; j < x; j++) 
  86.             { 
  87.                 for (int k = 0; k < y; k++) 
  88.                 { 
  89.                     try 
  90.                     { 
  91.                         Bitmap screenBitmap; 
  92.                         using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) 
  93.                         { 
  94.                             //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* 
  95.                             screenBitmap = new Bitmap(SpX, SpY, screenGraphic); 
  96.                             //建立位图相关Graphics 
  97.                             Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); 
  98.                             //建立屏幕上下文 
  99.                             IntPtr hdcScreen = screenGraphic.GetHdc(); 
  100.                             //建立位图上下文 
  101.                             IntPtr hdcBitmap = bitmapGraphics.GetHdc(); 
  102.                             //将屏幕捕获保存在位图中 
  103.                             BitBlt(hdcBitmap, 0, 0, SpX, SpY, hdcScreen, j * SpX, k * SpY, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); 
  104.                             //关闭位图句柄 
  105.                             bitmapGraphics.ReleaseHdc(hdcBitmap); 
  106.                             //关闭屏幕句柄 
  107.                             screenGraphic.ReleaseHdc(hdcScreen); 
  108.                             //释放位图对像 
  109.                             bitmapGraphics.Dispose(); 
  110.                             //释放屏幕对像 
  111.                             screenGraphic.Dispose(); 
  112.                             //比较之前的图片和现在的图片是否相同,不同则替换 
  113.                             if (ImageCompareString(LBitmap[j * 8 + k], screenBitmap)) 
  114.                             { 
  115.                                 //保存最新的图片 
  116.                                 LBitmap[j * 8 + k] = screenBitmap; 
  117.                                 Graphics g = Graphics.FromImage(ALL); 
  118.                                 //覆盖原来的图片区域 
  119.                                 g.DrawImageUnscaled(LBitmap[j * 8 + k], j * SpX, k * SpY); 
  120.                             } 
  121.                         } 
  122.                     } 
  123.                     catch 
  124.                     { 
  125.                         throw
  126.                     } 
  127.                 } 
  128.             } 
  129.             MemoryStream stream = new MemoryStream(); 
  130.             ALL.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
  131.             // MStream(ALL); 
  132.             ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); 
  133.             //显示整张图片 
  134.             image1.Dispatcher.Invoke(upimage, (ImageSource)imageSourceConverter.ConvertFrom(stream)); 
  135.         } 
  136.         //获得压缩文件流 减少像素 
  137.         private MemoryStream MStream(Bitmap myBitmap) 
  138.         { 
  139.             ImageCodecInfo myImageCodecInfo; 
  140.             System.Drawing.Imaging.Encoder myEncoder; 
  141.             EncoderParameter myEncoderParameter; 
  142.             EncoderParameters myEncoderParameters; 
  143.             MemoryStream ms = new MemoryStream(); 
  144.             //设置Mime类型 
  145.             myImageCodecInfo = GetEncoderInfo("image/jpeg"); 
  146.             myEncoder = System.Drawing.Imaging.Encoder.Quality; 
  147.             myEncoderParameters = new EncoderParameters(1); 
  148.             //设置图片像素100L为最大 
  149.             myEncoderParameter = new EncoderParameter(myEncoder, 50L); 
  150.             myEncoderParameters.Param[0] = myEncoderParameter; 
  151.             myBitmap.Save(ms, myImageCodecInfo, myEncoderParameters); 
  152.             return ms; 
  153.         } 
  154.         private static ImageCodecInfo GetEncoderInfo(String mimeType) 
  155.         { 
  156.             int j; 
  157.             ImageCodecInfo[] encoders; 
  158.             encoders = ImageCodecInfo.GetImageEncoders(); 
  159.             for (j = 0; j < encoders.Length; ++j) 
  160.             { 
  161.                 if (encoders[j].MimeType == mimeType) 
  162.                     return encoders[j]; 
  163.             } 
  164.             return null
  165.         } 
  166.         /// <summary> 
  167.         /// 抓取有鼠标的桌面,并且能抓到透明窗体 
  168.         /// </summary> 
  169.         /// <returns></returns> 
  170.         public void CaptureDesktopT() 
  171.         { 
  172.             for (int j = 0; j < x; j++) 
  173.             { 
  174.                 for (int k = 0; k < y; k++) 
  175.                 { 
  176.                     try 
  177.                     { 
  178.                         Bitmap screenBitmap; 
  179.                         using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) 
  180.                         { 
  181.                             //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* 
  182.                             //screenBitmap = new Bitmap(1280, 800, screenGraphic); 
  183.                             screenBitmap = new Bitmap(SpX, SpY, screenGraphic); 
  184.                             //建立位图相关Graphics 
  185.                             Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); 
  186.                             //建立屏幕上下文 
  187.                             IntPtr hdcScreen = screenGraphic.GetHdc(); 
  188.                             //建立位图上下文 
  189.                             IntPtr hdcBitmap = bitmapGraphics.GetHdc(); 
  190.                             //将屏幕捕获保存在位图中 
  191.                             BitBlt(hdcBitmap, 0, 0, SpX, SpY, hdcScreen, j * SpX, k * SpY, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); 
  192.                             //关闭位图句柄 
  193.                             bitmapGraphics.ReleaseHdc(hdcBitmap); 
  194.                             //关闭屏幕句柄 
  195.                             screenGraphic.ReleaseHdc(hdcScreen); 
  196.                             //释放位图对像 
  197.                             bitmapGraphics.Dispose(); 
  198.                             //释放屏幕对像 
  199.                             screenGraphic.Dispose(); 
  200.                             //初始化16*8张图片 
  201.                             LBitmap.Add(screenBitmap); 
  202.                         } 
  203.                     } 
  204.                     catch 
  205.                     { 
  206.                         
  207.                     } 
  208.                 } 
  209.             } 
  210.             //Graphics g = Graphics.FromImage(ALL); 
  211.             //g.DrawImageUnscaled(LBitmap[2], 200, 200); 
  212.             //MemoryStream stream = new MemoryStream(); 
  213.             //ALL.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
  214.             //ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); 
  215.             //image1.Source = (ImageSource)imageSourceConverter.ConvertFrom(stream); 
  216.         } 
  217.         public Bitmap CaptureDesktopALL() 
  218.         { 
  219.             Bitmap screenBitmap; 
  220.             using (Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero)) 
  221.             { 
  222.                 //根据屏幕大小建立位图,这就是最后截取到的*屏幕图像* 
  223.                 //screenBitmap = new Bitmap(1280, 800, screenGraphic); 
  224.                 screenBitmap = new Bitmap(GetScreenPixel()[0], GetScreenPixel()[1], screenGraphic); 
  225.                 //建立位图相关Graphics 
  226.                 Graphics bitmapGraphics = Graphics.FromImage(screenBitmap); 
  227.                 //建立屏幕上下文 
  228.                 IntPtr hdcScreen = screenGraphic.GetHdc(); 
  229.                 //建立位图上下文 
  230.                 IntPtr hdcBitmap = bitmapGraphics.GetHdc(); 
  231.                 //将屏幕捕获保存在位图中 
  232.                 BitBlt(hdcBitmap, 0, 0, GetScreenPixel()[0], GetScreenPixel()[1], hdcScreen, 0, 0, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy); 
  233.                 //关闭位图句柄 
  234.                 bitmapGraphics.ReleaseHdc(hdcBitmap); 
  235.                 //关闭屏幕句柄 
  236.                 screenGraphic.ReleaseHdc(hdcScreen); 
  237.                 //释放位图对像 
  238.                 bitmapGraphics.Dispose(); 
  239.                 //释放屏幕对像 
  240.                 screenGraphic.Dispose(); 
  241.                 MemoryStream stream = new MemoryStream(); 
  242.                 screenBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
  243.                 ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); 
  244.                 image1.Source = (ImageSource)imageSourceConverter.ConvertFrom(stream); 
  245.                 return screenBitmap; 
  246.             } 
  247.         } 
  248.         //图片比较类 
  249.         public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage) 
  250.         { 
  251.             MemoryStream ms = new MemoryStream(); 
  252.             firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
  253.             String firstBitmap = Convert.ToBase64String(ms.ToArray()); 
  254.             ms.Position = 0; 
  255.             secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
  256.             String secondBitmap = Convert.ToBase64String(ms.ToArray()); 
  257.             if (firstBitmap.Equals(secondBitmap)) 
  258.             { 
  259.                 return false
  260.             } 
  261.             else 
  262.             { 
  263.                 return true
  264.             } 
  265.         } 
  266.     } 

 

  • 上一篇资讯: 在Asp.netMVC中使用Repeater
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师