封装了一下,作成通用的,供其它朋友参照吧,大家一同把这个控件用好。
public void Detail_Format()
{
//绑定产品图片
BindPicture(this.Pic_ProductLogo,this.hidden_ProductLogo_URL);
}
/// <summary>
/// 绑定图片
/// </summary>
///<param name="pic">icture对象,用于显示图片</param>
///<param name="hiddenControl">隐藏域对象,用于绑定图片的路径,以间接实现显示图片</param>
/// <returns></returns>
public void BindPicture(GrapeCity.ActiveReports.SectionReportModel.Picture pic,
GrapeCity.ActiveReports.SectionReportModel.TextBox hiddenControl)
{
pic.Image = null;
string fileURL = string.Empty;
try
{
fileURL = hiddenControl.Value.ToString();
}
catch
{
}
if(string.IsNullOrEmpty(fileURL))
{
return;
}
System.IO.Stream stream = GetStreamFromURL(fileURL);
if (stream == null)
{
return;
}
pic.Image = System.Drawing.Image.FromStream(stream);
//释放资源
try
{
if(stream != null)
{
stream.Close();
}
}
catch
{
}
}
/// <summary>
/// 得到图片流
/// </summary>
/// <param name="picURL">图片URL</param>
/// <returns></returns>
public System.IO.Stream GetStreamFromURL(string picURL)
{
if(string.IsNullOrEmpty(picURL))
{
return null;
}
System.Net.WebResponse response = null;
System.IO.Stream stream = null;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(picURL);
response = request.GetResponse();
stream = response.GetResponseStream();
}
catch
{
}
return stream;
} |