简体   繁体   English

如何将BitmapImage图像转换为数据,以便可以使用Windows Phone 7通过HTTP POST请求将其上传到Web?

[英]How do I convert a BitmapImage image to data so I can upload it to the web on a HTTP POST request with Windows Phone 7?

I'd like to take a BitmapImage and POST it to a PHP page I have on a server. 我想获取一个BitmapImage并将其发布到服务器上的PHP页面。 The page then creates a .jpg file on the server for me. 该页面然后在服务器上为我创建一个.jpg文件。

How can I convert that image into data for HTTP POST? 如何将图像转换为HTTP POST数据? Here is a shortened version of what I have been using for my Objective-C code on the iPhone to give you an idea of what I'm looking to do: 这是我在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];

I understand this is a pretty complicated topic with a large amount of code. 我知道这是一个非常复杂的主题,包含大量代码。 Thanks for your help in advance. 感谢您的帮助。

First you convert the image to a byte array: 首先,将图像转换为字节数组:

System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] postData = stream.ToArray();

Then you create a POST request using HttpWebRequest . 然后,使用HttpWebRequest创建POST请求。

UPDATE(on comments below): 更新(在下面的评论):

I realized that you cannot use the above code in Silverlight/Windows Phone 7.5. 我意识到您不能在Silverlight / Windows Phone 7.5中使用以上代码。 But, you can use WriteableBitmap and copy it's pixels into a byte array. 但是,您可以使用WriteableBitmap并将其像素复制到字节数组中。

Example: 例:

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
}

result should contain the byte array. 结果应包含字节数组。

The following code should be fine (it's not tested). 以下代码应该没问题(未经测试)。

First, you have to convert your BitmapImage to a WriteableBitmap object and encode it into a JPEG stream using Extensions.SaveJpeg . 首先,您必须将BitmapImage转换为WriteableBitmap对象,然后使用Extensions.SaveJpeg其编码为JPEG流。

Then you can post your stream with WebClient , in particular using the OpenWriteAsync method and the OpenWriteCompleted event. 然后,您可以使用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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM