如何从文本文件中的流中分配图像值

huangapple go评论97阅读模式
英文:

How to assign an image value from a stream from a text file

问题

以下是您要翻译的部分:

"我正在尝试在一个项目中实现一个功能,允许用户上传他们自己的图像文件作为头像。我从图像中获取流并将其保存为 jpg 格式的文本文件,与用户的用户名一起保存在同一行。但是,当我尝试将图片框的图像设置为这个流时,我一直收到“参数无效”的错误消息。有没有人知道我做错了什么?

保存流到文本文件的代码:

  1. customavatar = Image.FromFile(filename);
  2. var stream = new System.IO.MemoryStream();
  3. customavatar.Save(stream, ImageFormat.Jpeg);
  4. stream.Position = 0;
  5. if (!File.Exists(textfile))
  6. {
  7. File.Create(textfile);
  8. }
  9. using (StreamWriter sw = File.AppendText(textfile))
  10. {
  11. sw.WriteLine("");
  12. sw.WriteLine($"{username},{stream}");
  13. }

设置图片框图像的代码,出现错误的地方:

  1. string[] filelinesarray = File.ReadAllLines(textfile);
  2. foreach (string str in filelinesarray)
  3. {
  4. if (str.Split(",")[0] == username)
  5. {
  6. byte[] byteArray = Encoding.ASCII.GetBytes(str.Split(",")[1]);
  7. MemoryStream stream = new MemoryStream(byteArray);
  8. Image img = Image.FromStream(stream);
  9. picturebox.Image = img;
  10. }
  11. }

希望这可以帮助您解决问题。

英文:

I'm trying to implement a feature in a project where the user can upload their own image files to be used as an avatar, I get the stream from the image as jpg format and save it in a text file on the same line with the user's username where it's retrieved but I keep getting the error message "Parameter is not valid" when I try to set the image of a picturebox to the stream, does anyone know what I'm doing wrong?

The code that saves the stream to the text file;

  1. customavatar = Image.FromFile(filename);
  2. var stream = new System.IO.MemoryStream();
  3. customavatar.Save(stream, ImageFormat.Jpeg);
  4. stream.Position = 0;
  5. if(!File.Exists( textfile))
  6. {
  7. File.Create( textfile);
  8. }
  9. using (StreamWriter sw = File.AppendText( textfile))
  10. {
  11. sw.WriteLine("");
  12. sw.WriteLine($"{username},{stream}");
  13. }

The code setting the image of a picturebox to the stream, where the error appears;

  1. string[] filelinesarray = File.ReadAllLines(text file);
  2. foreach (string str in filelinesarray)
  3. {
  4. if (str.Split(",")[0] == username)
  5. {
  6. byte[] byteArray = Encoding.ASCII.GetBytes(str.Split(",")[1]);
  7. MemoryStream stream = new MemoryStream(byteArray);
  8. Image img = Image.FromStream(stream);
  9. picturebox.Image = img;
  10. }
  11. }

答案1

得分: 1

根据Fildor的建议,使用base64对图像进行编码。

我使用winform(.Net Framework 4.8)进行了测试,并修改了您的代码。

我自定义了文本文件的保存地址和用户名的名称,并使用openfiledialog来选择需要上传的图像地址。

以下是我的演示:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. var openFileDialog = new OpenFileDialog();
  4. openFileDialog.Filter = "All Files (*.*)|*.*";
  5. openFileDialog.Title = "Select a file";
  6. if (openFileDialog.ShowDialog() == DialogResult.OK)
  7. {
  8. string filename = openFileDialog.FileName;
  9. var stream = new System.IO.MemoryStream();
  10. using (var customavatar = Image.FromFile(filename))
  11. {
  12. customavatar.Save(stream, ImageFormat.Jpeg);
  13. }
  14. var base64String = Convert.ToBase64String(stream.ToArray());
  15. // 创建文件,如果文件存在则覆盖。
  16. using (var fstream = new FileStream(textfile, FileMode.Create, FileAccess.Write, FileShare.None))
  17. {
  18. using (var writer = new StreamWriter(fstream))
  19. {
  20. writer.Write($"{username},{base64String}");
  21. }
  22. }
  23. }
  24. }
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. string[] filelinesarray = File.ReadAllLines(textfile);
  4. foreach (string str in filelinesarray)
  5. {
  6. if (str.Split(',')[0] == username)
  7. {
  8. byte[] byteArray = Convert.FromBase64String(str.Split(',')[1]);
  9. MemoryStream stream = new MemoryStream(byteArray);
  10. Image img = Image.FromStream(stream);
  11. pictureBox1.Image = img;
  12. }
  13. }
  14. }

如何从文本文件中的流中分配图像值

英文:

As Fildor said, use base 64 to encode the image.

I tested with winform (.Net Framework4.8) and modified your code.

I customized the save address of a txt file and the name of a username. And use openfiledialog to select the image address that needs to be uploaded.

The following is my demo:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. var openFileDialog = new OpenFileDialog();
  4. openFileDialog.Filter = "All Files (*.*)|*.*";
  5. openFileDialog.Title = "Select a file";
  6. if (openFileDialog.ShowDialog() == DialogResult.OK)
  7. {
  8. string filename = openFileDialog.FileName;
  9. var stream = new System.IO.MemoryStream();
  10. using (var customavatar = Image.FromFile(filename))
  11. {
  12. customavatar.Save(stream, ImageFormat.Jpeg);
  13. }
  14. var base64String = Convert.ToBase64String(stream.ToArray());
  15. // Create the file, or overwrite if the file exists.
  16. using (var fstream = new FileStream(textfile, FileMode.Create, FileAccess.Write, FileShare.None))
  17. {
  18. using (var writer = new StreamWriter(fstream))
  19. {
  20. writer.Write($"{username},{base64String}");
  21. }
  22. }
  23. }
  24. }
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. string[] filelinesarray = File.ReadAllLines(textfile);
  4. foreach (string str in filelinesarray)
  5. {
  6. if (str.Split(',')[0] == username)
  7. {
  8. byte[] byteArray = Convert.FromBase64String(str.Split(',')[1]);
  9. MemoryStream stream = new MemoryStream(byteArray);
  10. Image img = Image.FromStream(stream);
  11. pictureBox1.Image = img;
  12. }
  13. }
  14. }

如何从文本文件中的流中分配图像值

huangapple
  • 本文由 发表于 2023年2月27日 02:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定