日期:2014-05-17 浏览次数:21026 次
//转换二进制,通过网络地址获取图片,转换成二进制保存到数据库
public static byte[] BitmapToBytes(string photourl)
{
byte[] bytes=null;
Random seed = new Random();
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(photourl);
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
bytes = new byte[4096 + (int)response.ContentLength];
stream.Read(bytes, 0, Convert.ToInt32(4096 + (int)response.ContentLength));
}
catch (Exception ex)
{}
}
SqlParameter parameter = command.CreateParameter();
parameter.SqlDbType = SqlDbType.Image;
parameter.ParameterName = "@photo";
parameter.Value = images;
command.Parameters.Add(parameter);
String url = "http://img04.taobaocdn.com/sns_album/i4/T1yAdWXgdGXXb1upjX.jpg";
String fileName = url.Substring(url.LastIndexOf("/") + 1);
String refer = url.Substring(0, url.LastIndexOf("/") + 1);
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
req.AllowAutoRedirect = true;
req.Referer = refer;
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";