求大佬给个例子
private void Button1_Click(object sender, EventArgs e) { byte[] bet = returnbyte("E:\\IMG_4146.JPG");//图片转换成二进制 pictureBox1.Image = ReturnPhoto(bet); string strUrl = "http://localhost:24401/"; string strResult = this.GetPostString(strUrl, bet); } public string GetPostString(string url, byte[] data) { try { byte[] postBytes = data; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "POST"; myRequest.ContentType = "text/html"; myRequest.ContentLength = postBytes.Length; myRequest.Proxy = null; Stream newStream = myRequest.GetRequestStream(); newStream.Write(postBytes, 0, postBytes.Length); newStream.Close(); // Get response HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { string content = reader.ReadToEnd(); return content; } } catch (System.Exception ex) { return ex.Message; } } public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; } ///
private void Button1_Click(object sender, EventArgs e)
{
byte[] bet = returnbyte("E:\\IMG_4146.JPG");//图片转换成二进制
pictureBox1.Image = ReturnPhoto(bet);
string strUrl = "http://localhost:24401/";
string strResult = this.GetPostString(strUrl, bet);
}
public string GetPostString(string url, byte[] data)
{
try
{
byte[] postBytes = data;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "text/html";
myRequest.ContentLength = postBytes.Length;
myRequest.Proxy = null;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(postBytes, 0, postBytes.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
string content = reader.ReadToEnd();
return content;
}
}
catch (System.Exception ex)
{
return ex.Message;
}
}
public System.Drawing.Image ReturnPhoto(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
///
/// 图片路径转二进制
///
///
///
public byte[] returnbyte(string strpath)
{
// 以二进制方式读文件
FileStream fsMyfile = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 创建一个二进制数据流读入器,和打开的文件关联
BinaryReader brMyfile = new BinaryReader(fsMyfile);
// 把文件指针重新定位到文件的开始
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
// 关闭以上new的各个对象
brMyfile.Close();
return bytes;
}