如何将BitmapImage图像转换为数据,以便使用Windows Phone 7通过HTTP POST请求将其上传到网
本文关键字:POST HTTP 7通过 请求 Phone Windows 转换 图像 BitmapImage 数据 | 更新日期: 2023-09-27 18:27:28
我想要一个BitmapImage并将其POST到服务器上的PHP页面。然后页面在服务器上为我创建了一个.jpg文件。
如何将该图像转换为HTTPPOST的数据?以下是我在iPhone上使用的Objective-C代码的缩短版本,让你了解我想要做的事情:
// We need to resize the images before uploading to the server.
resizedImage = [resizedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(h,w) interpolationQuality:kCGInterpolationHigh];
NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.9f);
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"'r'n--%@'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name='"userfile'"; filename='"%@.jpg'"'r'n", barcode]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream'r'n'r'n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"'r'n--%@--'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
我知道这是一个非常复杂的主题,包含大量的代码。感谢您提前提供的帮助。
首先将图像转换为字节数组:
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] postData = stream.ToArray();
然后使用HttpWebRequest创建POST请求。
更新(在下面的评论中):
我意识到您不能在Silverlight/Windows Phone 7.5中使用上述代码。但是,您可以使用WriteableBitmap并将其像素复制到字节数组中。
示例:
BitmapImage bi = new BitmapImage();
WriteableBitmap wbm = new WriteableBitmap(bi);
int w = wbm.PixelWidth;
int h = wbm.PixelHeight;
int[] p = wbm.Pixels;
int len = p.Length;
byte[] result = new byte[4 * w * h];
// Copy pixels to buffer
for (int i = 0, j = 0; i < len; i++, j += 4)
{
int color = p[i];
result[j + 0] = (byte)(color >> 24); // A
result[j + 1] = (byte)(color >> 16); // R
result[j + 2] = (byte)(color >> 8); // G
result[j + 3] = (byte)(color); // B
}
结果应该包含字节数组。
下面的代码应该很好(没有经过测试)。
首先,您必须将BitmapImage
转换为WriteableBitmap
对象,并使用Extensions.SaveJpeg
将其编码为JPEG流。
然后,您可以使用WebClient
发布流,特别是使用OpenWriteAsync
方法和OpenWriteCompleted
事件。
string uploadUri = "http://www.myuri.com";
WebClient webClient = new WebClient();
// This event will be raised when we are ready to send data to the server
webClient.OpenWriteCompleted += (s, args) =>
{
var writeableBitmap = new WriteableBitmap(bitmapImage);
// Write the encoded image into the result stream
writeableBitmap.SaveJpeg(
args.Result,
bitmapImage.PixelWidth,
bitmapImage.PixelHeight,
0,
100);
};
// This event will be raised when writing is completed
webClient.WriteStreamClosed += (s, args) =>
{
MessageBox.Show("Upload Complete");
};
// Write to the WebClient
webClient.OpenWriteAsync(new Uri(uploadUri), "POST");