-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
102 lines (85 loc) · 3.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
namespace ImageProcessingSystem
{
class Program
{
static void Main(string[] args)
{
string inputDirectory = "images";
string outputDirectory = "processed_images";
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
string[] imageFiles = Directory.GetFiles(inputDirectory, "*.jpg");
// Crear un array de hilos
Thread[] threads = new Thread[imageFiles.Length];
// Crear un Stopwatch para medir el tiempo de ejecución
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Iniciar cada hilo para procesar una imagen
for (int i = 0; i < imageFiles.Length; i++)
{
string imagePath = imageFiles[i];
threads[i] = new Thread(() => ProcessImage(imagePath, outputDirectory));
threads[i].Start();
}
// Esperar a que todos los hilos terminen
foreach (Thread thread in threads)
{
thread.Join();
}
// Detener el Stopwatch y mostrar el tiempo transcurrido
stopwatch.Stop();
Console.WriteLine($"Todas las imágenes se han procesado en {stopwatch.Elapsed.TotalSeconds} segundos.");
}
public static void ProcessImage(string imagePath, string outputDirectory)
{
try
{
using (var originalImage = new Bitmap(imagePath))
{
var scaledImage = ScaleImage(originalImage, 0.5);
var grayscaleImage = ConvertToGrayscale(scaledImage);
string outputFilePath = Path.Combine(outputDirectory, Path.GetFileName(imagePath));
grayscaleImage.Save(outputFilePath);
Console.WriteLine($"Imagen procesada y guardada: {outputFilePath}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error al procesar la imagen {imagePath}: {ex.Message}");
}
}
public static Bitmap ScaleImage(Image image, double scale)
{
int newWidth = (int)(image.Width * scale);
int newHeight = (int)(image.Height * scale);
var scaledImage = new Bitmap(newWidth, newHeight);
using (var graphics = Graphics.FromImage(scaledImage))
{
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
return scaledImage;
}
public static Bitmap ConvertToGrayscale(Bitmap original)
{
var grayscaleImage = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++)
{
for (int x = 0; x < original.Width; x++)
{
Color originalColor = original.GetPixel(x, y);
int grayScaleValue = (int)((originalColor.R * 0.3) + (originalColor.G * 0.59) + (originalColor.B * 0.11));
Color grayColor = Color.FromArgb(grayScaleValue, grayScaleValue, grayScaleValue);
grayscaleImage.SetPixel(x, y, grayColor);
}
}
return grayscaleImage;
}
}
}