Code Viewer

./Classes/__gdip.js File Size: 24.34 KB

  1. /*
  2. * -----
  3. * Screenshot Sender - __gdip.js
  4. * -----
  5. * Gdip functions for Screenshot Sender
  6. * -----
  7. */
  8.  
  9. var Gdip = function (bSaveImage) {
  10.         this.GdipToken = Interop.Allocate(4);
  11.         this.GdipStartupInput = Interop.Allocate(16)
  12.                 this.GdipStartupInput.WriteDWORD(0, 1);
  13.         this.Initialized = false, this.ImageWidth, this.ImageHeight;
  14.         this.SaveSuccessful = false, this.ImageLocation, this.hBitmap = 0;
  15.         this.ImageFormats = {'BMP' : '{557CF400-1A04-11D3-9A73-0000F81EF32E}',
  16.                                                  'JPG' : '{557CF401-1A04-11D3-9A73-0000F81EF32E}',
  17.                                                  'GIF' : '{557CF402-1A04-11D3-9A73-0000F81EF32E}',
  18.                                                  'PNG' : '{557CF406-1A04-11D3-9A73-0000F81EF32E}' }
  19.         this.objImageQuality = {'DEFAULT_QUALITY' : 0,
  20.                                                         'DRAFT_QUALITY' : 1,
  21.                                                         'PROOF_QUALITY' : 2,
  22.                                                         'NONANTIALIASED_QUALITY' : 3,
  23.                                                         'ANTIALIASED_QUALITY' : 4,
  24.                                                         'CLEARTYPE_QUALITY' : 5};
  25.         this.bSaveImage = bSaveImage;
  26. }
  27.  
  28. Gdip.prototype = {
  29.         "Initialize" : function () { return this.Initialized = Interop.Call('gdiplus', 'GdiplusStartup', this.GdipToken, this.GdipStartupInput, 0) === 0; },
  30.        
  31.         "Uninitialize" : function () { Interop.Call('gdiplus', 'GdiplusShutdown', this.GdipToken.ReadDWORD(0) ); },
  32.        
  33.         "SaveImage" : function (hPicture, sLocation, JpgQuality) {
  34.                 if (typeof JpgQuality === 'undefined') JpgQuality = 75;
  35.                 if (typeof CreateThumbnail === 'undefined') CreateThumbnail = false;
  36.                 if (typeof hPicture === 'undefined' || typeof(sLocation) === 'undefined') return false;
  37.                 if (this.Initialized == false) return false;
  38.                
  39.                 var ImageFormat = Interop.Allocate(16);
  40.                 var lBitmap = Interop.Allocate(4);
  41.                 Interop.Call('ole32', 'CLSIDFromString', this.ImageFormats[aImageFormats[objPreferences['cFileType']]], ImageFormat);
  42.                 if (Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hPicture, 0, lBitmap) === 0) {
  43.                         objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ] = {};
  44.                         objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ].Picture = hPicture;
  45.                         objPictures[sLocation.substr(sLocation.lastIndexOf('\\') +1) ].Quality = JpgQuality;
  46.                         this.hBitmap = hPicture;
  47.                        
  48.                         var EncoderParameters = Interop.Allocate(32);
  49.                         with (EncoderParameters) {
  50.                                 WriteDWORD(0, 1);
  51.                                 WriteDWORD(20, 1);
  52.                                 WriteDWORD(24, 4);
  53.                                 if (objPreferences['cFileType'] === 1) {
  54.                                         var Ptr = Interop.Allocate(4);
  55.                                         Ptr.WriteDWORD(0, JpgQuality /* JPG QUALITY */);
  56.                                         WriteDWORD(28, Ptr.DataPtr);
  57.                                 }else{
  58.                                         WriteDWORD(28, 0);
  59.                                 }
  60.                         }
  61.                         Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
  62.            
  63.                         if (Interop.Call('gdiplus', 'GdipSaveImageToFile', lBitmap.ReadDWORD(0), sLocation, ImageFormat, EncoderParameters) === 0) {
  64.                                 this.SaveSuccessful = true;
  65.                                 this.ImageLocation = sLocation;
  66.                                 if (typeof objWindows['ScreenshotViewer'] === 'object') {
  67.                                         RefreshScreenshotViewerList();
  68.                                 }
  69.                         }
  70.                 }
  71.                 this.DisposeImage(lBitmap.ReadDWORD(0) );
  72.                 if (objPreferences['cPreviews'] == false) {
  73.                         Interop.Call('gdi32', 'DeleteObject', hPicture);
  74.                 }
  75.         },
  76.        
  77.     "CreateThumbnail" : function (hPicture) {
  78.         var ImageThumb = Interop.Allocate(4);
  79.         var lBitmap = Interop.Allocate(4);
  80.                 var hbmBitmap = Interop.Allocate(4);
  81.         Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hPicture, 0, lBitmap)
  82.         Interop.Call('gdiplus', 'GdipGetImageThumbnail', lBitmap.ReadDWORD(0), 100, this.ImageHeight / (this.ImageWidth/100), ImageThumb.DataPtr, 0, 0);
  83.         Interop.Call('gdiplus', 'GdipCreateHBITMAPFromBitmap', ImageThumb.ReadDWORD(0), hbmBitmap, 0x00000000);
  84.         return  hbmBitmap.ReadDWORD(0);
  85.     },
  86.    
  87.         "UpdateQuality" : function (hBitmap, sImage, JpgQuality) {
  88.                 var EncoderParameters = Interop.Allocate(32);
  89.                 var ImageFormat = Interop.Allocate(16);
  90.                 var lBitmap = Interop.Allocate(4);
  91.                
  92.                 Interop.Call('ole32', 'CLSIDFromString', '{557CF401-1A04-11D3-9A73-0000F81EF32E}', ImageFormat);
  93.                 if (Interop.Call('gdiplus', 'GdipCreateBitmapFromHBITMAP', hBitmap, 0, lBitmap) === 0) {
  94.                         with (EncoderParameters) {
  95.                                 WriteDWORD(0, 1);
  96.                                 WriteDWORD(20, 1);
  97.                                 WriteDWORD(24, 4);
  98.                                         var Ptr = Interop.Allocate(4);
  99.                                         Ptr.WriteDWORD(0, JpgQuality);
  100.                                         WriteDWORD(28, Ptr.DataPtr);
  101.                         }
  102.                         Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
  103.                         var ret = Interop.Call('gdiplus', 'GdipSaveImageToFile', lBitmap.ReadDWORD(0), sImage, ImageFormat, EncoderParameters);
  104.                         this.DisposeImage(lBitmap.ReadDWORD(0) );
  105.                         return ret === 0;
  106.                 }
  107.         },
  108.        
  109.         "ConvertImage" : function (sImageIn, sImageOut, nImgFrmt, JpgQuality) {
  110.                 var ret, hImage = this.LoadImageFromFile(sImageIn);
  111.                 if (hImage !== 0) {
  112.                         var ImageFormat = Interop.Allocate(16);
  113.                         Interop.Call('ole32', 'CLSIDFromString', this.ImageFormats[nImgFrmt], ImageFormat);
  114.                         var EncoderParameters = Interop.Allocate(32);
  115.                         with (EncoderParameters) {
  116.                                 WriteDWORD(0, 1);
  117.                                 WriteDWORD(20, 1);
  118.                                 WriteDWORD(24, 4);
  119.                                 if (nImgFrmt.toLowerCase() === 'jpg') {
  120.                                         var Ptr = Interop.Allocate(4);
  121.                                         Ptr.WriteDWORD(0, JpgQuality /* JPG QUALITY */);
  122.                                         WriteDWORD(28, Ptr.DataPtr);
  123.                                 }else { WriteDWORD(28, 0); }
  124.                         }
  125.                         Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
  126.                         ret = Interop.Call('gdiplus', 'GdipSaveImageToFile', hImage, sImageOut, ImageFormat, EncoderParameters);
  127.                         this.DisposeImage(hImage);
  128.                 } 
  129.                 return ret === 0;
  130.         },
  131.  
  132.         "CaptureWindow" : function (hWnd, rect, bIsFullscreen, bIsSelectedArea, bIsPointClickCapture, bIsThumbRequired) {
  133.                 if (typeof bIsPointClickCapture === 'undefined') bIsPointClickCapture = false;
  134.                 if (typeof bIsThumbRequired === 'undefined') bIsThumbRequired = false;
  135.                 if (typeof bIsSelectedArea === 'undefined') bIsSelectedArea = false;
  136.                
  137.         var sDC = _win32.GetWindowDC(hWnd);
  138.                 this.ImageWidth = rect.ReadDWORD(8) - rect.ReadDWORD(0);
  139.                 this.ImageHeight = rect.ReadDWORD(12) - rect.ReadDWORD(4);
  140.                
  141.                 var destDC = Interop.Call('gdi32', 'CreateCompatibleDC', sDC),
  142.                         hBMP = Interop.Call('gdi32', 'CreateCompatibleBitmap', sDC, this.ImageWidth, this.ImageHeight),
  143.                         hobj = Interop.Call('gdi32', 'SelectObject', destDC, hBMP),
  144.                         SystemArea = GetSystemArea();
  145.  
  146.                 if (bIsSelectedArea) {
  147.                         // Selected area - Position of rectangle on screen
  148.                         rect.WriteDWORD(0, rect.ReadDWORD(0) + SystemArea.Left);
  149.                         rect.WriteDWORD(4, rect.ReadDWORD(4) + SystemArea.Top);
  150.                 } else if (bIsFullscreen) {
  151.                         // Full screen - Top left corner of screen
  152.                         rect.WriteDWORD(0, SystemArea.Left);
  153.                         rect.WriteDWORD(4, SystemArea.Top);
  154.                 } else {
  155.                         // Window - Top left corner of window
  156.                         rect.WriteDWORD(0, 0);
  157.                         rect.WriteDWORD(4, 0);
  158.                 }
  159.  
  160.                 Interop.Call('gdi32', 'BitBlt', destDC, 0, 0, this.ImageWidth, this.ImageHeight, sDC, rect.ReadDWORD(0), rect.ReadDWORD(4), 0x40000000 /* CAPTUREBLT */ | 0xCC0020 /* SRCCOPY */);           
  161.                
  162.                 var oOverlayText = new OverlayText(destDC, this.bSaveImage, this.ImageWidth, this.ImageHeight);
  163.                
  164.                 Interop.Call('gdi32', 'SelectObject', destDC, hobj);
  165.                 _win32.ReleaseDC(hWnd, destDC);
  166.                 return hBMP;
  167.         },
  168.        
  169.         "CaptureWindow_Active" : function () {
  170.                 var hWnd = _win32.GetForegroundWindow(), rect = Interop.Allocate(16);
  171.                 _win32.GetWindowRect(hWnd, rect);
  172.                 var bIsSelectedArea = false;
  173.                 /*_win32.GetClassNameW(hWnd, lpsz, 512);
  174.                 //Check if this is a chat window
  175.                 if (lpsz.ReadString(0) === 'IMWindowClass' || lpsz.ReadString(0) === 'MSBLWindowClass') {
  176.                         if (_win32.WindowFromPoint(rect.ReadDWORD(0) +10, rect.ReadDWORD(4) +10) !== hWnd) {
  177.                                 //Use the DirectUIHWND instead
  178.                                 hWnd = _win32.FindWindowExW(hWnd, 0, 'DirectUIHWND', '');
  179.                                 _win32.GetWindowRect(hWnd, rect);
  180.                                 // Treat this window as a selected area
  181.                                 bIsSelectedArea = true;
  182.                         }              
  183.                 }*/
  184.                 return this.CaptureWindow(hWnd, rect, false, bIsSelectedArea);
  185.         },
  186.        
  187.         "CaptureWindow_Fullscreen" : function () {
  188.                 var rect = Interop.Allocate(16), SystemArea = GetSystemArea();
  189.                 rect.WriteDWORD(0, SystemArea.Top);
  190.                 rect.WriteDWORD(4, SystemArea.Left);
  191.                 rect.WriteDWORD(8, SystemArea.Width);
  192.                 rect.WriteDWORD(12, SystemArea.Height);
  193.                 return this.CaptureWindow(_win32.GetDesktopWindow(), rect, true);
  194.         },
  195.        
  196.         "CaptureWindow_Clipboard" : function () {
  197.                 var nImgPtr = 0;
  198.                 try {
  199.                         _win32.OpenClipboard(0);
  200.                         nImgPtr = _win32.GetClipboardData(_win32._const._CF_BITMAP);
  201.                 } catch(e) {
  202.                         _debug.error(e, 'CaptureWindow_Clipboard');
  203.                 } finally {
  204.                         _win32.CloseClipboard();
  205.                 }
  206.                 return nImgPtr;
  207.         },
  208.        
  209.         "LoadImageFromFile" : function (sImage) {
  210.                 var nImgPtr = Interop.Allocate(4);
  211.                 Interop.Call('gdiplus', 'GdipLoadImageFromFile', sImage, nImgPtr);
  212.                 return nImgPtr.ReadDWORD(0) !== 0 ? nImgPtr.ReadDWORD(0) : false;
  213.         },
  214.        
  215.         "DisposeImage" : function (hImage) { Interop.Call('gdiplus', 'GdipDisposeImage', hImage); },
  216.        
  217.         "GdiCreateHBITMAPFromFile" : function (sImage) {
  218.                 if (this.Initialized == false) return false;
  219.                 var lngBitmap = Interop.Allocate(4), hbm = false;
  220.                 var lGdipCreateBitmapFromFile = Interop.Call('gdiplus', 'GdipCreateBitmapFromFile', sImage, lngBitmap);
  221.                 if (lGdipCreateBitmapFromFile == 0) {
  222.                         var hbmBitmap = Interop.Allocate(4);
  223.                         var lGdipCreateHBITMAPFromBitmap = Interop.Call('gdiplus', 'GdipCreateHBITMAPFromBitmap', lngBitmap.ReadDWORD(0), hbmBitmap, 0x00000000);
  224.                         if (lGdipCreateHBITMAPFromBitmap === 0) hbm = hbmBitmap.ReadDWORD(0);
  225.                 }
  226.                 this.DisposeImage(lngBitmap.ReadDWORD(0) );
  227.                 return hbm;
  228.                
  229.         },
  230.        
  231.         "Clipboard_SetData" : function (hBitmap, hWnd) {
  232.                 if (typeof hWnd === 'undefined') hWnd = 0;
  233.                 try {
  234.                         _win32.OpenClipboard(hWnd);
  235.                         _win32.EmptyClipboard();
  236.                         _debug.trace(_win32.SetClipboardData(_win32._const._CF_BITMAP, hBitmap) );
  237.                 } catch(e) {
  238.                         _debug.error(e, 'Clipboard_SetData');
  239.                 } finally {
  240.                         _win32.CloseClipboard();
  241.                 }
  242.         },
  243.        
  244.         "GetImageDimensions" : function (sImageLocation) {
  245.                 var _width = Interop.Allocate(4),
  246.                         _height = Interop.Allocate(4),
  247.                         hImage = this.LoadImageFromFile(sImageLocation);
  248.                 if (hImage !== 0) {
  249.                         Interop.Call('gdiplus', 'GdipGetImageWidth', hImage, _width);
  250.                         Interop.Call('gdiplus', 'GdipGetImageHeight', hImage, _height);
  251.                         this.DisposeImage(hImage);
  252.                 }
  253.                 return _width.ReadDWORD(0)+' × '+_height.ReadDWORD(0);
  254.         },
  255.        
  256.         "GetDateCreated" : function (sImageLocation) {
  257.                 var BY_HANDLE_FILE_INFORMATION = Interop.Allocate(52);
  258.                 var _h_file = _win32.CreateFileW(sImageLocation, _win32._const._GENERIC_READ, _win32._const._FILE_SHARE_READ, 0, _win32._const._OPEN_EXISTING, 0, 0);
  259.                 _win32.GetFileInformationByHandle(_h_file, BY_HANDLE_FILE_INFORMATION);
  260.                 _win32.CloseHandle(_h_file);
  261.                
  262.                 var FILETIME = Interop.Allocate(8),
  263.                         FILETIMELOCALTIME = Interop.Allocate(8);
  264.                 FILETIME.WriteDWORD(0, BY_HANDLE_FILE_INFORMATION.ReadDWORD(4) );
  265.                 FILETIME.WriteDWORD(4, BY_HANDLE_FILE_INFORMATION.ReadDWORD(8) );
  266.                 Interop.Call('kernel32', 'FileTimeToLocalFileTime', FILETIME, FILETIMELOCALTIME);
  267.                
  268.                 var SYSTEMTIME = Interop.Allocate(16);
  269.                 Interop.Call('kernel32', 'FileTimeToSystemTime', FILETIMELOCALTIME, SYSTEMTIME);
  270.                
  271.                 var sTime = Interop.Allocate(512),
  272.                         sDate = Interop.Allocate(512);
  273.                
  274.                 Interop.Call('kernel32', 'GetTimeFormatW', 0x400 /* LOCALE_USER_DEFAULT */, 0, SYSTEMTIME, 0, sTime, 512);
  275.                 Interop.Call('kernel32', 'GetDateFormatW', 0x400 /* LOCALE_USER_DEFAULT */, 0, SYSTEMTIME, 0, sDate, 512);
  276.                 return sDate.ReadString(0)+' '+sTime.ReadString(0);
  277.         },
  278.        
  279.         "GetImageSizeInBytes" : function (sImageLocation) {
  280.                 var BY_HANDLE_FILE_INFORMATION = Interop.Allocate(52);
  281.                 var _h_file = _win32.CreateFileW(sImageLocation, _win32._const._GENERIC_READ, _win32._const._FILE_SHARE_READ, 0, _win32._const._OPEN_EXISTING, 0, 0);
  282.                 _win32.GetFileInformationByHandle(_h_file, BY_HANDLE_FILE_INFORMATION);
  283.                 _win32.CloseHandle(_h_file);
  284.                 return BY_HANDLE_FILE_INFORMATION.ReadDWORD(36); /* size of file */
  285.         },
  286.        
  287.         "FormatFileSize" : (function() {
  288.                 var dwKB = 0x400,
  289.                         dwMB = 0x400 * dwKB,
  290.                         dwGB = 0x400 * dwMB;
  291.                
  292.                 return function (_lsize) {
  293.                         if (_lsize >= dwGB) return Math.round(_lsize/dwGB) +' GB';
  294.                         else if (_lsize >= dwMB) return Math.round(_lsize/dwMB) +' MB';
  295.                         else if (_lsize >= dwKB) return Math.round(_lsize/dwKB) +' KB';
  296.                         else if  (_lsize > 0 && _lsize < 0x400) return Math.round(_lsize) +' B';
  297.                         else return '0 KB';
  298.                 }
  299.         })()
  300. };

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162