C# 将字符串转换为图像,并保存一个类似于图像的字符串的txt文件
在winform应用程序中,我正在将字符串转换为图像,并希望保存一个txt文件。其中(txt文件)文本的格式类似于图像 这是将字符串转换为图像=>C# 将字符串转换为图像,并保存一个类似于图像的字符串的txt文件,c#,asp.net,forms,winforms,C#,Asp.net,Forms,Winforms,在winform应用程序中,我正在将字符串转换为图像,并希望保存一个txt文件。其中(txt文件)文本的格式类似于图像 这是将字符串转换为图像=> private void DrawText(string text) { textBox2.SelectAll(); FontColor = Color.Black; FontBackColor =Color.White; FontName = "LinoScript";// richTextBox1.Select
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文件,其中的文本格式应该像生成的图像一样
我想要一个.txt文件,其中的文本应该像在图像中一样逐行显示
提前感谢正如我在评论中提到的,您不能在文本文件中设置字体、字号或边距。 在txt文件中可以做什么和不能做什么:您可以使用类似的富文本文件(.rtf)来做这件事。我使用了“DotNetRtfWriter”nuget包
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");
}
你的问题是什么?你被困在哪里?请阅读,因为您似乎要求有人编写您的代码-这在本网站上是离题的。@nilsk我想要一个文本文件,其中的文本应该像在图像中一样逐行排列……您的意思是希望该文本文件具有与图像中相同的字体、字号和边距吗?如果是这样,这在txt文件中是不可能的。无法设置txt文件的格式。您可以使用富文本文件(.rtf)或类似的文件来实现这一点。在txt文件中可以和不能执行的操作:@gunnerone..如果我将文件格式更改为.rtf,那么这是否可能…如果可能,请告诉我..如何操作???我得到**System.BadImageFormatException:'无法加载文件或程序集'DotNetRtfWriter,Version=2.0.0.3,Culture=neutral,PublicKeyToken=null'或其依赖项之一。试图加载格式不正确的程序**此错误。看起来,
DotNetRtfWriter
包仅为x86。因此,您需要将项目的平台类型更改为x86(而不是任何CPU或x64)。如果您需要以x64为目标,我可能会找到另一个包来代替。