在Windows Phone上将ByteArray转换为base64

时间:2014-07-18 08:30:04

标签: windows-phone-8 encoding windows-phone base64

我使用base64转换器遇到了一个巨大的问题。我在Windows Phone上调用标准的base64转换功能,但结果总是不同于Web转换器和Android和iOS平台上的许多其他转换器。

基本上我从图库或相机中取出一个图像并将其转换为字节数组。字节数组传递给convert方法。

Convert.ToBase64String(bytearray);

结果字符串应如下所示:

/9j/4AAQSkZJRgABAQEASABIAAD//gATQ3JlYXRlZCB3aXRoIEdJTVD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wgARCAADAAMDAREAAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAABv/EABUBAQEAAAAAAAAAAAAAAAAAAAUG/9oADAMBAAIQAxAAAAFMVff/xAAVEAEBAAAAAAAAAAAAAAAAAAAEBf/aAAgBAQABBQIlxoS//8QAGhEAAAcAAAAAAAAAAAAAAAAAAAEEFVKh0f/aAAgBAwEBPwFrSQs9H//EABcRAAMBAAAAAAAAAAAAAAAAAAADE1H/2gAIAQIBAT8BszT/xAAbEAACAgMBAAAAAAAAAAAAAAABAwISAAQRIf/aAAgBAQAGPwJOsl1EpgFwjUHgHgz/xAAZEAACAwEAAAAAAAAAAAAAAAABEQAhMUH/2gAIAQEAAT8hwppWyGQzQ7P/2gAMAwEAAgADAAAAEJ//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDAQE/ECH/xAAXEQEAAwAAAAAAAAAAAAAAAAABANHw/9oACAECAQE/ENAqf//EABgQAQEAAwAAAAAAAAAAAAAAAAERACEx/9oACAEBAAE/EOKqRz0uAFSs2rn/2Q==

但结果就是这个

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAADAAMDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD7d+FP7cn7UvwM+F/w5+Cfws+KJ8LfDH4PeBPCXwt+HPhn/hCvh3rf/CO+BPh/oNh4T8I6H/bPiLwlq/iDV/7J0DSdPsDqmu6rqes6gbf7XqmoXt9NPcylFFfy7/b+e/8AQ6zb/wAOOM/+XH+2X/EKfC7/AKNtwD/4h3Dv/wA7j//Z

它的区别并不大,但它有所不同。 正确的是使用PHP生成的。 Windows手机是在图像上使用UNICODE编码生成的。

有谁知道问题出在哪里?

1 个答案:

答案 0 :(得分:1)

尝试使用WriteableBitmap

使用这段代码
 public string GetBase64Encoding(BitmapImage imageUrl)
        {
        byte[] bytearray = null;
        using (MemoryStream ms = new MemoryStream())
            {
            if (imageUrl == null)
                {

                }
            else
                {
                WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)imageUrl);

                wbitmp.SaveJpeg(ms, 46, 38, 0, 100);

                bytearray = ms.ToArray();
                }
            }
        string str = Convert.ToBase64String(bytearray);

        return str;
        }

有关更多信息,您也可以参考:

Convert base64 string to image in C# on Windows Phone

希望它有所帮助!