Utilities.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace WebApplication3.Clases
  8. {
  9. public static class Utilities
  10. {
  11. public static bool String2Png(string data)
  12. {
  13. Logger.WriteLog("im in");
  14. bool result = true;
  15. List<string> rows = data.Split("\r\n",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
  16. Logger.WriteLog("rows has a lenght of" + rows.Count);
  17. List<string> cols = rows[0].Split(" ",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
  18. Logger.WriteLog("cols has a length of " + cols.Count);
  19. int width = cols.Count;
  20. int height = rows.Count;
  21. int[,] integers = new int[height, width];
  22. int stride = width * 4;
  23. string[,] array2Db = new string[height, width];
  24. for(int i= 0; i < height; i++)
  25. {
  26. string[] values = rows[i].Split(" ",StringSplitOptions.RemoveEmptyEntries);
  27. for(int j= 0; j< width; j++)
  28. {
  29. string hexColor = Constants._recharge_geometry_colors[values[j]];
  30. array2Db[i, j] = hexColor;
  31. }
  32. }
  33. // Fill array with random values
  34. Random random = new Random();
  35. for (int x = 0; x < height; ++x)
  36. {
  37. for (int y = 0; y < width; ++y)
  38. {
  39. var color = System.Drawing.ColorTranslator.FromHtml(array2Db[x, y]);
  40. byte[] bgra = new byte[] { color.B, color.G, color.R, color.A };
  41. integers[x, y] = BitConverter.ToInt32(bgra, 0);
  42. }
  43. }
  44. Bitmap b;
  45. unsafe
  46. {
  47. fixed (int* intPtr = &integers[0, 0])
  48. {
  49. b = new Bitmap(width, height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
  50. }
  51. }
  52. using (b)
  53. {
  54. b.Save(@"C:\Users\Admin\Desktop\AAAA\green.png", ImageFormat.Png);
  55. }
  56. return result;
  57. }
  58. }
  59. }