gpt4 book ai didi

c# - 将图像从 Windows Phone 应用程序发布到 PHP

转载 作者:行者123 更新时间:2023-11-30 17:54:55 24 4
gpt4 key购买 nike

我正在尝试为 Windows Phone 7 开发一个应用程序,它使用 PHP 将选定的图片(从图片选择器任务)上传到服务器。

我正在尝试使用 HttpWebRequest 来执行此操作。并且数据也已成功发布。我面临的问题是图像在发布之前需要编码为 base64。而且,我无法正确编码。

到目前为止,这是我的 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";

string str = BitmapToByte(image1);
MessageBox.Show(str);

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;
}
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);
bytearray = ms.ToArray();
}
string str = Convert.ToBase64String(bytearray);
return str;
}
}

BitmapToByte 函数用于将图像转换为base64 字符串。并且,ImageToStream 函数用于将其转换为流。

现在,我严重怀疑这两个函数有问题。

此外,我为每张图片获得了完全相同的 base64 字符串。是的,无论我选择什么图像,我都会得到完全相同的 base64 字符串。这很奇怪。我不知道这有什么问题。

我在这里上传了一个文本文件:http://textuploader.com/?p=6&id=vWZy它包含该 base64 字符串。

在服务器端,PHP 成功地接受了 postdata,并且解码工作也很完美(我手动解码了一些 base64 字符串以确保这一点)。我面临的唯一问题是 base64 编码。

请帮帮我。

编辑我对 ImageToStream 和 BitmapToByte 函数进行了以下更改:

private MemoryStream ImageToStream(Image image1)
{
WriteableBitmap wb = new WriteableBitmap(400, 400);

wb.Render(image1, new TranslateTransform { X = 400, Y = 400 });

wb.Invalidate();
MemoryStream myStream = new MemoryStream();

wb.SaveJpeg(myStream, 400, 400, 0, 70);

return myStream;
}

private string BitmapToByte(Image image)
{
MemoryStream photoStream = ImageToStream(image);

byte[] bytearray = photoStream.ToArray();
string str = Convert.ToBase64String(bytearray);
return str;
}

最佳答案

您的 BitmapToByte 方法过于复杂。更改 ImageToStream 以返回 MemoryStream 并且:

private string BitmapToByte(Image image)
{
MemoryStream photoStream = ImageToStream(image);

byte[] bytearray = photoStream.ToArray();
string str = Convert.ToBase64String(bytearray);
return str;
}

关于c# - 将图像从 Windows Phone 应用程序发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com