How To Convert RGB Images To Text

How To Convert RGB Images To Text
How To Convert RGB Images To Text

Video: How To Convert RGB Images To Text

Video: How To Convert RGB Images To Text
Video: Convert Image To Text Using Microsoft Word 2016 Free (Offline) | Convert Screenshot To Text 2024, November
Anonim

Each pixel of a bmp image carries information about its color from the RGB model (the color model of the image, which consists of three components R - red, G - green, B - blue). It is more convenient to store RGB color value in hex format (hexadecimal), where the value of each component is in the range 00… FF. Combination 000000 - corresponds to black, FFFFFF - white.

How to convert RGB images to text
How to convert RGB images to text

To get started, let's open the Drawing namespace:

using System. Drawing;

Let's create a new instance of the Bitmap class:

Bitmap bmp = new Bitmap ("c: / 1.bmp") / / c: / 1.bmp - image address

To store the pixel color value, you can create a separate structure, whose fields are RGB components, but I decided to use the Color class from the Drawing namespace. To get the color, we use the GetPixel (x, y) method, where x, y are the coordinates of the image pixel. If you want to use your own structure to store the color, rather than an instance of the Color class, then you can use the bmp. GetPixel (x, y).x method to get a single RGB component, where x is R, G, or B.

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);

}

To write color values to a file, we use the StreamWriter class. Since the R, G, B elements of the Color instance are of the byte type, we convert them using the ToString ("X2") method to a string type that will store the hex values of the RGB elements.

StreamWriter steamWriter = new StreamWriter ("c: / 1.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 ();

Now we will carry out the reverse operation - we will convert the resulting text file into an image.

With the help of StreamReader we read information from the file.

StreamReader txtFile = new StreamReader ("c: / 1.txt");

We calculate the width and height of the image. Since each pixel takes 6 characters and 1 gap, and there is no gap at the end, we use the following formula to calculate the width:

temp = txtFile. ReadLine ();

width = (temp. Length + 1) / 7;

The height of the image is the number of lines in the file:

while (! txtFile. EndOfStream)

{

txtFile. ReadLine ();

height ++;

}

height ++;

Move the read pointer in the file to the beginning:

txtFile. DiscardBufferedData ();

Create a new instance of the Bitmap class:

Bitmap bmp2 = new Bitmap (width, height);

Using the split method, we split the elements of the matrix. We declare three variables of the byte type - R, G, B. Using the Parse and Substring methods, select the color elements separately.

To fill the pixel in the image with this color, use the SetPixel (j, i, Color. FromArgb (R, G, B)) method, where j, i are the image coordinates, Color. FromArgb (R, G, B) is the method that creates the structure Color.

for (int i = 0; i <height; i + +)

{

temp = txtFile2. ReadLine ();

string substring = temp. Split ('');

for (int j = 0; j <width; j + +)

{

R = byte. Parse (substring [j]. Substring (0, 2), System. Globalization. NumberStyles. HexNumber)

G = byte. Parse (substring [j]. Substring (2, 2), System. Globalization. NumberStyles. HexNumber)

B = byte. Parse (substring [j]. Substring (4, 2),

System. Globalization. NumberStyles. HexNumber)

bmp2. SetPixel (j, i, Color. FromArgb (R, G, B));

}

}

Now you can save the images. If you are working in WindowsForm you can use the SaveFileDialog class:

SaveFileDialog saveImage = new SaveFileDialog ();

saveImage. Filter = bmp files (*. bmp) | *. bmp | All files (*. *)

| *. * ;

saveImage. ShowDialog ();

bmp2. Save (saveImage. FileName);

Recommended: