将String转换成image并保存一个字符串的txt文件,就像在Image中一样

时间:2019-01-18 15:42:23

标签: c# asp.net forms winforms

在winform应用程序中,我正在将字符串转换为Image,并希望保存一个txt文件。其中(txt文件)文本的格式类似于图像。

这是将字符串转换为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);
        }
    }
}

我正在创建这样的文本文件:

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();  
}

我想要.txt文件,其文本格式应类似于“生成的图像”。

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

我想要一个.txt文件,其中的文本应与图像中的一行一行一样。

预先感谢

1 个答案:

答案 0 :(得分:0)

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");
}