php - Windows Phone中上传图片和php POST方法

标签 php web-services windows-phone-7 image-processing byte

这是我的第一篇文章。请善待:)

我正在尝试从媒体库(在 WP 7 中)获取图片,使用 httpwebrequest 上传它,并将其保存到服务器中的文件夹中。我成功地将图像转换为字节字符串(但我怀疑这里有问题),使用 POST 发送字符串,并在我的 php 网页中检索它。

一切似乎都运行良好,但是当我将字节字符串转换为 jpeg 时(使用 imagecreatefromstring 函数),它总是出现空图片。这是我的 C# 和 php 代码。如果我的英语不完美(或远非完美),我很抱歉:)

这是我的 C# 代码以及一些注释

 public partial class MainPage : PhoneApplicationPage
{
    string uploadUri = @"http://192.168.138.1/meeshot/upload.php"; //php web page for retrieve and saving file in the server
    string requestImageName = "picture"; //variable name for post ---- >$_POST['picture']
    string postdata; //byte data generate using BitmapToByte function

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }
    PhotoChooserTask selectphoto = null; 
    Image image1  = new Image ();
    private void button1_Click(object sender, RoutedEventArgs e) //user choosing photo from media library
    {
        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));

                     HttpWebRequest req = HttpWebRequest.Create(
                  new    Uri(this.uploadUri)) as HttpWebRequest;

                     postdata = BitmapToByte(image1); //convert image to byte. My suspisicion there is something wrong here
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.BeginGetRequestStream(HttpWebRequestButton2_RequestCallback, req);   

         }




    }
    private void HttpWebRequestButton2_RequestCallback(IAsyncResult result)
    {
        var req = result.AsyncState as HttpWebRequest;

        using (var requestStream = req.EndGetRequestStream(result))
        {
            using (StreamWriter writer = new StreamWriter(requestStream))
            {
                writer.Write(requestImageName+"="+this.postdata); //writing "picture=bytedata"
                writer.Flush();
            }
        }
        req.BeginGetResponse(HttpWebRequestButton_Callback, req);
    }
    private void HttpWebRequestButton_Callback(IAsyncResult result)
    {
        var req = result.AsyncState as HttpWebRequest;
        var resp = req.EndGetResponse(result);
        var strm = resp.GetResponseStream();
        var reader = new StreamReader(strm);

        this.Dispatcher.BeginInvoke(() => {
                this.DownloadedText.Text = reader.ReadToEnd();   //the web page will print byte data that has been sent using httpwebrequest. i can see that byte data has benn sent sucessfuly.
                this.DownloadedText.Visibility = System.Windows.Visibility.Visible;
            });
    }      

    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);
            ms.Seek(0, SeekOrigin.Begin);
            bytearray = ms.GetBuffer();
        }
        string str = Convert.ToBase64String(bytearray);
        return str;
    }

这是我在 php 网页中的代码

    if(isset($_REQUEST['picture'])) //check
{
    $myFile = "picture.jpg";
    $fh = fopen($myFile, 'wb') or die("can't open file");
    $stringData = $_REQUEST['picture']."<br>";

    $im = imagecreatefromstring($stringData);
    if ($im) {

    imagejpeg($im);
    fwrite($fh, $im);
    imagedestroy($im);
        }
    fclose($fh);
     echo $stringData;
}

最佳答案

关于php - Windows Phone中上传图片和php POST方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860309/

相关文章:

web-services - 如何从 grails 独立客户端访问 EJB 服务

c# - 地理数据网络服务

c# - HttpClient.PostAsync 花费的时间是实际延迟的两倍?

windows-mobile - Windows Phone 7 开发,与版本 6.x 有何不同?

.NET 的 Javascript 解释器

php - iOS 安全性将带有密码的数据发送至服务器或从服务器发送数据

php - SQL语句只更新一个用户的数据

php - 是否可以使用 MySQL - LOAD DATA LOCAL INFILE 仅上传 CSV 文件的前 10 行?

php - 什么是 <<<_END?

c# - 计算一个坐标是否在另一个坐标范围内