3

我正在尝试为 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;
        }
4

1 回答 1

1

您的 BitmapToByte 方法非常复杂。更改ImageToStream为返回一个MemoryStream和:

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

    byte[] bytearray = photoStream.ToArray();
    string str = Convert.ToBase64String(bytearray);
    return str;
}
于 2013-03-22T02:15:48.553 回答