【80分】通过网络url地址获取图片,转换成二进制保存到数据库

h475410885 2011-09-26 01:38:13
通过网络地址获取图片,转换成二进制保存到数据库,可是保存到数据库中其中有部分图片显示不完整。代码:

//转换二进制,通过网络地址获取图片,转换成二进制保存到数据库
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)
{}
}



现在是部分图片显示上面样式。大部分都正确。。如果把加红的部分byte[1024];改为固定的只要小于1024字节的图片就能显示大于的统统不显示。。



图片见下面样式



有人用过这个方面没有。把完整上的url地址获取图片。然后转换成二进制流保存。我这儿有些图片老是保存不完整

先说明,在添加数据到数据中我是写对了的

 
SqlParameter parameter = command.CreateParameter();
parameter.SqlDbType = SqlDbType.Image;
parameter.ParameterName = "@photo";
parameter.Value = images;
command.Parameters.Add(parameter);


...全文
1026 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
magic009 2012-12-03
  • 打赏
  • 举报
回复
孟老大NB,困扰几天了
Specialaspnet 2012-11-06
  • 打赏
  • 举报
回复
谢谢各位!!!!
abaal888 2011-12-13
  • 打赏
  • 举报
回复
找了好久,总算找到了。孟子E章 老大的方法很实用
h475410885 2011-09-26
  • 打赏
  • 举报
回复
谢谢楼上的朋友。。

sshakl
你的也采用
byte[] bPicture = ms.GetBuffer();//注意这一条与下两条语句的区别

现在结贴。非常感谢。。

h475410885 2011-09-26
  • 打赏
  • 举报
回复
嗯。谢谢孟老大。孟老大V5

按着您写的方法现在已经弄好了。
我一直没懂stream 这个获取字节后的原理。您这个和我以前写的是多了一个写入缓存。然后冲缓存中获取到无字节的byte数据。那么我以前那种存在什么原因。
sshakl 2011-09-26
  • 打赏
  • 举报
回复
 private void btnSavePicture_Click(object sender, EventArgs e) 
{
Image Picture = this.pictureBox2.Image;//获取图片

MemoryStream ms = new MemoryStream();
Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//转换成数据流

byte[] bPicture = ms.GetBuffer();//注意这一条与下两条语句的区别

//下面这两条语句似乎要实现存储二进制的目的,但存储图片的方式会有问题,读出来的数据无法还原成图片
//byte[] bPicture = new byte[ms.Length];
//ms.Write(bPicture, 0, (int)ms.Length);


SqlConnection conn = new SqlConnection("Data Source=.;Integrated Security=True");

string strSQL = "use Northwind Insert into Pictures(Picture) values(@image)";

SqlCommand cmd = new SqlCommand(strSQL, conn);

cmd.Parameters.Add("@image", SqlDbType.Image);

cmd.Parameters["@image"].Value = bPicture;

try
{
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("存储成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
conn.Close();
}
}
孟子E章 2011-09-26
  • 打赏
  • 举报
回复
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";
System.Net.HttpWebResponse res = req.GetResponse() as System.Net.HttpWebResponse;
System.IO.Stream stream = res.GetResponseStream();
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
byte[] buffer = new byte[32 * 1024];
int bytes;
while ((bytes = stream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytes);
}
byte[] images = memoryStream.GetBuffer();
res.Close();
memoryStream.Dispose();


保存方法

command.Parameters.AddWithValue("@photo",images );
h475410885 2011-09-26
  • 打赏
  • 举报
回复
我看到图片无法显示,感觉二进制数据空间不足。然后给多加4Kb的空间。但是还是无法显示。。没加也是有这个问题。
孟子E章 2011-09-26
  • 打赏
  • 举报
回复
你为啥要加4096 ?
h475410885 2011-09-26
  • 打赏
  • 举报
回复
我觉得我把内容说的很完整了。如果还有什么不清楚的可以提出来啊!怎么没人来帮顶呢!寻求高手或知道的兄弟们帮帮啊!我结贴率100%从不扣分的

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧