Reputation: 21
I was trying to upload an image after choosing with PhotoChooserTask to my php server and write in afile in server.I used UTF8 for encoding.but in the server,I am getting a only 1 kilobyte file whatever size the original image is.Then I encoded it to base64string and decoded in server. Now I got file having size 4/3*imagesize(without decoding), but after decoding I cant get any image(filesize equal to original filesize). I tried in many ways(for reading image) but failed to solve this.What can be the problem?Or can me suggest some other ways?
Code in Client:
PhotoChooserTask selectphoto = new PhotoChooserTask();
selectphoto.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
selectphoto.Show();
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
byte[] data = null;
using (MemoryStream stream = new MemoryStream())
{
WriteableBitmap wBitmap = new WriteableBitmap(bmp);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
data = stream.GetBuffer();
}
string utfData = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.49/xampp/imageserver.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
postData = String.Format("image={0}", utfData);
// 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();
System.Diagnostics.Debug.WriteLine("sssssrreeeeeessssulllltttttt========="+srresult);
}
}
}, null);
}, null);
}
In the server :
<?php
if (isset($_POST['image'])) {
$ifp = fopen( "withoutdecode.txt", "wb" );
fwrite( $ifp, (($_POST['image'])) );
fclose( $ifp );
$ifp2 = fopen( "theImage.png", "wb" );
fwrite( $ifp2, utf8_decode(($_POST['image'])) );
fclose( $ifp2 );
}
else
{
die("no image data found");
echo "fail";
}
?>
Upvotes: 0
Views: 318
Reputation: 21
I finally got my solution.
the problem was- As I used Base64 for encoding and decoding, some unsafe characters for network was also encoded.They were '+','/'and '='. This characters were not being found in the server.So, I replaced the '+','/' to '-','_' and removed '='.In the server, the opposite work in done(decdoing the edited base64 string back to original base64 string). Now I got the image in server after decoding .
Upvotes: 0
Reputation: 1619
try this :
on the WP side(with image re-size algorithm )
// convert image to string
public string ImageToByte(BitmapImage imageSource) { MemoryStream ms = new MemoryStream(); WriteableBitmap wb = new WriteableBitmap(imageSource); wb.SaveJpeg(ms, imageSource.PixelWidth, imageSource.PixelHeight, 0, 100); byte[] imageBytes = ms.ToArray();
string result = Convert.ToBase64String(imageBytes);
return result;
}
// Photo chooser task completed
BitmapImage image = new BitmapImage(); image.SetSource(e.ChosenPhoto);
string fileName = DateTime.Now.Ticks.ToString() + ".jpg";
State["filename"] = fileName;
// Load the picture back into our image
BitmapImage b = new BitmapImage();
b.CreateOptions = BitmapCreateOptions.None;
b.SetSource(e.ChosenPhoto);
double actualHeight = b.PixelHeight;
double actualWidth = b.PixelWidth;
double maxHeight = 600;
double maxWidth = 800;
double imgRatio = actualWidth / actualHeight;
double maxRatio = maxWidth / maxHeight;
double compressionQuality = 0.5;
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if (imgRatio < maxRatio)
{
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if (imgRatio > maxRatio)
{
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else
{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
int newh = Convert.ToInt32(ActualHeight);
int neww = Convert.ToInt32(actualWidth);
WriteableBitmap wb = new WriteableBitmap(b);
WriteableBitmap wb1 = wb.Resize(neww, newh, WriteableBitmapExtensions.Interpolation.Bilinear);
// Save the image into isolated storage
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
WriteableBitmap bitmap = new WriteableBitmap(wb1);
bitmap.SaveJpeg(targetStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 70);
byte[] content = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(content, 0, content.Length);
targetStream.Write(content, 0, content.Length);
targetStream.Flush();
}
}
BitmapImage fbmp = new BitmapImage();
using (MemoryStream ms = new MemoryStream())
{
wb1.SaveJpeg(ms, neww, newh, 0, 100);
fbmp.SetSource(ms);
}
string img64 = ImageToByte(fbmp);
Send the img64 to the server as you where doing! and it should work
Upvotes: 1