好的,这就是我所拥有的:
几行代码,可以选择/拍摄照片并将其上传到我的服务器:
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
image1.Source = bmp;
BitmapImage bitmapImage = bmp;
var largest = Math.Max(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
var ratio = largest / 1024;
var width = bitmapImage.PixelWidth / ratio;
var height = bitmapImage.PixelHeight / ratio;
WriteableBitmap wb = new WriteableBitmap(bitmapImage);
Stream str = null;
wb.SaveJpeg(str, width, height, 0, 75);
byte[] sbytedata = ReadToEnd(str);
string s = EncodeTo64(sbytedata.ToString());
WebClient wc = new WebClient();
Uri u = new Uri("//something ;)//");
wc.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
wc.OpenWriteAsync(u, "POST", sbytedata);
而且..它不起作用:异常 - 屏幕
此外,您还需要 ReadToEnd() 函数。这是:
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position;
stream.Position = 0;
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
stream.Position = originalPosition;
}
}
有任何想法吗?