每次我尝试在我当前正在做的项目的电子邮件正文中显示图像时,我都会遇到异常。它正在检索共享点图片库中的图像以显示为附件。这是我的代码,我在当前调用该方法的第 264 行出现错误。
if (txtEventPictureURL.Text != "")
{
string siteurl = "http://it3127:30091";
string filename = txtEventPictureURL.Text;
System.Drawing.Image imgForCallOut = Image.FromStream(GetImageforCharts(siteurl, filename)); //LINE 264 ERROR
mailItem.HTMLBody = Environment.NewLine + @"<html>Event Title : " + txtTitle.Text +
Environment.NewLine + "Event Description : " + txtDescription.Text + Environment.NewLine + "
Event Start Date From :" + dtpStartDate.Text + " To " + dtpEndDate.Text + Environment.NewLine + "Time From : " + cbStartHours.Text + " : " +
cbStartMins.Text + " To " + cbEndHours.Text + " : " + cbEndMins.Text + "Image : " + "<img src=" + imgForCallOut + " /> </html>";
//var doc = global.ThisAddIn.Application.ActiveWindow().WordEditor;
//var pic = doc.Application.Selection.InlineShapes.AddPicture("MY IMAGE URL", true);
//doc.Application.Selection.Hyperlinks.add(pic, "MY URL");
}
}
public static MemoryStream GetImageforCharts(string siteUrl, string fileName)
{
Byte[] fileContentsArray = null;
MemoryStream imageStream = null;
//siteUrl = "http://it3127:30091/";
try
{
using (SPSite site = new SPSite(siteUrl))
// using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb())
{
SPPictureLibrary chartPictureLibrary = (SPPictureLibrary)web.Lists["Pictures"];
SPQuery query = new SPQuery();
query.Query = @"<Where><Eq><FieldRef Name ='Title'/><Value Type='Text'>" + fileName + "</Value></Eq></Where>";
SPListItemCollection lstImages = chartPictureLibrary.GetItems(query);
foreach (SPListItem r in lstImages)
{
SPFile file = r.File;
using (Stream fileContents = file.OpenBinaryStream())
{
long length = fileContents.Length;
fileContentsArray = new Byte[length];
fileContents.Read(fileContentsArray, 0, Convert.ToInt32(length));
}
}
}
}
imageStream = new MemoryStream(fileContentsArray);
return imageStream;
}
catch (Exception ex)
{
return null;
}
}
}