-2

I am trying to convert a part of a .txt file to TIFF image, as

public static void ReadTextFileLineByLine(string fileName)//fileName- "c:\\test.txt"
{
        int counter = 0;
        string line;
        List<string> linelist = new List<string>();            
        System.IO.StreamReader file =  new System.IO.StreamReader(strFileName);

        while ((line = file.ReadLine()) != null && counter!=19)
        {
            linelist.Add(line);
            counter++;
        }
        file.Close();
        ConvertlistToByteArrayToTiff(linelist);
    }

    public static void ConvertlistToByteArrayToTiff(List<string> list)
    {         
        byte[] dataAsBytes = list
          .SelectMany(s => System.Text.Encoding.ASCII.GetBytes(s))
          .ToArray();

        MemoryStream ms = new MemoryStream(dataAsBytes);
        Image returnImage = Image.FromStream(ms);
        returnImage.Save("c:\\133.tiff", System.Drawing.Imaging.ImageFormat.Tiff);        
    }

I am getting error at Image.FromStream(ms);:

Parameter is not valid.

Is my approach correct or I need to do it in different way?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Kumar Gaurav
  • 899
  • 5
  • 19
  • 35
  • Not sure if this helps, but MSDN says the method will throw an `ArgumentException` if "The stream does not have a valid image format". Do you get that in your code? – shree.pat18 Jul 30 '14 at 07:37
  • I am passing memorystream that i am getting from byte array, and in above code you can see, byte array is from string list that I am reading from plain text file - line by line. Do you have any other approach to achive the goal - Text file to TIFF image? – Kumar Gaurav Jul 30 '14 at 07:43
  • can you provide example of this "text file"? – rufanov Jul 30 '14 at 07:47
  • Text file sample: A new welcome to Yahoo. The new Yahoo experience makes it easier to discover the news and information that you care about most. It's the web ordered for you – Kumar Gaurav Jul 30 '14 at 07:51

1 Answers1

1

Is my approach correct

No

or I need to do it in different way?

Yes

You can "print"/draw the text to an image. Like shown here: https://stackoverflow.com/a/6311628/2655508

Your assumption, that a tiffs (or any other image) representation of text, is just the ascii value of the chars isn't correct.

To read about Tiff: http://en.wikipedia.org/wiki/Tagged_Image_File_Format

To understand the structure of Tiff: See RFC 2306

Community
  • 1
  • 1
Heslacher
  • 2,167
  • 22
  • 37