Tanoda
ScreneCapture.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5using System.Linq;
6using System.Runtime.InteropServices;
7using System.Text;
8using System.Drawing;
9using System.ComponentModel;
10using System.IO;
11using UnityEngine.UI;
12using NaughtyAttributes;
13#if !UNITY_WEBGL
14using System.Diagnostics;
15using System.Drawing.Imaging;
16using System.Text.RegularExpressions;
17#endif
18public class ScreneCapture : MonoBehaviour
19{
20 #if !UNITY_WEBGL
21 [DllImport("gdi32.dll")]
22 static extern bool BitBlt(IntPtr hdcDest, int nxDest, int nyDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
23
24 [DllImport("gdi32.dll")]
25 static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int width, int nHeight);
26
27 [DllImport("gdi32.dll")]
28 static extern IntPtr CreateCompatibleDC(IntPtr hdc);
29
30 [DllImport("gdi32.dll")]
31 static extern IntPtr DeleteDC(IntPtr hdc);
32
33 [DllImport("gdi32.dll")]
34 static extern IntPtr DeleteObject(IntPtr hObject);
35
36 [DllImport("user32.dll")]
37 static extern IntPtr GetDesktopWindow();
38
39 [DllImport("user32.dll")]
40 static extern IntPtr GetWindowDC(IntPtr hWnd);
41
42 [DllImport("user32.dll")]
43 static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
44
45 [DllImport("gdi32.dll")]
46 static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
47
48 [DllImport("user32.dll")]
49 static extern IntPtr GetForegroundWindow();
50 [DllImport("user32.dll")]
51
52 private static extern IntPtr FindWindow(string className, string windowName);
53
54 const int SRCCOPY = 0x00CC0020;
55
56 const int CAPTUREBLT = 0x40000000;
57 bool Reloading = true;
58 public RawImage rawImage;
59 public GameObject StageManager;
60 internal int LeftCorr;
61 internal int TopCorr;
62 internal int RightCorr;
63 internal int BottomCorr;
64 internal float xWidth, yWidth;
65 private float fps;
66 internal Queue<float> fpsQueue;
67 private float avgFPS;
68 private float i = 1;
69
70 //public Camera cam;
71#endif
72 [Button]
73 public void ApplyTexture()
74 {
75 #if !UNITY_WEBGL
76 var hWnd =
77#if UNITY_EDITOR
78 GetForegroundWindow();
79#else
80 FindWindow("UnityWndClass", "VR Skills Toolkit");
81#endif
82
83 var frame = CaptureWindow(hWnd);
84 Texture2D texture = new Texture2D(90, 90);
86 Bitmap original = (Bitmap)frame;
87 Bitmap resized = new Bitmap(original, new Size(original.Width, original.Height));
88 MemoryStream msFinger = new MemoryStream();
89 resized.Save(msFinger, ImageFormat.Png);
90 texture.LoadImage(msFinger.ToArray());
91 DestroyImmediate(rawImage.texture);
92 rawImage.texture = texture;
93#endif
94 }
95
96 public void ImageResize()
97 {
98 #if !UNITY_WEBGL
99 GetComponent<RectTransform>().SetWidth(StageManager.GetComponent<RectTransform>().sizeDelta.x/4);
100 GetComponent<RectTransform>().SetHeight(StageManager.GetComponent<RectTransform>().sizeDelta.y/4);
101 GetComponent<BoxCollider>().size = new Vector3(GetComponent<RectTransform>().sizeDelta.x, GetComponent<RectTransform>().sizeDelta.y, 0.000001f);
102 GetComponent<BoxCollider>().center = new Vector3(GetComponent<RectTransform>().sizeDelta.x / 2, GetComponent<RectTransform>().sizeDelta.y / 2, 0);
103#endif
104 }
105 public void Start()
106 {
107 #if !UNITY_WEBGL
108 fpsQueue = new Queue<float>();
109 StartCoroutine(ScreenRefresher());
110#endif
111
112 }
113 public void Update()
114 {
115 #if !UNITY_WEBGL
116 fps= 1/Time.deltaTime;
117 fpsQueue.Enqueue(fps);
118 if (fpsQueue.Count > 9)
119 {
120 var avg = default(float);
121 foreach (var v in fpsQueue)
122 {
123 avg += v;
124 }
125
126 avg /= fpsQueue.Count;
127 avgFPS = avg;
128 while (fpsQueue.Count > 9)
129 {
130 fpsQueue.Dequeue();
131 }
132 }
133#endif
134 }
135 IEnumerator ScreenRefresher()
136 {
137 #if !UNITY_WEBGL
138 while (Reloading)
139 {
140 if (avgFPS >= 40)
141 {
142 i -= 0.02f;
143 }
144 if (avgFPS < 40 && i<1)
145 {
146 i += 0.02f;
147 }
148 ApplyTexture();
149 yield return new WaitForSeconds(i);
150 }
151#else
152 yield return null;
153#endif
154
155 }
156
157#if !UNITY_WEBGL
158 private ImageCodecInfo GetEncoder(ImageFormat format)
159 {
160 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
161
162 foreach (ImageCodecInfo codec in codecs)
163 {
164 if (codec.FormatID == format.Guid)
165 {
166 return codec;
167 }
168 }
169
170 return null;
171 }
172
173 public Bitmap CaptureRegion(int x1, int x2, int y1, int y2)
174 {
175 IntPtr desktophWnd;
176 IntPtr desktopDc;
177 IntPtr memoryDc;
178 IntPtr bitmap;
179 IntPtr oldBitmap;
180 bool success;
181 Bitmap result;
182
183 desktophWnd = GetDesktopWindow();
184 desktopDc = GetWindowDC(desktophWnd);
185 memoryDc = CreateCompatibleDC(desktopDc);
186 bitmap = CreateCompatibleBitmap(desktopDc, x2-x1, y2-y1);
187 oldBitmap = SelectObject(memoryDc, bitmap);
188
189 success = BitBlt(memoryDc, 0, 0, x2-x1, y2-y1, desktopDc, x1, y1, SRCCOPY | CAPTUREBLT);
190
191 try
192 {
193 if (!success)
194 {
195 throw new Win32Exception();
196 }
197
198 result = System.Drawing.Image.FromHbitmap(bitmap);
199 }
200 finally
201 {
202 SelectObject(memoryDc, oldBitmap);
203 DeleteObject(bitmap);
204 DeleteDC(memoryDc);
205 ReleaseDC(desktophWnd, desktopDc);
206 }
207 return result;
208 }
209 [DllImport("user32.dll", SetLastError = true)]
210 public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
211#endif
212
213 [StructLayout(LayoutKind.Sequential)]
214 public struct RECT
215 {
216 public int Left;
217 public int Top;
218 public int Right;
219 public int Bottom;
220
221 }
222
223#if !UNITY_WEBGL
224 public Bitmap CaptureWindow(IntPtr hWnd)
225 {
226 RECT region;
227
228 //Capture(hWnd);
229 Vector3[] center = new Vector3[4];
230 StageManager.GetComponent<RectTransform>().GetWorldCorners(center);
231 //Camera cam = Camera.main;
232 //Vector3 centerScreen = cam.WorldToScreenPoint(center);
233 Vector3 size = StageManager.GetComponent<RectTransform>().sizeDelta;
234 //Bounds bounds = new Bounds(center,size);
235 xWidth = center[2].x - center[0].x;
236 yWidth = center[1].y - center[0].y;
237 LeftCorr = (int)center[0].x;
238 TopCorr = Screen.height - (int)center[1].y;
239 RightCorr = Screen.width - (int)center[2].x;
240 BottomCorr = (int)center[3].y;
241 GetWindowRect(hWnd, out region);
242
243 return this.CaptureRegion(region.Left + LeftCorr/*+10*/, region.Right - RightCorr/*-10*/, region.Top + TopCorr/*+40*/, region.Bottom - BottomCorr);
244
245 }
246#endif
247
248}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
RawImage rawImage
static bool GetWindowRect(IntPtr hwnd, out RECT lpRect)
GameObject StageManager
void ApplyTexture()
Bitmap CaptureRegion(int x1, int x2, int y1, int y2)
Bitmap CaptureWindow(IntPtr hWnd)
void ImageResize()