使用C#下载jpg图片的问题
我使用C#从网站下载图片,能够下载到电脑,但是图片会变形变色。请问这时是怎么回事呢?还有我把缓冲区设置成64(byte[] buffer = new byte[64];)时和设置成1024时图片的颜色变化不一样,附件是我下载到的图片,第一个图片是使用1024的缓冲区下载的,第二张是用64的缓冲区下的。下面是我的代码private void button1_Click(object sender, EventArgs e)
{
string url = "http://st.blog.
string LocalPath = @"E:\imagedownload\1.jpg";
try
{
Uri u = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(u);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse();
Stream sIn = wr.GetResponseStream();
FileStream fs = new FileStream(LocalPath, FileMode.OpenOrCreate, FileAccess.Write);
long length = wr.ContentLength;
long i = 0;
decimal j = 0;
while (i < length)
{
byte[] buffer = new byte[1024];
i += sIn.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
sIn.Close();
wr.Close();
fs.Close();
MessageBox.Show("下载完成!");
}
catch(Exception ex)
{
MessageBox.Show("错了 "+ex.ToString());
}
}
[[it] 本帖最后由 slny 于 2008-11-28 14:59 编辑 [/it]]