我正在开发一个Windows Phone应用程序,它使用PHP将用户选择的照片上传到服务器。 为此,我正在尝试将图像转换为base64并将其POST到PHP,然后将其解码回JPG图像。
base64字符串已成功发布到PHP但我总是得到一个损坏的JPG文件而不是服务器上的原始图像(意味着图像的编码/解码有一些错误)。
应用程序的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";
Stream photoStream = ImageToStream(image1);
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);
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;
}
} // End of Class
服务器上解码和保存图像的PHP代码是:
<?php
function base64_to_image( $imageData, $outputfile ) {
// encode & write data (binary)
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
// return output filename
return( $outputfile );
}
if (isset($_POST['image'])) {
base64_to_image($_POST['image'], "img.jpg");
}
else
die("no image data found");
?>
我不知道这里有什么问题。 我一直在寻找数小时才能做到这一点,但我仍然失败了。
请帮帮我。
修改 我只是发现我总是得到相同的base64字符串,而不管我选择的任何图像。 这是base64转换后得到的: http://textuploader.com/?p=6&id=vWZy
我现在真的很困惑。我为每个和非常的图像获得相同的输出。 我不知道为什么会这样。
我真的想这样做,而且我坚持使用base64编码。 请帮帮我。
答案 0 :(得分:0)
可能不是所有问题,但此代码已损坏:
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();
}
通过使用GetBuffer
,您将获得可能比真实数据更大的底层缓冲区。您可以使用ToArray
代替 - 并且在使用GetBuffer
或ToArray
之前无需“回放”流:
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();
}
如果这不起作用,你应该仔细查看前后文件,最好是在二进制文件编辑器中: