0

Faced such a problem "System.ArgumentException", while writing an envelope from an image (.jpg) in a text file with a picture (ASCII). Did according to the instructions (https://www.bilibili.com/video/av5862027/)

For the second or third day I try to solve this problem.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;

namespace ayy
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream stream = new FileStream(@"meme.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream);

            for (int imageNumber = 0; imageNumber <= 7600; imageNumber++)
            {
                string url = @"C:\Users\Admin\source\repos\badapple\ayy\ba\ba";
                if (imageNumber < 10)
                {
                    url += "00000";
                }
                else if (imageNumber < 100)
                {
                    url += "0000";
                }
                else if (imageNumber < 1000)
                {
                    url += "000";
                }
                else
                {
                    url += "00";
                }
                url += imageNumber.ToString() + ".jpg";
                Bitmap image = new Bitmap(url, true);

                for (int y = 0; y < image.Height; y++)
                {
                    string str = "";
                    for (int x = 0; x < image.Width; x++)
                    {
                        Color pixel = image.GetPixel(x, y);
                        if (pixel.R > 200)
                        {
                            str += "#";
                        }
                        else
                        {
                            str += " ";
                        }
                        writer.WriteLine(str);
                    }
                    Console.WriteLine(url);
                }
                writer.Close();
            }
        }
    }
}
6
  • 2
    The exact location and message of the exception (ie. the full exception trace) should make the problem (or at least where it occurs) 'relatively obvious'; this question contains neither. Commented Oct 19, 2018 at 5:07
  • 2
    Also, calling Bitmap image = new Bitmap(url, true); with disposing or using the using statement is a one way street to an OutOfMemoryException Commented Oct 19, 2018 at 5:11
  • 1
    What exactly is it you are trying to do Commented Oct 19, 2018 at 5:15
  • Make ascii image Commented Oct 19, 2018 at 5:18
  • Have you tried putting the Boolean parameter as false. I have done a little research and it suggests it does some processing on the file. It is possibly not required. Commented Oct 19, 2018 at 6:25

1 Answer 1

1

using, using, using

  • Every time you go to play with something (or create an object) check if you can use a using statement

  • If you expect a file to be there, do some due diligence and check if it exists

  • If you want to join paths and file names, use Path.Combine()

  • GetPixel is extremely slow, so probably better to use LockBits

  • Why use lots of ifs to add 0's when you can use a format specifier $"{imageNumber:D5}.jpg"

Truthfully, i am not sure if this will fix your problem, but you are much better place regardless

using (var stream = new FileStream(@"meme.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
   using (var writer = new StreamWriter(stream))
   {
      for (var imageNumber = 0; imageNumber <= 7600; imageNumber++)
      {
         var dir = @"C:\Users\Admin\source\repos\badapple\ayy\ba\ba";

         var fileName = Path.Combine(dir, $"{imageNumber:D5}.jpg");

         if (File.Exists(fileName))
         {
            throw new FileNotFoundException($"Woah, what now : {fileName}");
         }

         using (var image = new Bitmap(fileName, true))
         {
            for (var y = 0; y < image.Height; y++)
            {
               for (var x = 0; x < image.Width; x++)
               {
                  var pixel = image.GetPixel(x, y);
                  writer.Write(pixel.R > 200 ? "#" : " ");
               }

               writer.WriteLine();
            }
         }
      }
   }
}

If you are still having problems, work out what file is causing the problem, check to see if it is actually an image and loads. My spidey senses tells me its not

Sign up to request clarification or add additional context in comments.

6 Comments

This error still comes out, but thanks for the help, I will try to solve the problem
Is the file you loading actually an image?
I am trying to convert several images into one text file in order to create an animation in the console in another project
@H0d3ine that didnt answer the question, when it fails, it it trying to load a real image? have you double checked this
it does not load anything
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.