搜索
aardio官方社区 门户 查看主题

截图并获取二进制数据问题

发布者: ltmf | 发布时间: 2016-7-28 21:56| 查看数: 1316| 评论数: 1|帖子模式

如题,soImage库轻松办到,但编个小程序就要带个2M的库心有不甘。于是参照网上和库源码研究多日不成功,现特向各位老师请教。
  1. import win.ui;
  2. /*DSG{{*/
  3. var winform = ..win.form(text="aardio form";right=471;bottom=341)
  4. winform.add(
  5. button={cls="button";text="button";left=160;top=254;right=261;bottom=295;z=1};
  6. picturebox={cls="picturebox";left=59;top=34;right=225;bottom=94;autosize=1;z=2}
  7. )
  8. /*}}*/

  9. GetDIBits = ::Gdi32.api("GetDIBits","int(ptr aHDC,ptr hBitmap,int nStartScan,int nNumScans,ptr lpBits,struct& lpBI,int wUsage)")
  10. class BITMAP {
  11.         int bmType;
  12.         int bmWidth;
  13.         int bmHeight;
  14.         int bmWidthBytes;
  15.         WORD bmPlanes;
  16.         WORD bmBitsPixel;
  17.         pointer bmBits;
  18. };
  19. class BITMAPINFOHEADER{
  20.         int biSize;
  21.         int biWidth;
  22.         int biHeight;       
  23.         WORD biPlanes;
  24.         WORD biBitCount;
  25.         int biCompression;
  26.         int biSizeImage;
  27.         int biXPelsPerMeter;
  28.         int biYPelsPerMeter;
  29.         int biClrUsed;
  30.         int biClrImportant;
  31. };       
  32. var BITMAPINFOHEADER = BITMAPINFOHEADER;
  33. class BITMAPINFO{                 
  34.         struct BITMAPINFOHEADER = BITMAPINFOHEADER();
  35.         int bmiColors;
  36. };
  37. class BITMAPFILEHEADER{                 
  38.         WORD bfType;
  39.         int bfSize;
  40.         WORD bfReserved1;
  41.         WORD bfReserved2;
  42.         int bfOffBits;
  43. };       
  44. winform.button.oncommand = function(id,event){               
  45.         var bmp=BITMAP()//位图
  46.         var bmpinfo =BITMAPINFO() ; //位图信息
  47.         var bmpfh=BITMAPFILEHEADER()       
  48.         var hdc = ::GetDC(0)//创建屏幕DC           
  49.     var hMemDc = CreateCompatibleDC(hdc);//创建内存DC
  50.     var hbmp = ::CreateCompatibleBitmap(hdc,200,200);
  51.         var hOldBitmap = ::SelectObject(hMemDc,hbmp);
  52.         ::BitBlt( hMemDc , 0, 0,200,200, hdc,0,0,0xCC0020/*_SRCCOPY*/ )
  53.         ::SelectObject(hMemDc, hOldBitmap);
  54.         //winform.picturebox.setBitmap(hbmp)//调试输出
  55.         ::GetObject(hbmp,24,bmp);
  56.         bmpinfo.BITMAPINFOHEADER.biSize =44;       
  57.         GetDIBits(hMemDc,hbmp,0,0,null,bmpinfo,0);               
  58.         var pi=raw.buffer(bmpinfo.BITMAPINFOHEADER.biSizeImage);//位图像素点阵
  59.         bmpinfo.BITMAPINFOHEADER.biCompression=0
  60.         GetDIBits(hMemDc,hbmp,0,bmp.bmHeight,pi,bmpinfo,0);       
  61.         //构造位图信息
  62.         var l=raw.tostring(bmpinfo)       
  63.         var bmpinfobyte = ::GlobalAlloc( 0x2 /*_GMEM_MOVEABLE*/,bmpinfo.BITMAPINFOHEADER.biSize); //位图信息字节集
  64.         pImageMemory = ::GlobalLock(bmpinfobyte);
  65.         ::CopyMemory(pImageMemory,l,bmpinfo.BITMAPINFOHEADER.biSize);
  66.         ::GlobalUnlock(bmpinfobyte);
  67.         //构造位图文件头       
  68.         bmpfh.bfType=19778
  69.         bmpfh.bfOffBits=14+bmpinfo.BITMAPINFOHEADER.biSize
  70.         bmpfh.bfSize=bmpfh.bfOffBits+bmpinfo.BITMAPINFOHEADER.biSizeImage
  71.         bmpfh.bfReserved1=0
  72.         bmpfh.bfReserved2=0
  73.         var l=raw.tostring(bmpfh)
  74.         var bmpfhbyte = ::GlobalAlloc( 0x2 /*_GMEM_MOVEABLE*/,14);//位图文件头字节集
  75.         pImageMemory = ::GlobalLock(bmpfhbyte);
  76.         ::CopyMemory(pImageMemory,l,14);
  77.         ::GlobalUnlock(bmpfhbyte);
  78.         var str=raw.tostring(bmpfhbyte)+raw.tostring(bmpinfobyte)+raw.tostring(pi)
  79.         winform.picturebox.image=str//显示图象不成功       
  80.         ::SelectObject(hMemDc, hOldBitmap);
  81.         ::DeleteDC(hMemDc);       
  82.         ::ReleaseDC(winform.hwnd,hdc);
  83.         ::DeleteObject(hbmp)       
  84. }
  85. winform.show()
  86. win.loopMessage();
复制代码

最新评论

手机版|未经许可严禁引用或转载本站文章|站长邮箱|aardio.com|aardio官方社区 ( 皖ICP备09012014号 )

GMT+8, 2017-11-21 16:17 , Processed in 0.046875 second(s), 18 queries , WinCache On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表