-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathUIHelper.cs
More file actions
266 lines (227 loc) · 9.29 KB
/
UIHelper.cs
File metadata and controls
266 lines (227 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//-----------------------------------------------------------------------------
// Filename: UIHelper.cs
//
// Description: This class contains some helper and utility methods for WPF
// applications.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 01 Jan 2015 Aaron Clauson Added to softphone project, Hobart, Australia.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace SIPSorcery.SoftPhone
{
public static class UIHelper
{
private const int MOUSE_INPUT = 0;
private const int MOUSEEVENTF_MOVE = 0x0001;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
internal struct INPUT
{
public int TYPE;
// This is supposed to be a union for all input types, but we only want mouse so...
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
public static void DoOnUIThread(this Dispatcher dispatcher, Action action)
{
dispatcher.Invoke(DispatcherPriority.Render, (SendOrPostCallback)delegate { action(); }, null);
}
/// <summary>
/// Retrieve the time of the last user interaction
/// </summary>
/// <returns></returns>
public static TimeSpan GetLastInput()
{
var plii = new LASTINPUTINFO();
plii.cbSize = (uint)Marshal.SizeOf(plii);
if (GetLastInputInfo(ref plii))
{
return TimeSpan.FromMilliseconds(Environment.TickCount - plii.dwTime);
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
/// <summary>
/// Reset the last input timer. We move the mouse one pixel to reset the system timer.
/// </summary>
public static void ResetIdleTimer()
{
INPUT input = new INPUT();
input.TYPE = MOUSE_INPUT;
input.dx = 1;
input.dy = 1;
input.mouseData = 0;
input.dwFlags = MOUSEEVENTF_MOVE;
input.time = 0;
input.dwExtraInfo = (IntPtr)0;
if (SendInput(1, ref input, System.Runtime.InteropServices.Marshal.SizeOf(input)) != 1)
{
throw new Win32Exception();
}
}
public static byte[] LoadImageBytes(Uri imageURI)
{
if (imageURI != null)
{
var streamInfo = Application.GetResourceStream(imageURI);
var ms = new MemoryStream();
var imgBytes = new byte[streamInfo.Stream.Length];
using (var stream = streamInfo.Stream)
{
stream.Read(imgBytes, 0, (int)stream.Length);
}
return imgBytes;
}
return null;
}
public static System.Drawing.Bitmap LoadBitmap(string imageURI)
{
return LoadBitmap(new Uri(imageURI, UriKind.Absolute));
}
public static System.Drawing.Bitmap LoadBitmap(Uri imageURI)
{
var bmp = new BitmapImage(imageURI);
int height = bmp.PixelHeight;
int width = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
width,
height,
stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
ptr);
return bitmap;
}
}
}
/// <summary>
/// Converts supplied screen coordinates into the correct WPF device-independent coordinates using an
/// existing control or window
/// </summary>
/// <param name="visual">The control or window the screen point is in</param>
/// <param name="screenPoint">the point in screen coordinates to be transformed</param>
/// <returns>new point representing the screen coordinates in WPF coordinates</returns>
public static Point ScreenCoordinatesToWPF(Visual visual, Point screenPoint)
{
var t = PresentationSource.FromVisual(visual).CompositionTarget.TransformFromDevice;
return t.Transform(screenPoint);
}
public static BitmapImage GetBitmapImageFromBase64String(string base64Bitmap)
{
BitmapImage scannedImage = new BitmapImage();
scannedImage.BeginInit();
scannedImage.StreamSource = new MemoryStream(Convert.FromBase64String(base64Bitmap));
scannedImage.EndInit();
return scannedImage;
}
public static string GetJPEGBase64StringFromBitmapImage(BitmapImage bmpImage, long jpegEncoderQuality, System.Drawing.RotateFlipType rotateFlip)
{
System.Drawing.Bitmap bmp = null;
MemoryStream ms = new MemoryStream();
try
{
System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(myEncoder, jpegEncoderQuality);
System.Drawing.Imaging.ImageCodecInfo jgpEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg);
bmp = UIHelper.BitmapImage2Bitmap(bmpImage);
bmp.RotateFlip(rotateFlip);
bmp.Save(ms, jgpEncoder, encoderParameters);
return Convert.ToBase64String(ms.ToArray());
}
finally
{
DeleteObject(bmp.GetHbitmap());
bmp.Dispose();
bmp = null;
}
}
public static System.Drawing.Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
return new System.Drawing.Bitmap(bitmap);
}
}
public static BitmapImage Bitmap2BitmapImage(System.Drawing.Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSource bmpSource;
BitmapImage bitmapImage = new BitmapImage();
try
{
bmpSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
BitmapEncoder enc = new BmpBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
enc.Frames.Add(BitmapFrame.Create(bmpSource));
enc.Save(memoryStream);
memoryStream.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
}
finally
{
DeleteObject(hBitmap);
}
return bitmapImage;
}
private static System.Drawing.Imaging.ImageCodecInfo GetEncoder(System.Drawing.Imaging.ImageFormat format)
{
System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();
foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
}
}