ecfedele
ecfedele

Reputation: 316

Image processing code help - C#

Alright y'all, time for more n00b help.
I've been working on this code in C# (Mono, to be exact). It is designed to take an image, break it down pixel by pixel, and write R,G, and B values as color coordinates to a text file.
Problem is, I don't get any processing. I simply get a red 0 (the percent function right above the for() loops, and an empty file. No text/coordinates generated. I'll post the console output below the code itself for clarity.

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++;    
}
 }
 }

Console Output:

Enter filepath for bitmap image
/Users/user0/Desktop/IMG_1528.JPG
Enter filepath for file interpretation output
/Users/user0/Desktop/imageData.txt
Enter width of image:
2448 Enter height of image: 3264 0

Upvotes: 0

Views: 2056

Answers (3)

SilentDoc
SilentDoc

Reputation: 357

I would try to open the file outside the loop:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
{

   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});
          file.WriteLine(pixData);
       }
    }
}

This should help :)

Upvotes: 0

Jarek
Jarek

Reputation: 3379

First thing: you are creating new stream writter on each loop entry - that will each time overwrite existing file and also will be slow. Create this stream first, and then do the processing.

using (var file = new System.IO.StreamWriter(filepath2))
{
    // for loops
}

Second, according to documentation: Bitmap.GetPixel pixels are indexed for 0 to Width-1/Height-1 - which gives incorrect lvaues in your for loops. Start from 0 and change loop end condition to y < ImH and x < ImW.

The other thing - try format code better for future, extract methods for easier understanding and reading the code.

Upvotes: 2

lex87
lex87

Reputation: 1326

        static void Main(string[] args)
    {
        string filepath = @"C:\SomePath\SomeFile.JPG";
        Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
        string filepath2 = @"C:\temp\myFile.txt";

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
        {
            for (int y = 0; y < image1.Height; y++)
            {
                for (int x = 0; x < image1.Width; 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 });

                    file.WriteLine(pixData);
                }
            }
        }
    }

Upvotes: 0

Related Questions