国米preting基地64在C#中从通过JSON / PHP(base64_en code)基于图像 [英] Interpreting base 64 in C# from an image based via JSON/PHP (base64_encode)

查看:179
本文介绍了国米preting基地64在C#中从通过JSON / PHP(base64_en code)基于图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我能够成功地读取图像文件,并将其传递回我的C#应用​​程序,但我无法去code正确。

我返回JSON数据本身(未显示json_en code函数)通过​​PHP:

  $ imgbinary = FREAD(FOPEN($文件名,R),文件大小($文件名));        如果(strlen的($ imgbinary)大于0){
            返回阵列(成功=>真实的地图=> base64_en code(imgbinary $));
        }

然后在C#中我用Newtonsoft.Json脱code中的字符串(我可以读成功,并成功地在地图属性),但我无法再使用的base64德code正确地写入图像到一个文件(或显示)。

我做的这样:

  File.WriteAllText(System.Windows.Forms.Application.StartupPath +\\\\ MYDIR \\\\+文件名,Base64De code(的FileData));    公共字符串Base64De code(字符串数据)
    {
        字节[] =二进制Convert.FromBase64String(数据);
        返回Encoding.Default.GetString(二进制);
    }

我失去了一些疯狂简单吗?什么是真正奇怪的是,当我去code中的数据,文件大小比原文件大。 (我知道,一旦你连接code,约33%的数据增长,只是奇怪的是,当我再德code,它依然较大)。

任何帮助/指针将大大AP preciated!


解决方案

  

我失去了一些疯狂简单吗?


是的。图像的不是的文本文件,所以你不应该使用 File.WriteAllText 。什么的字符的你相信在一个图像文件present?当你的数据是从根本上的文字来区分是真的,真的很重要,当它从根本上二进制文件。如果试图把要么就好像它是其他的,你是在自找麻烦。

的从字节数组转换回文本(你的 Encoding.Default.GetString 的通话将被丢失数据) - 只使用

  File.WriteAllBytes(路径,Convert.FromBase64String(数据));

So I'm able to read an image file successfully, and pass it back to my C# application but I'm unable to decode it properly.

I'm returning the JSON data as such (the json_encode function isn't shown) via PHP:

        $imgbinary = fread(fopen($filename, "r"), filesize($filename));

        if ( strlen($imgbinary) > 0 ){
            return array("success"=>true, "map"=>base64_encode($imgbinary));
        }

Then in C# I use Newtonsoft.Json to decode the string (I can read success and the map properties successfully), but I'm unable to then use base64 decode to properly write the image to a file (or to display).

I'm doing it as such:

File.WriteAllText(System.Windows.Forms.Application.StartupPath + "\\MyDir\\" + FileName, Base64Decode(FileData));

    public string Base64Decode(string data)
    {
        byte[] binary = Convert.FromBase64String(data);
        return Encoding.Default.GetString(binary);
    }

Am I missing something crazy simple here? What is really strange is after I decode the data, the file size is LARGER than the original file. (I realize once you encode, data increases by about 33%, just strange that after I then decode, it is still larger).

Any help/pointers would be greatly appreciated!

解决方案

Am I missing something crazy simple here?

Yes. An image isn't a text file, so you shouldn't be using File.WriteAllText. What characters do you believe are present in an image file? It's really, really important to distinguish between when your data is fundamentally text, and when it's fundamentally binary. If you try to treat either as if it were the other, you're asking for trouble.

Don't convert back from the byte array to text (your Encoding.Default.GetString call will be losing data) - just use:

File.WriteAllBytes(path, Convert.FromBase64String(data));

这篇关于国米preting基地64在C#中从通过JSON / PHP(base64_en code)基于图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆