現在行っているプロジェクトのメール本文に画像を表示しようとするたびに、例外が発生し続けます。添付ファイルとして表示するために、sharepoint 画像ライブラリ内の画像を取得しています。これは私のコードで、現在メソッドを呼び出している 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;
}
}
}