Quasar/Client/Core/Helper/Helper.cs

161 lines
5.5 KiB
C#
Raw Normal View History

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;
using System.Text.RegularExpressions;
2014-07-08 12:58:53 +00:00
using System.Windows.Forms;
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
}
public static Bitmap GetDesktop(int screenNumber)
2014-07-08 12:58:53 +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);
using (Graphics graph = Graphics.FromImage(screenshot))
2015-01-14 12:15:31 +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)
throw new Exception("Sizes must be equal.");
Bitmap bmpRes = null;
System.Drawing.Imaging.BitmapData bmData = null;
System.Drawing.Imaging.BitmapData bmData2 = null;
System.Drawing.Imaging.BitmapData bmDataRes = null;
try
{
bmpRes = new Bitmap(bmp.Width, bmp.Height);
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);
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
byte* p = (byte*)scan0.ToPointer();
p += y * stride;
byte* p2 = (byte*)scan02.ToPointer();
p2 += y * stride2;
byte* pRes = (byte*)scan0Res.ToPointer();
pRes += y * strideRes;
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;
}
}
bmp.UnlockBits(bmData);
bmp2.UnlockBits(bmData2);
bmpRes.UnlockBits(bmDataRes);
}
catch
{
if (bmData != null)
{
try
{
bmp.UnlockBits(bmData);
}
catch
{ }
}
if (bmData2 != null)
{
try
{
bmp2.UnlockBits(bmData2);
}
catch
{ }
}
if (bmDataRes != null)
{
try
{
bmpRes.UnlockBits(bmDataRes);
}
catch
{ }
}
if (bmpRes != null)
{
bmpRes.Dispose();
bmpRes = null;
}
}
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
}
public static string FormatMacAddress(string macAddress)
{
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");
}
2014-07-08 12:58:53 +00:00
}
}