2

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

我正在尝试从媒体库(在 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;
}
4

1 回答 1

4

请在此处查看我的问题:带参数的照片上传到 PHP 页面

我的解决方案在这里: http: //nediml.wordpress.com/2012/05/10/uploading-files-to-remote-server-with-multiple-parameters/

于 2012-06-02T06:41:37.523 回答