好的,是时候寻求更多的 n00b 帮助了。
我一直在用 C# 编写这段代码(确切地说是单声道)。它旨在拍摄图像,逐像素分解,并将 R、G 和 B 值作为颜色坐标写入文本文件。
问题是,我没有得到任何处理。我只是得到一个红色的 0(for() 循环正上方的百分比函数和一个空文件。没有生成文本/坐标。为了清楚起见,我将在代码本身下方发布控制台输出。
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Image_Interpreter_I
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Enter filepath for bitmap image");
Console.ForegroundColor = ConsoleColor.Blue;
string filepath = Console.ReadLine ();
Console.ResetColor();
Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
Console.WriteLine ("Enter filepath for file interpretation output");
Console.ForegroundColor = ConsoleColor.Blue;
string filepath2 = Console.ReadLine();
Console.ResetColor();
int i;
i = 1;
int ImW, ImH;
string ImWstr, ImHstr;
Console.WriteLine ("Enter width of image:");
Console.ForegroundColor = ConsoleColor.Blue;
ImWstr = Console.ReadLine ();
Console.ResetColor ();
ImW = Convert.ToInt16 (ImWstr);
Console.WriteLine ("Enter height of image:");
Console.ForegroundColor = ConsoleColor.Blue;
ImHstr = Console.ReadLine ();
ImH = Convert.ToInt16 (ImHstr);
decimal percent;
percent = (i / (ImH * ImW)) * 100;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine (percent);
Console.ResetColor ();
for (int y = 1; y <= ImH; y++)
{
for (int x = 1; x <= ImW; x++)
{
Color pixelColor = image1.GetPixel(x,y);
byte r = pixelColor.R;
byte g = pixelColor.G;
byte b = pixelColor.B;
string pixData = String.Format ("({0},{1},{2}", new object[] {r, g, b});
using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
{
file.WriteLine(pixData);
}
}
}
i++;
}
}
}
控制台输出:
输入位图图像的文件路径
/Users/user0/Desktop/IMG_1528.JPG
输入文件解释输出的文件路径
/Users/user0/Desktop/imageData.txt
输入图像宽度:
2448 输入图像高度:3264 0