在C#(c/s)中如何实现将打开的图片保存到指定的文件夹中
privatevoidbutton2_Click(objectsender,EventArgse){openFileDialog1.ShowDialog();pictur...
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
.........下面的代码如何写?
} 展开
{
openFileDialog1.ShowDialog();
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
.........下面的代码如何写?
} 展开
1个回答
展开全部
public static int SaveImageFromWeb(string imgUrl,string path,string fileName)
...{
if(path.Equals(\"\"))
throw new Exception(\"未指定保存文件的路径\");
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf(\"/\")+1);
string defaultType = \".jpg\";
string[] imgTypes = new string[]...{\".jpg\",\".jpeg\",\".png\",\".gif\",\".bmp\"};
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf(\".\"));
foreach (string it in imgTypes)
...{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(\".bmp\"))
imgType = defaultType;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = \"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)\";
request.Timeout = 3000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if( response.ContentType.ToLower().StartsWith(\"image/\") )
...{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
if(fileName == \"\")
fileName = imgName;
FileStream fso = new FileStream(path+fileName+imgType,FileMode.Create);
while(l<imgLong)
...{
int i = stream.Read(arrayByte,0,1024);
fso.Write(arrayByte,0,i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
...{
return 0;
}
}
...{
if(path.Equals(\"\"))
throw new Exception(\"未指定保存文件的路径\");
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf(\"/\")+1);
string defaultType = \".jpg\";
string[] imgTypes = new string[]...{\".jpg\",\".jpeg\",\".png\",\".gif\",\".bmp\"};
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf(\".\"));
foreach (string it in imgTypes)
...{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(\".bmp\"))
imgType = defaultType;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = \"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)\";
request.Timeout = 3000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if( response.ContentType.ToLower().StartsWith(\"image/\") )
...{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
if(fileName == \"\")
fileName = imgName;
FileStream fso = new FileStream(path+fileName+imgType,FileMode.Create);
while(l<imgLong)
...{
int i = stream.Read(arrayByte,0,1024);
fso.Write(arrayByte,0,i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
...{
return 0;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |