Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:07:47+00:00 2026-06-05T08:07:47+00:00

this is my first post. please be kind :) i’m trying to get picture

  • 0

this is my first post. please be kind 🙂

i’m trying to get picture from media library (in WP 7), upload it using httpwebrequest, and save it to folder in the server. i succeed to convert the image to string of byte (but i suspect there are something wrong here), send the string using POST, and retrieve it in my php web page.

Everything seems to be working well, but when i convert the string of byte to jpeg (using imagecreatefromstring function) it always come up empty picture. here is my code in C# and php. I’m sorry for my English if it’s not perfect (or far from perfect) 🙂

this is my c# code along with some comments

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

and this is my code in php web page

    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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:07:48+00:00Added an answer on June 5, 2026 at 8:07 am

    Please look at my question here: Photo upload with parameters to a PHP page

    And my solution here: http://nediml.wordpress.com/2012/05/10/uploading-files-to-remote-server-with-multiple-parameters/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post on stackoverflow.com so please be kind (rewind) ;) I
This is my first post so please bear with me. I'm trying to execute
This is my first post, so please be gentle. I've recentley started using Powershell
This is my first post, so please go easy on me if I say
This is my first post on Stack, so please bear with me if I
This is my first post, so please be nice :) I have a question
This is my first post, and first please forgive me for my poor english.
This is my first post, so please forgive me if this question has been
This is my first post, so please be gentle. I've been playing around with
First post, please be kind. NOTE: I have reviewed entry #20856 (how to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.