Permabanned
Google and stackoverflow.com have the answers to 99% of programming queries.
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
namespace ImageProcessor
{
class Program
{
private static string SourceFolder = @"C:\TempPath";
private class PixelData
{
public int x { get; set; }
public int y { get; set; }
public Color pixel { get; set; }
}
static void Main(string[] args)
{
ParseFolder();
}
static void ParseFolder()
{
DirectoryInfo d = new DirectoryInfo(SourceFolder);
FileInfo[] Files = d.GetFiles("*.png");
double distance;
int row = 1;
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Parsed Images");
foreach (FileInfo file in Files)
{
distance = ParseImage(file.Name);
worksheet.Cell($"A{row}").Value = file.Name;
worksheet.Cell($"B{row}").Value = distance;
row++;
}
workbook.SaveAs($"{SourceFolder}\\ParsedImages.xlsx");
}
}
static double ParseImage(string fileName)
{
double distance;
List<PixelData> data = new List<PixelData>();
Bitmap img = new Bitmap($"{SourceFolder}\\{fileName}");
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
Color pixel = img.GetPixel(x, y);
var newDataPoint = new PixelData
{
x = x,
y = y,
pixel = pixel
};
data.Add(newDataPoint);
}
}
var allWhitePixels = data.Where(d => d.pixel.A == 255 && d.pixel.B == 255 && d.pixel.G == 255).OrderBy(d => d.x);
if (allWhitePixels.Count() > 0)
{
Point left = new Point(allWhitePixels.FirstOrDefault().x, allWhitePixels.FirstOrDefault().y);
Point right = new Point(allWhitePixels.LastOrDefault().x, allWhitePixels.LastOrDefault().y);
distance = Math.Sqrt(Math.Pow(right.X - left.X, 2) + Math.Pow(right.Y - left.Y, 2));
Console.WriteLine($"Parsing:{fileName}:distance:{distance}");
}
else
{
Console.WriteLine($"Parsing Failed:{fileName}");
distance = 0;
}
return distance;
}
}
}
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
namespace ImageProcessor
{
class Program
{
private static string SourceFolder = @"C:\TempPath";
private class PixelData
{
public int x { get; set; }
public int y { get; set; }
}
static void Main(string[] args)
{
ParseFolder();
}
static void ParseFolder()
{
DirectoryInfo d = new DirectoryInfo(SourceFolder);
FileInfo[] Files = d.GetFiles("*.png");
double distance;
int row = 1;
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Parsed Images");
foreach (FileInfo file in Files)
{
distance = ParseImage(file.Name);
worksheet.Cell($"A{row}").Value = file.Name;
worksheet.Cell($"B{row}").Value = distance;
row++;
}
workbook.SaveAs($"{SourceFolder}\\ParsedImages.xlsx");
}
}
static double ParseImage(string fileName)
{
double distance;
List<PixelData> data = new List<PixelData>();
Bitmap img = new Bitmap($"{SourceFolder}\\{fileName}");
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
Color pixel = img.GetPixel(x, y);
if(pixel.A == 255 && pixel.B == 255 && pixel.G == 255)
{
var newDataPoint = new PixelData
{
x = x,
y = y
};
data.Add(newDataPoint);
}
}
}
var allWhitePixels = data.OrderBy(d => d.x);
if (allWhitePixels.Count() > 0)
{
Point left = new Point(allWhitePixels.FirstOrDefault().x, allWhitePixels.FirstOrDefault().y);
Point right = new Point(allWhitePixels.LastOrDefault().x, allWhitePixels.LastOrDefault().y);
distance = Math.Sqrt(Math.Pow(right.X - left.X, 2) + Math.Pow(right.Y - left.Y, 2));
Console.WriteLine($"Parsing:{fileName}:distance:{distance}");
}
else
{
Console.WriteLine($"Parsing Failed:{fileName}");
distance = 0;
}
return distance;
}
}
}
Yeah I left them in in case I wanted to use them but didnt in the end.
Could use a load more images to actually test properly but I expect you could filter out any extreme anomalies in excel anyway.
6vuZR6T.png 0
N1IZXW9.png 38.20995
Wnb2ALb.png 114.3547
a9DYf83.png 572.0428
Is spot on from those 4 samples at least.
Error CS0246 The type or namespace name 'Color' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp8 C:\Users\wmose\source\repos\ConsoleApp8\Program.cs 18 N/A
system.drawing; is still at the top but it's greyed out along with system.linq; and system;