0

每次我尝试在我当前正在做的项目的电子邮件正文中显示图像时,我都会遇到异常。它正在检索共享点图片库中的图像以显示为附件。这是我的代码,我在当前调用该方法的第 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;
        }



    }
}
4

1 回答 1

0

正如 gzaxx 所说,您忽略了内部的异常GetImageforCharts(),然后返回null.

在这种情况下,GetImageforCharts()当异常发生时不知道该怎么做——调用代码要求它发送一个类型的对象,MemoryStream当创建失败时,你所能做的就是 return null

但是,您可以从中删除异常处理程序GetImageforCharts()并在调用代码中捕获它,而不是在那里捕获异常。或者,您可以保留异常处理程序,可选地记录消息,并且

throw new ApplicationException("Error getting image for charts", ex);

因此您可以更好地控制调用方法时必须捕获的异常。

第三个选项是创建一个方法

bool TryGetImageforCharts(...params..., out MemoryStream stream)

false如果找不到图像(如int.TryParse()方法) ,则返回。

于 2013-10-21T06:50:55.153 回答