1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace WebApplication3.Clases
- {
- public static class Utilities
- {
- public static string String2Png(string data)
- {
- List<string> rows = data.Split("\r\n",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
- List<string> cols = rows[0].Split(" ",StringSplitOptions.RemoveEmptyEntries).ToList<string>();
- int width = cols.Count;
- int height = rows.Count;
- int[,] integers = new int[height, width];
- int stride = width * 4;
- string fileName = @"C:\Users\Admin\Desktop\AAAA\green.png";
-
- string[,] array2Db = new string[height, width];
- for(int i= 0; i < height; i++)
- {
- string[] values = rows[i].Split(" ",StringSplitOptions.RemoveEmptyEntries);
- for(int j= 0; j< width; j++)
- {
- string hexColor = Constants._recharge_geometry_colors[values[j]];
- array2Db[i, j] = hexColor;
- }
- }
-
- // Fill array with random values
- for (int x = 0; x < height; ++x)
- {
- for (int y = 0; y < width; ++y)
- {
- var color = System.Drawing.ColorTranslator.FromHtml(array2Db[x, y]);
- byte[] bgra = new byte[] { color.B, color.G, color.R, color.A };
- integers[x, y] = BitConverter.ToInt32(bgra, 0);
- }
- }
-
- Bitmap b;
- unsafe
- {
- fixed (int* intPtr = &integers[0, 0])
- {
- b = new Bitmap(width, height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
- }
- }
-
-
- using (b)
- {
- b.Save(fileName, ImageFormat.Png);
- }
- return fileName;
- }
-
- public static void BinaryToFile(byte[] ba, string fileName)
- {
- using (FileStream fs = new FileStream(fileName, FileMode.Create))
- {
- using (BinaryWriter bw = new BinaryWriter(fs))
- {
- bw.Write(ba);
- }
- }
- }
- }
- }
|