【问题标题】:how to convert image into string in c#? Especially in Monotouch ios [closed]如何在c#中将图像转换为字符串?特别是在 Monotouch ios [关闭]
【发布时间】:2015-02-13 19:41:12
【问题描述】:

我是 C# 新手。我正在开发一个 iOS 项目。我想将图像上传到服务器。所以我想将图像转换为字符串。谁能帮我做这件事。

我同意任何将图片上传到服务器的方法。

【问题讨论】:

标签: c# image xamarin.ios


【解决方案1】:

怎么样?

FileStream stream = new FileStream(imageFilePath, FileMode.Open);
BinaryReader binreader = new BinaryReader(stream);
byte[] buffer = new byte[(int) stream.Length];
buffer = binreader.ReadBytes((int) stream.Length);
string serialized = Convert.ToBase64String(buffer)

如果你有 System.Drawing.Image 对象而不是文件路径,你可以这样做:

System.Drawing.Image image; //initialize it someway
MemoryStream ms = new MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); //if it is jpeg
byte[] buffer = ms.ToArray();
string serialized = Convert.ToBase64String(buffer);

然后将“序列化”值传递给服务器。

Put 能否正常工作取决于服务器将如何处理。

【讨论】:

  • 我会检查这个。谢谢
  • 我遇到了人类无法阅读的内容。好的。如何将其发送到 php 服务器?
  • 你好 heringer,说我,我必须将哪个传递给服务器?你已经转换了imagefilepath而不是image?不需要转换图像?请解释。我不明白。
  • 请看编辑后的答案
  • 我在尝试您的第二种方法时遇到了两个错误。错误是我不能使用 System.Drawing.ImageView.Image。我的意思是,我在 imageview 上有图像。如何将其设置为 system.drawing?第二个错误是保存没有任何定义。如何解决这个问题?指导我赫林格。
【解决方案2】:

同样的只是少了一点代码

//Clientside    
byte[] imgBytes = File.ReadAllBytes("FilePath");
string imgStr = System.Convert.ToBase64String(imgBytes);

//Serverside 
byte[] serverSideImgBytes = System.Convert.FromBase64String(imgStr);
File.WriteAllBytes("PathAndFileName", serverSideImgBytes);

【讨论】:

  • 服务器是 php.. 怎么能把这段代码应用到 php 上?
  • 我认为你可以使用名为 base64_decode 的 php 函数
  • 伙计们,你们都教我如何转换文件路径。我不需要转换文件路径。我只需要转换图像。不是它的路径。请理解。
  • 替换图片路径上的 FilePath,例如 ../Images/Picture.jpg。当您将图像字节发送到服务器时,发送文件名进行保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2020-10-16
  • 2012-12-24
  • 2013-10-05
  • 2018-03-20
  • 2015-08-02
  • 1970-01-01
相关资源
最近更新 更多