Please tell me how to convert the image color indices (stored for example in text txt format) into an image. code example met on the Runet https://sohabr.net/habr/post/195344/ or http://deadlock.org.ua/kit/habr/post/21221 or http://all-best-tips.com /52930-how-to-convert-rgb-images-to-text.html or http://learn-myself.com/convert-rgb-images-to-text-8319/ or http: //www.pvsm. ru / programmirovanie / 44259 / print / but the code failed to run. in the first part - converting to a matrix is ​​less or less smooth, but in the second - like "back" but nothing at all .. write to file

это запись Π² Ρ„Π°ΠΉΠ» ΠΈΠ»ΠΈ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Π° ΠΊΠΎΠ΄Π° работоспособная: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Drawing; namespace steamWriter { class Program { static void Main(string[] args) { Bitmap bmp = new Bitmap(@"c:\x1.bmp"); // c : \ 1.bmp - адрСс изобраТСния Color[,] color = new Color[bmp.Width, bmp.Height]; for (int y = 0; y < bmp.Height; y++) for (int x = 0; x < bmp.Width; x++) { color[x, y] = bmp.GetPixel(x, y); } StreamWriter steamWriter = new StreamWriter(@"z:\x5.txt"); for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { steamWriter.Write(color[x, y].R.ToString("X2")); steamWriter.Write(color[x, y].G.ToString("X2")); steamWriter.Write(color[x, y].B.ToString("X2") + " "); } steamWriter.WriteLine(); } steamWriter.Close(); } } } 
  • 3
    Look in the direction of Bitmap.LockBits . - VladD
  • one
    What objects matrix do you want to get? - user227049
  • actually from the matrix image. in the picture you can see the result of converting the image into a similarity of the matrix, but back something does not stick with the code. - asex
  • This code is not mine, I googled it, took everything ready, unfortunately, those who multiplied it on the network can be seen dragged from each other by reproducing not the current code but also errors. I could not find the original source probably some kind of books from the early years. Here are the links: deadlock.org.ua/kit/habr/post/21221 or all-best-tips.com/52930-how-to-convert-rgb-images-to-text.html or learn-myself.com/convert -rgb-images-to-text-8319 - asex

1 answer 1

Unfortunately, the reference to DocD I didn’t open for some reason, but here’s the working code for converting BMP into a color matrix like this:

 public Color[][] GetBitMapColorMatrix(string bitmapFilePath) { bitmapFilePath = @"C:\Filename.bmp"; Bitmap b1 = new Bitmap(bitmapFilePath); int hight = b1.Height; int width = b1.Width; Color[][] colorMatrix = new Color[width][]; for (int i = 0; i < width; i++) { colorMatrix[i] = new Color[hight]; for (int j = 0; j < hight; j++) { colorMatrix[i][j] = b1.GetPixel(i, j); } } return colorMatrix; } 

Accordingly, you will have a picture as a two-dimensional array of the Color type, which has a Color.ToArgb() method, which returns a value of type int and the 'ToString ()' method, which returns a string representation.

So with the help of these methods it is quite possible to present a picture in the form of a text file, writing in it the string or integer values ​​of the matrix.

Here is an example of a modified method for saving to files via ToString() and via ToArgb() respectively:

  private void button1_Click(object sender, EventArgs e) { //ОбъявляСм ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ для формирования строки ΠΏΠ΅Ρ€Π²ΠΎΠΉ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ Π² тСкстовом прСдставлСнии (значСния ΠΏΠΎ оси Π₯ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ) string FileLine1 = string.Empty; //ОбъявляСм список строк, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅ΠΌ построчно Π΄ΠΎΠ±Π°Π²Π»ΡΡ‚ΡŒ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρƒ Π² тСкстовом Π²ΠΈΠ΄Π΅ List<string> file1 = new List<string>(); //ОбъявляСм ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ для формирования строки ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹, Π² ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ Π±ΡƒΠ΄Π΅ΠΌ ΠΏΠΈΡΠ°Ρ‚ΡŒ значСния Ρ†Π²Π΅Ρ‚ΠΎΠ² Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ ARGB string FileLine2 = string.Empty; //Бписок строк для формирования Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° List<string> file2 = new List<string>(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { //Если ΠΌΡ‹ Ρ‡Π΅Ρ€Π΅Π· Π΄ΠΈΠ°Π»ΠΎΠ³ Π²Ρ‹Π±Ρ€Π°Π»ΠΈ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ, Ρ‚ΠΎ Π³Ρ€ΡƒΠ·ΠΈΠΌ Π΅Π΅ Π² Π±ΠΈΡ‚ΠΌΠ°ΠΏ. Bitmap b1 = new Bitmap(openFileDialog1.FileName); //ОбъявляСм ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ для Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ высоты ΠΈ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ (ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ)... //...ΠΈ Ρ‚ΡƒΡ‚ ΠΆΠ΅ Π·Π°Π΄Π°Π΅ΠΌ значСния этих ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… взяв ΠΈΡ… ΠΈΠ· высоты ΠΈ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Π² пиксСлях int height = b1.Height; //Π­Ρ‚ΠΎ высота ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ, ΠΈ наша ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Π° ΠΏΠΎ Π²Π΅Ρ€Ρ‚ΠΈΠΊΠ°Π»ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ ΡΠΎΡΡ‚ΠΎΡΡ‚ΡŒ ΠΈΠ· Ρ‚ΠΎΡ‡Π½ΠΎ Ρ‚Π°ΠΊΠΎΠ³ΠΎ ΠΆΠ΅ числа элСмСнтов. int width = b1.Width; //Π­Ρ‚ΠΎ ΡˆΠΈΡ€ΠΈΠ½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ, Ρ‚.Π΅. число элСмСнтов ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ ΠΏΠΎ Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΠΈ //Π’ΡƒΡ‚ ΠΌΡ‹ объявляСм саму ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρƒ Π² Π²ΠΈΠ΄Π΅ Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива, Color[,] colorMatrix = new Color[width,height]; //ЗапускаСм Ρ†ΠΈΠΊΠ», Π³Π΄Π΅ ΠΈΡ‚Π΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ Π±ΡƒΠ΄Π΅Ρ‚ цСлочислСнная пСрСмСнная i, равная ΠΏΡ€ΠΈ Π½Π°Ρ‡Π°Π»Π΅ Ρ†ΠΈΠΊΠ»Π° Π½ΡƒΠ»ΡŽ. //Π¦ΠΈΠΊΠ» Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½ΡΡ‚ΡŒΡΡ ΠΎΡ‚ 0 ΠΈ Π΄ΠΎ Ρ‚Π΅Ρ… ΠΏΠΎΡ€, ΠΏΠΎΠΊΠ° y мСньшС height (высоты ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ ΠΈ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ) //На ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ y Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ. for (int y = 0; y < height; y++) { //Π’ Π½Π°Ρ‡Π°Π»Π΅ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΌΡ‹ обнуляСм ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ для формирования строк для Ρ„Π°ΠΉΠ»ΠΎΠ² FileLine1 = string.Empty; FileLine2 = string.Empty; //А Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ сканируСм Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΡŒΠ½Ρ‹Π΅ строки ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹: for (int x = 0; x < width; x++) { //Π’ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρƒ добавляСм Ρ†Π²Π΅Ρ‚ Ρ‚ΠΎΡ‡ΠΊΠΈ с ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌΠΈ x,y ΠΈΠ· ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ b1. colorMatrix[x,y] = b1.GetPixel(x, y); //А Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅ΠΌ Ρ†Π²Π΅Ρ‚ Ρ‚ΠΎΡ‡ΠΊΠΈ (x,y) Π²: //1. ВСкстовоС прСдставлСниС: FileLine1 += colorMatrix[x,y].ToString() + " "; //2. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ†Π²Π΅Ρ‚Π° Π² цСлочислСнном Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅: FileLine2 += colorMatrix[x,y].ToArgb().ToString() + " "; //Π’ΡƒΡ‚ Π² ΠΎΠ±ΠΎΠΈΡ… случаях Π² ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠΌ ΠΈΡ‚ΠΎΠ³Π΅ строки ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΡŽΡ‚ΡΡ ΠΊ Ρ‚ΠΈΠΏΡƒ string для удобства ΠΈΡ… сохранСния Π² тСкстовом Ρ„Π°ΠΉΠ»Π΅ } //А Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π² списки добавляСм ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ΠΈΠ· ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Ρ… строк: //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Π² тСкстовом Π²ΠΈΠ΄Π΅: file1.Add(FileLine1); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Π² Π²ΠΈΠ΄Π΅ значСния Ρ†Π²Π΅Ρ‚ΠΎΠ² пиксСлСй file2.Add(FileLine2); } //ЗаписываСм ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² тСкстовыС Ρ„Π°ΠΉΠ»Ρ‹: File.WriteAllLines("D:\\pic1.txt", file1); File.WriteAllLines("D:\\pic2.txt", file2); } } 

Personally, the variant with preserving integer values ​​seemed more convenient to me. Therefore, the reverse process was written for him. I wrote quite quickly and "in the forehead," I am sure, it was possible to do more optimally:

 private void button2_Click(object sender, EventArgs e) { List<string> file = new List<string>(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { file = File.ReadAllLines(openFileDialog1.FileName).ToList(); if (file.Count() < 1) return; //Π€Π°ΠΉΠ» пустой! var width = file[0].Split(' ').Count() - 1; //Π’ ΠΊΠΎΠ½Ρ†Π΅ строки Ρƒ нас Π΅ΡΡ‚ΡŒ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΡ€ΠΎΠ±Π΅Π»! var heigh = file.Count; Bitmap b2 = new Bitmap(width, heigh); string[] s; for (var y = 0; y < heigh; y++) { s = file[y].TrimEnd(' ').Split(' '); for (var x = 0; x < width; x++) { var i = int.Parse(s[x]); Color pixel = new Color(); pixel = Color.FromArgb(i); b2.SetPixel(x, y, pixel); } } } } 
  • It remains only to translate unsafe , and even working with pixels from managed code is not a quick deal. - Sergey
  • Pixel-by-pixel work with pictures is in any case not a quick deal, and even when pixels are represented as lines (text file), not numbers (bytes). - BlackWitcher
  • one
    @ Sergey, unsafe is not needed, but you need to get rid of GetPixel . - Qwertiy ♦
  • Something is too much code ... - Qwertiy ♦
  • For clarity and clarity, and even hurry before the end of the working day, there is always a way to refactor)) - BlackWitcher