namespace txtToImg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string fileContent = File.ReadAllText("D:\pixels.txt");
string[] integerStrings = fileContent.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int[] integers = new int[integerStrings.Length];
for (int n = 0; n < integerStrings.Length; n++)
{
integers[n] = int.Parse(integerStrings[n]);
}
Bitmap my = new Bitmap(512, 512);
for (int i = 0; i < 512; i++)
for (int j = 0; j < 512; j++)
my.SetPixel(i, j, Color.Blue);
my.Save("D:\my.jpg");
}
}
}
Instead of setting all the pixels to Blue as I've done, I want to use the values from the array.
This is how I save the pixels to a text file! They are integers from 0 to 255. Now I'm trying to deal with greyscale images so I don't need the R, G and B separately, that's why it's (R+G+B)/3.
using (Bitmap bitmap = new Bitmap("D:\6.jpg"))
{
int width = 512;
int height = 512;
TextWriter tw = new StreamWriter("D:\pixels.txt");
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Color color = bitmap.GetPixel(j, i);
tw.Write((color.R + color.G + color.B) / 3 + " ");
}
//tw.Write(" ");
}
tw.Close();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…