将图像从Windows Phone应用程序发布到PHP

时间:2013-03-22 01:58:29

标签: c# windows-phone-7 encoding base64

我正在尝试为Windows Phone 7开发一个应用程序,它使用PHP将选定的图片(从图片选择器任务)上传到服务器。

我正在尝试使用HttpWebRequest来执行此操作。数据也成功发布。 我面临的问题是在发布之前需要将图像编码为base64。 而且,我无法正确编码。

到目前为止,这是我的C#代码:

public partial class SamplePage : PhoneApplicationPage
    {
        public SamplePage()
        {
            InitializeComponent();
        }

        PhotoChooserTask selectphoto = null;

        private void SampleBtn_Click(object sender, RoutedEventArgs e)
        {
            selectphoto = new PhotoChooserTask();
            selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
            selectphoto.Show();
        }

        void selectphoto_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BinaryReader reader = new BinaryReader(e.ChosenPhoto);
                image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
                txtBX.Text = e.OriginalFileName;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+QR_Reader.MainPage.txtBlck+"/beamer/saveimage.php");
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                string str = BitmapToByte(image1);
                MessageBox.Show(str);

                string postData = String.Format("image={0}", str);   

                // Getting the request stream.
                request.BeginGetRequestStream
                (result =>
                {
                    // Sending the request.
                    using (var requestStream = request.EndGetRequestStream(result))
                    {
                        using (StreamWriter writer = new StreamWriter(requestStream))
                        {
                            writer.Write(postData);
                            writer.Flush();
                        }
                    }

                    // Getting the response.
                    request.BeginGetResponse(responseResult =>
                    {
                        var webResponse = request.EndGetResponse(responseResult);
                        using (var responseStream = webResponse.GetResponseStream())
                        {
                            using (var streamReader = new StreamReader(responseStream))
                            {
                                string srresult = streamReader.ReadToEnd();
                            }
                        }
                    }, null);
                }, null);

            }  // end of taskresult == OK           
        }  // end of select photo completed

        private Stream ImageToStream(Image image1)
        {
            WriteableBitmap wb = new WriteableBitmap(400, 400);

            wb.Render(image1, new TranslateTransform { X = 400, Y = 400 });

            wb.Invalidate();
            Stream myStream = new MemoryStream();

            wb.SaveJpeg(myStream, 400, 400, 0, 70);

            return myStream;
        }
        private string BitmapToByte(Image image) //I suspect there is something wrong here
        {
            Stream photoStream = ImageToStream(image);
            BitmapImage bimg = new BitmapImage();
            bimg.SetSource(photoStream); //photoStream is a stream containing data for a photo

            byte[] bytearray = null;
            using (MemoryStream ms = new MemoryStream())
            {
                 WriteableBitmap wbitmp = new WriteableBitmap(bimg);
                 wbitmp.SaveJpeg(ms, wbitmp.PixelWidth, wbitmp.PixelHeight, 0, 100);
                 bytearray = ms.ToArray();
            }
            string str = Convert.ToBase64String(bytearray);
            return str;
        }
    }

BitmapToByte函数用于将图像转换为base64字符串。 并且,ImageToStream函数用于将其转换为流。

现在,我非常怀疑这两个功能有问题。

此外,我为每个图像获得完全相同的base64字符串。是的,无论我选择什么图像,我都得到完全相同的base64字符串。这很奇怪。 我不知道这有什么问题。

我在这里上传了一个文本文件:http://textuploader.com/?p=6&id=vWZy 它包含base64字符串。

在服务器端,PHP正在成功接受postdata并且解码也正常工作(我手动解码了一些base64字符串以确保这一点)。 我遇到的唯一问题是base64编码。

请帮帮我。

修改 我在ImageToStream和BitmapToByte函数中进行了以下更改:

private MemoryStream ImageToStream(Image image1)
        {
            WriteableBitmap wb = new WriteableBitmap(400, 400);

            wb.Render(image1, new TranslateTransform { X = 400, Y = 400 });

            wb.Invalidate();
            MemoryStream myStream = new MemoryStream();

            wb.SaveJpeg(myStream, 400, 400, 0, 70);

            return myStream;
        }

        private string BitmapToByte(Image image)
        {
            MemoryStream photoStream = ImageToStream(image);

            byte[] bytearray = photoStream.ToArray();
            string str = Convert.ToBase64String(bytearray);
            return str;
        }

1 个答案:

答案 0 :(得分:1)

你的BitmapToByte方法非常复杂。更改ImageToStream以返回MemoryStream和:

private string BitmapToByte(Image image)
{
    MemoryStream photoStream = ImageToStream(image);

    byte[] bytearray = photoStream.ToArray();
    string str = Convert.ToBase64String(bytearray);
    return str;
}