Abhendra Tiwari
Abhendra Tiwari

Reputation: 19

Convert String into image and save a txt file of string like as in Image

In winform application, I am converting a string into Image and want to save a txt file. In which (txt file) text is formatted like as image.

This is code to convert string into image=>

private void DrawText(string text)
{
    textBox2.SelectAll();

    FontColor = Color.Black;
    FontBackColor =Color.White;

    FontName = "LinoScript";// richTextBox1.SelectionFont.Name;
    FontSize = 24; //Convert.ToInt16(richTextBox1.SelectionFont.Size);
    ImageHeight = 2630;
    ImageWidth = 1599;
    ImagePath = textBox1.Text.Trim() + numericUpDown1.Value.ToString()+".JPEG";

    //  ImagePath = @"D:\Test.JPEG"; //give the file name you want to export to as image
    ImageText = new Bitmap(ImageWidth, ImageHeight);
    ImageText.SetResolution(90,100);
    ImageGraphics = Graphics.FromImage(ImageText);
    MarginsBox m = new MarginsBox();
    //   printmap.SetResolution(dpi, dpi); // Set the resolution of our paper
    m.Top = 1 * 95; // Set a 1' margin, from the top
    m.Left = 1.25f * 95; // Set a 1.25' margin, from the left
    m.Bottom = ImageText.Height - m.Top; // 1', from the bottom
    m.Right = ImageText.Width - m.Left; // 1.25', from the right
    m.Width = ImageText.Width - (m.Left * 2); // Get the width of our working area
    m.Height = ImageText.Height - (m.Top * 2); // Get the height of our working area

    ImageFont = new Font(FontName, FontSize);

    ImagePointF = new PointF(5, 5);
    BrushForeColor = new SolidBrush(FontColor);
    BrushBackColor = new SolidBrush(Color.White);
    StringFormat drawFormat = new StringFormat();
    ImageGraphics.FillRectangle(BrushBackColor, 0, 0, ImageWidth,ImageHeight);
    //ImageGraphics.DrawString(text, ImageFont, BrushForeColor, ImagePointF);

    ImageGraphics.DrawString(text, ImageFont, BrushForeColor, new RectangleF(m.Left, m.Top, m.Width, m.Height),drawFormat);
    SaveMyFile(text);
    //Draw a byte and create image file
    string outputFileName = ImagePath;
    using (MemoryStream memory = new MemoryStream())
    {
        using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
        {
            ImageText.Save(memory,  ImageFormat.Gif);
            byte[] bytes = memory.ToArray();
            fs.Write(bytes, 0, bytes.Length);
        }
    }
}

I am creating text file like this:

public void SaveMyFile(string datastring)
{
    StringFormat drawFormat = new StringFormat();
    TextWriter writer = new StreamWriter(@textBox1.Text.Trim() + numericUpDown1.Value.ToString() + "image.txt");
    writer.Write(datastring, drawFormat);
    writer.Close();  
}

I want .txt file, In which Text Format should be like as Generated Image.

This is image, which is I am getting after code execution.

I want a .txt file In which text should be like as line by line as in Image.

Thanks in advance

Upvotes: 0

Views: 531

Answers (1)

gunnerone
gunnerone

Reputation: 3572

As I mentioned in my comment, you can't set the font, font size, or margins in a text file. What can and can't be done in a txt file: https://www.computerhope.com/issues/ch001872.htm You could do this with a rich text file (.rtf) like so. I used the "DotNetRtfWriter" nuget package.

using HooverUnlimited.DotNetRtfWriter;
...

public void SaveMyFile(string datastring)
{
    RtfDocument doc = new RtfDocument(
        HooverUnlimited.DotNetRtfWriter.PaperSize.Letter,
        PaperOrientation.Portrait,
        Lcid.English);

    doc.Margins[Direction.Top] = 1 * 72;
    doc.Margins[Direction.Left] = 1.25f * 72;
    doc.Margins[Direction.Bottom] = 1 * 72;
    doc.Margins[Direction.Right] = 1.25f * 72;

    doc.SetDefaultFont("LinoScript");
    RtfParagraph para = doc.AddParagraph();
    RtfCharFormat format = para.AddCharFormat();
    format.FontSize = 24;
    para.SetText(datastring);
    doc.Save(@textBox1.Text.Trim() + numericUpDown1.Value.ToString() + "image.rtf");
}

Upvotes: 1

Related Questions