Created
July 11, 2014 13:11
-
-
Save CipherLab/31c87b1f41e791d1f3f9 to your computer and use it in GitHub Desktop.
General useful functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using System.Web.Script.Serialization; | |
public class General | |
{ | |
#region colorization code | |
protected Color HexStringToColor(string hex) | |
{ | |
hex = hex.Replace("#", ""); | |
if (hex.Length != 6) | |
throw new Exception(hex + | |
" is not a valid 6-place hexadecimal color code."); | |
string r, g, b; | |
r = hex.Substring(0, 2); | |
g = hex.Substring(2, 2); | |
b = hex.Substring(4, 2); | |
return System.Drawing.Color.FromArgb(HexStringToBase10Int(r), | |
HexStringToBase10Int(g), | |
HexStringToBase10Int(b)); | |
} | |
protected int HexStringToBase10Int(string hex) | |
{ | |
int base10value = 0; | |
try { base10value = System.Convert.ToInt32(hex, 16); } | |
catch { base10value = 0; } | |
return base10value; | |
} | |
static char[] hexDigits = { | |
'0', '1', '2', '3', '4', '5', '6', '7', | |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | |
public static string ColorToHexString(Color color) | |
{ | |
byte[] bytes = new byte[3]; | |
bytes[0] = color.R; | |
bytes[1] = color.G; | |
bytes[2] = color.B; | |
char[] chars = new char[bytes.Length * 2]; | |
for (int i = 0; i < bytes.Length; i++) | |
{ | |
int b = bytes[i]; | |
chars[i * 2] = hexDigits[b >> 4]; | |
chars[i * 2 + 1] = hexDigits[b & 0xF]; | |
} | |
return new string(chars); | |
} | |
#endregion | |
private void DeleteFile(string tempFile) | |
{ | |
try | |
{ | |
if (File.Exists(tempFile)) | |
{ | |
File.Delete(tempFile); | |
} | |
} | |
catch { } | |
} | |
public static byte[] StringToByteArray(string str) | |
{ | |
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); | |
return encoding.GetBytes(str); | |
} | |
public static string ImageToBase64(Image image, ImageFormat format) | |
{ | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
// Convert Image to byte[] | |
image.Save(ms, format); | |
byte[] imageBytes = ms.ToArray(); | |
// Convert byte[] to Base64 String | |
string base64String = Convert.ToBase64String(imageBytes); | |
return base64String; | |
} | |
} | |
public static string ByteArrayToString(byte[] input) | |
{ | |
UTF8Encoding enc = new UTF8Encoding(); | |
string str = enc.GetString(input); | |
return str; | |
} | |
public static Image getImageFromURL(string url) | |
{ | |
if (string.IsNullOrWhiteSpace(url)) | |
{ | |
url = "http://i.imgur.com/zxBgpGu.png"; | |
} | |
var webClient = new WebClient(); | |
byte[] imageBytes = webClient.DownloadData(url); | |
return byteArrayToImage(imageBytes); | |
} | |
public static Image byteArrayToImage(byte[] byteArrayIn) | |
{ | |
try | |
{ | |
MemoryStream ms = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length); | |
ms.Write(byteArrayIn, 0, byteArrayIn.Length); | |
return Image.FromStream(ms, true); | |
} | |
catch | |
{ | |
} | |
return null; | |
} | |
public static string CsvToJson(string value) | |
{ | |
// Get lines. | |
if (value == null) return null; | |
string[] lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | |
if (lines.Length < 2) throw new InvalidDataException("Must have header line."); | |
// Get headers. | |
string[] headers = lines.First().Split(','); | |
// Build JSON array. | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("["); | |
for (int i = 1; i < lines.Length; i++) | |
{ | |
//string[] fields = lines[i].Split(','); | |
List<string> fields = new List<string>(); | |
var regex = new Regex("(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)"); | |
foreach (Match m in regex.Matches(lines[i])) | |
{ | |
fields.Add(m.Value.Replace("\"", "")); | |
} | |
if (fields.Count != headers.Length) throw new InvalidDataException("Field count must match header count."); | |
var jsonElements = headers.Zip(fields, (header, field) => string.Format("\"{0}\": \"{1}\"", header, field)).ToArray(); | |
string jsonObject = "{" + string.Format("{0}", string.Join(",", jsonElements)) + "}"; | |
if (i < lines.Length - 1) | |
jsonObject += ","; | |
sb.AppendLine(jsonObject); | |
} | |
sb.AppendLine("]"); | |
return sb.ToString(); | |
} | |
public static string ObjectToJson(object obj) | |
{ | |
var js = new JavaScriptSerializer(); | |
return js.Serialize(obj); | |
} | |
public static object JsonToObject(string datain, Type objectType) | |
{ | |
var js = new JavaScriptSerializer(); | |
return js.Deserialize(datain, objectType); | |
} | |
public static bool IsNumber(string s) | |
{ | |
return s.All(char.IsDigit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment