2014-07-08 12:58:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2015-03-21 18:14:00 +00:00
|
|
|
|
using System.Text;
|
2015-02-24 20:58:20 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
using System.Windows.Forms;
|
2015-05-21 16:27:43 +00:00
|
|
|
|
using System.Drawing.Imaging;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-01-13 18:29:11 +00:00
|
|
|
|
namespace xClient.Core.Helper
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-01-13 18:43:55 +00:00
|
|
|
|
public static class Helper
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
|
|
|
|
private const string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
2015-01-14 12:15:31 +00:00
|
|
|
|
private static readonly Random _rnd = new Random(Environment.TickCount);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
public static string GetRandomFilename(int length, string extension)
|
|
|
|
|
{
|
2015-03-21 18:14:00 +00:00
|
|
|
|
StringBuilder randomName = new StringBuilder(length);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
for (int i = 0; i < length; i++)
|
2015-03-21 18:14:00 +00:00
|
|
|
|
randomName.Append(CHARS[_rnd.Next(CHARS.Length)]);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-03-21 18:14:00 +00:00
|
|
|
|
return string.Concat(randomName.ToString(), extension);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetRandomName(int length)
|
|
|
|
|
{
|
2015-03-21 18:14:00 +00:00
|
|
|
|
StringBuilder randomName = new StringBuilder(length);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
for (int i = 0; i < length; i++)
|
2015-03-21 18:14:00 +00:00
|
|
|
|
randomName.Append(CHARS[_rnd.Next(CHARS.Length)]);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-03-21 18:14:00 +00:00
|
|
|
|
return randomName.ToString();
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 08:26:57 +00:00
|
|
|
|
public static Bitmap GetDesktop(int screenNumber)
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-04-02 08:26:57 +00:00
|
|
|
|
var bounds = Screen.AllScreens[screenNumber].Bounds;
|
2015-01-14 12:15:31 +00:00
|
|
|
|
var screenshot = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
2015-03-31 18:38:16 +00:00
|
|
|
|
using (Graphics graph = Graphics.FromImage(screenshot))
|
2015-01-14 12:15:31 +00:00
|
|
|
|
{
|
2015-03-31 18:38:16 +00:00
|
|
|
|
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
|
|
|
|
|
return screenshot;
|
2015-01-14 12:15:31 +00:00
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static unsafe Bitmap GetDiffDesktop(Bitmap bmp, Bitmap bmp2)
|
|
|
|
|
{
|
|
|
|
|
if (bmp.Width != bmp2.Width || bmp.Height != bmp2.Height)
|
2015-05-21 16:16:18 +00:00
|
|
|
|
throw new InvalidOperationException("Sizes must be equal.");
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
Bitmap bmpRes = null;
|
|
|
|
|
|
2015-05-21 16:27:43 +00:00
|
|
|
|
BitmapData bmData = null;
|
|
|
|
|
BitmapData bmData2 = null;
|
|
|
|
|
BitmapData bmDataRes = null;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bmpRes = new Bitmap(bmp.Width, bmp.Height);
|
|
|
|
|
|
2015-04-21 18:27:52 +00:00
|
|
|
|
bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
|
|
|
|
bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
|
|
|
|
bmDataRes = bmpRes.LockBits(new Rectangle(0, 0, bmpRes.Width, bmpRes.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
IntPtr scan0 = bmData.Scan0;
|
|
|
|
|
IntPtr scan02 = bmData2.Scan0;
|
|
|
|
|
IntPtr scan0Res = bmDataRes.Scan0;
|
|
|
|
|
|
|
|
|
|
int stride = bmData.Stride;
|
|
|
|
|
int stride2 = bmData2.Stride;
|
|
|
|
|
int strideRes = bmDataRes.Stride;
|
|
|
|
|
|
|
|
|
|
int nWidth = bmp.Width;
|
|
|
|
|
int nHeight = bmp.Height;
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < nHeight; y++)
|
|
|
|
|
{
|
|
|
|
|
//define the pointers inside the first loop for parallelizing
|
2015-05-21 16:27:43 +00:00
|
|
|
|
byte* p = (byte*)scan0.ToPointer();
|
|
|
|
|
p += y * stride;
|
|
|
|
|
byte* p2 = (byte*)scan02.ToPointer();
|
|
|
|
|
p2 += y * stride2;
|
|
|
|
|
byte* pRes = (byte*)scan0Res.ToPointer();
|
|
|
|
|
pRes += y * strideRes;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
for (int x = 0; x < nWidth; x++)
|
|
|
|
|
{
|
|
|
|
|
//always get the complete pixel when differences are found
|
|
|
|
|
if (p[0] != p2[0] || p[1] != p2[1] || p[2] != p2[2])
|
|
|
|
|
{
|
|
|
|
|
pRes[0] = p2[0];
|
|
|
|
|
pRes[1] = p2[1];
|
|
|
|
|
pRes[2] = p2[2];
|
|
|
|
|
|
|
|
|
|
//alpha (opacity)
|
|
|
|
|
pRes[3] = p2[3];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p += 4;
|
|
|
|
|
p2 += 4;
|
|
|
|
|
pRes += 4;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2015-05-21 16:27:43 +00:00
|
|
|
|
{ }
|
|
|
|
|
finally
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-05-21 16:27:43 +00:00
|
|
|
|
if (bmp != null)
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-05-21 16:27:43 +00:00
|
|
|
|
bmp.UnlockBits(bmData);
|
|
|
|
|
bmp.Dispose();
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
2015-05-21 16:27:43 +00:00
|
|
|
|
if (bmp2 != null)
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-05-21 16:27:43 +00:00
|
|
|
|
bmp2.UnlockBits(bmData2);
|
|
|
|
|
bmp2.Dispose();
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
if (bmpRes != null)
|
|
|
|
|
{
|
2015-05-21 16:29:15 +00:00
|
|
|
|
bmpRes.UnlockBits(bmDataRes);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bmpRes;
|
|
|
|
|
}
|
2014-07-18 16:23:04 +00:00
|
|
|
|
|
|
|
|
|
public static bool IsWindowsXP()
|
|
|
|
|
{
|
2015-01-14 12:15:31 +00:00
|
|
|
|
var osVersion = Environment.OSVersion.Version;
|
|
|
|
|
return osVersion.Major == 5 && osVersion.Minor >= 1;
|
2014-07-18 16:23:04 +00:00
|
|
|
|
}
|
2015-02-24 20:58:20 +00:00
|
|
|
|
|
|
|
|
|
public static string FormatMacAddress(string macAddress)
|
|
|
|
|
{
|
2015-04-21 18:27:52 +00:00
|
|
|
|
return (macAddress.Length != 12)
|
|
|
|
|
? "00:00:00:00:00:00"
|
|
|
|
|
: Regex.Replace(macAddress, "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})", "$1:$2:$3:$4:$5:$6");
|
2015-02-24 20:58:20 +00:00
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
2015-04-21 18:27:52 +00:00
|
|
|
|
}
|