Utilities.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace WebApplication3.Clases
  9. {
  10. public static class Utilities
  11. {
  12. public static string String2Png(string data)
  13. {
  14. List<string> rows = data.Split("\r\n",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
  15. List<string> cols = rows[0].Split(" ",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
  16. int width = cols.Count;
  17. int height = rows.Count;
  18. int[,] integers = new int[height, width];
  19. int stride = width * 4;
  20. string fileName = @"C:\Users\Admin\Desktop\AAAA\green.png";
  21. string[,] array2Db = new string[height, width];
  22. for(int i= 0; i < height; i++)
  23. {
  24. string[] values = rows[i].Split(" ",StringSplitOptions.RemoveEmptyEntries);
  25. for(int j= 0; j< width; j++)
  26. {
  27. string hexColor = Constants._recharge_geometry_colors[values[j]];
  28. array2Db[i, j] = hexColor;
  29. }
  30. }
  31. // Fill array with random values
  32. for (int x = 0; x < height; ++x)
  33. {
  34. for (int y = 0; y < width; ++y)
  35. {
  36. var color = System.Drawing.ColorTranslator.FromHtml(array2Db[x, y]);
  37. byte[] bgra = new byte[] { color.B, color.G, color.R, color.A };
  38. integers[x, y] = BitConverter.ToInt32(bgra, 0);
  39. }
  40. }
  41. Bitmap b;
  42. unsafe
  43. {
  44. fixed (int* intPtr = &integers[0, 0])
  45. {
  46. b = new Bitmap(width, height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
  47. }
  48. }
  49. using (b)
  50. {
  51. b.Save(fileName, ImageFormat.Png);
  52. }
  53. return fileName;
  54. }
  55. public static void BinaryToFile(byte[] ba, string fileName)
  56. {
  57. using (FileStream fs = new FileStream(fileName, FileMode.Create))
  58. {
  59. using (BinaryWriter bw = new BinaryWriter(fs))
  60. {
  61. bw.Write(ba);
  62. }
  63. }
  64. }
  65. public static bool ModifySVG(string fileOriginal, string x, string y, string length, string height, string destinationFile)
  66. {
  67. string text = File.ReadAllText(fileOriginal);
  68. bool result = true;
  69. List<string> textLines = text.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();
  70. string aux = textLines.Find(x => x.Contains("metadata"));
  71. if (aux != null)
  72. {
  73. try
  74. {
  75. float latitude = float.Parse(x) + float.Parse(length);
  76. float longitude = float.Parse(y) + float.Parse(height);
  77. int index = textLines.IndexOf(aux);
  78. textLines[index] += "\r\n <MetaInfo xmlns=\"http://www.prognoz.ru\">\r\n < Geo >\r\n < GeoItem X = \"" + 0 + "\" Y = \"" + 0 + "\" Latitude = \"" + x + "\" Longitude = \"" + y + "\" /> < GeoItem X = \"" + x + "\" Y = \"" + y + "\" Latitude = \"" + latitude + "\" Longitude = \"" + longitude + "\" />\r\n </ Geo >\r\n </ MetaInfo > ";
  79. File.WriteAllLines(destinationFile, textLines);
  80. }
  81. catch(Exception ex)
  82. {
  83. Logger.WriteLog(ex.Message);
  84. Logger.WriteLog(ex.InnerException.Message);
  85. result = false; ;
  86. }
  87. }
  88. return result;
  89. }
  90. }
  91. }