Windows phone 7 服务器正在使用windows phone上载损坏的图像
我尝试在选择使用PhotoChooserTask后将一个图像上载到我的php服务器并在服务器中写入文件。我使用UTF8进行编码。但在服务器中,无论原始图像大小,我只得到1 KB的文件。然后我将其编码为base64string并在服务器中解码。现在我得到的文件大小为4/3*imagesize(无解码),但解码后我无法得到任何图像(文件大小等于原始文件大小)。我尝试了很多方法(阅读图像),但都没能解决这个问题。有什么问题吗?或者我可以建议其他一些方法吗 客户端中的代码:Windows phone 7 服务器正在使用windows phone上载损坏的图像,windows-phone-7,windows-phone-8,windows-phone,Windows Phone 7,Windows Phone 8,Windows Phone,我尝试在选择使用PhotoChooserTask后将一个图像上载到我的php服务器并在服务器中写入文件。我使用UTF8进行编码。但在服务器中,无论原始图像大小,我只得到1 KB的文件。然后我将其编码为base64string并在服务器中解码。现在我得到的文件大小为4/3*imagesize(无解码),但解码后我无法得到任何图像(文件大小等于原始文件大小)。我尝试了很多方法(阅读图像),但都没能解决这个问题。有什么问题吗?或者我可以建议其他一些方法吗 客户端中的代码: PhotoChooser
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);
}
PhotoChooserTask selectphoto=new PhotoChooserTask();
选择Photo.Completed+=新事件处理程序(photoChooserTask_已完成);
选择photo.Show();
无效photoChooserTask_已完成(对象发送方,PhotoResult e)
{
if(e.TaskResult==TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp=新系统.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
字节[]数据=null;
使用(MemoryStream stream=new MemoryStream())
{
WriteableBitmap wBitmap=新的WriteableBitmap(bmp);
SaveJpeg(stream,wBitmap.PixelWidth,wBitmap.PixelHeight,0100);
stream.Seek(0,SeekOrigin.Begin);
data=stream.GetBuffer();
}
字符串utfData=System.Text.Encoding.UTF8.GetString(数据,0,数据.长度);
HttpWebRequest请求=(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);
//获取请求流。
request.BeginGetRequestStream
(结果=>
{
//发送请求。
使用(var requestStream=request.EndGetRequestStream(结果))
{
使用(StreamWriter=newstreamwriter(requestStream))
{
writer.Write(postData);
writer.Flush();
}
}
//得到回应。
request.BeginGetResponse(responseResult=>
{
var webResponse=request.EndGetResponse(responseResult);
使用(var responseStream=webResponse.GetResponseStream())
{
使用(var streamReader=newstreamreader(responseStream))
{
字符串srresult=streamReader.ReadToEnd();
System.Diagnostics.Debug.WriteLine(“SSSSS RREEEEESSSULLLTTTTTT====”+srresult);
}
}
},空);
},空);
}
在服务器中:
<?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";
}
?>
试试这个:
在WP侧(使用图像重新调整大小算法)
//将图像转换为字符串
公共字符串ImageToByte(BitmapImage imageSource)
{
MemoryStream ms=新的MemoryStream();
WriteableBitmap wb=新的WriteableBitmap(imageSource);
wb.SaveJpeg(ms,imageSource.PixelWidth,imageSource.PixelHeight,0100);
byte[]imageBytes=ms.ToArray()
//照片选择器任务已完成
BitmapImage=新的BitmapImage();
image.SetSource(如ChosenPhoto)
string fileName=DateTime.Now.Ticks.ToString()+“.jpg”;
状态[“文件名”]=文件名;
//将图片加载回我们的图像
BitmapImage b=新的BitmapImage();
b、 CreateOptions=BitmapCreateOptions.None;
b、 SetSource(如ChosenPhoto);
双实际高度=b像素高度;
双实际宽度=b像素宽度;
双倍最大高度=600;
双最大宽度=800;
双imgRatio=实际宽度/实际高度;
double maxRatio=最大宽度/最大高度;
双压缩质量=0.5;
如果(实际高度>最大高度| |实际宽度>最大宽度)
{
if(imgRatiomaxRatio)
{
//根据maxWidth调整高度
imgRatio=最大宽度/实际宽度;
实际高度=平均*实际高度;
实际宽度=最大宽度;
}
其他的
{
实际高度=最大高度;
实际宽度=最大宽度;
}
}
int newh=转换为32(实际高度);
int neww=转换为32(实际宽度);
WriteableBitmap wb=新的WriteableBitmap(b);
WriteableBitmap wb1=wb.Resize(neww,newh,WriteableBitmapExtensions.Interpolation.Bilinear);
string result = Convert.ToBase64String(imageBytes);
return result;
}
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);