Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post image from Windows Phone Application to PHP

I am trying to develop an application for Windows Phone 7 which uploads a selected picture (from the picture chooser task) to a server with the use of PHP.

I am trying to use HttpWebRequest in order to do this. And the data is posted successfully too. The Problem that I am facing is that the image is required to be encoded to base64 before posting. And, I am not able to encode it properly.

This is my C# code so far:

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;
        }
    }

The BitmapToByte function is used to convert the image to base64 string. And, the ImageToStream function is used to convert it to stream.

Now, I seriously suspect that there is something wrong in these two functions.

Further, I'm getting exactly the same base64 string for every image. Yes, I'm getting exactly same base64 string irrespective of what image I'm selecting. This is very weird. I don't know what is wrong with this.

I've uploaded a text file here: http://textuploader.com/?p=6&id=vWZy It contains that base64 string.

On, the server side, the PHP is accepting the postdata successfully and the decoding is working perfectly too (I decoded some base64 strings manually to ensure this). Only problem I'm facing is base64 encoding.

Please help me.

EDIT I've made the following changes in the ImageToStream and BitmapToByte functions:

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;
        }
like image 962
Shrayansh Sharma Avatar asked Jun 01 '26 09:06

Shrayansh Sharma


1 Answers

You BitmapToByte method is tremendously overcomplicated. Change ImageToStream to return a MemoryStream and:

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

    byte[] bytearray = photoStream.ToArray();
    string str = Convert.ToBase64String(bytearray);
    return str;
}
like image 130
Jason Watkins Avatar answered Jun 03 '26 23:06

Jason Watkins