Skip to main content
 首页 » 编程设计

c#之如何将 BitmapImage 图像转换为数据,以便我可以通过 Windows Phone 7 的 HTTP POST 请求将其上传到 Web

2024年01月25日24telwanggs

我想获取一个 BitmapImage 并将其发布到我在服务器上的 PHP 页面。然后该页面会在服务器上为我创建一个 .jpg 文件。

如何将该图像转换为 HTTP POST 的数据?这是我在 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 
} 

结果应包含字节数组。