Добавление картинки в ячейку Word из программы
Есть код для вывода информации бд,которая находится в listView
var allfields = Fields1Entities.GetContex().Fields.ToList();
var application = new Word.Application();
application.Visible = true;
Word.Document document = application.Documents.Add();
foreach(var field in allfields)
{
Word.Paragraph tableParagraph = document.Paragraphs.Add();
Word.Range tableRange = tableParagraph.Range;
Word.Table fieldsTable = document.Tables.Add(tableRange, allfields.Count() + 1, 8);
fieldsTable.Borders.InsideLineStyle = fieldsTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
fieldsTable.Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
Word.Range cellRange;
cellRange = fieldsTable.Cell(1, 1).Range;
cellRange.Text = "Картинка";
cellRange = fieldsTable.Cell(1, 2).Range;
cellRange.Text = "Название";
cellRange = fieldsTable.Cell(1, 3).Range;
cellRange.Text = "Местоположение";
cellRange = fieldsTable.Cell(1, 4).Range;
cellRange.Text = "Год открытия";
cellRange = fieldsTable.Cell(1, 5).Range;
cellRange.Text = "Возвраст";
cellRange = fieldsTable.Cell(1, 6).Range;
cellRange.Text = "Условия эксплуатации";
cellRange = fieldsTable.Cell(1, 7).Range;
cellRange.Text = "Запасы";
cellRange = fieldsTable.Cell(1, 8).Range;
cellRange.Text = "Эксплуатируется/не эксплуатируется";
fieldsTable.Rows[1].Range.Bold = 1;
fieldsTable.Rows[1].Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
for(int i=0; i< allfields.Count(); i++)
{
var currentField = allfields[i];
cellRange = fieldsTable.Cell(i + 2, 1).Range;
Word.InlineShape imageShape = cellRange.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\" + currentField.Picture);
imageShape.Width = imageShape.Height = 40;
cellRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
cellRange = fieldsTable.Cell(i + 2, 2).Range;
cellRange.Text = currentField.Name;
cellRange = fieldsTable.Cell(i + 2, 3).Range;
cellRange.Text = currentField.Location;
cellRange = fieldsTable.Cell(i + 2, 4).Range;
cellRange.Text = currentField.Year_of_study.ToString();
cellRange = fieldsTable.Cell(i + 2, 5).Range;
cellRange.Text = currentField.Age;
cellRange = fieldsTable.Cell(i + 2, 6).Range;
cellRange.Text = currentField.Conditions_of_exploitation;
cellRange = fieldsTable.Cell(i + 2, 7).Range;
cellRange.Text = currentField.Stocks;
cellRange = fieldsTable.Cell(i + 2, 8).Range;
cellRange.Text = currentField.DevelopNot;
}
Проблема возникает на этой строчке, при добавлении в ячейку таблицы картинки
Word.InlineShape imageShape = cellRange.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\" + currentField.Picture);
cellRange = fieldsTable.Cell(i + 2, 1).Range;
var bit = currentField.Picture;
using (MemoryStream ms = new MemoryStream(bit))
{
BitmapImage img1 = new BitmapImage();
img1.BeginInit();
img1.CacheOption = BitmapCacheOption.OnLoad;
img1.StreamSource = ms;
img1.EndInit();
Word.InlineShape imageShape = cellRange.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory, @"..\..\", img1);
}
//imageShape.Width = imageShape.Height = 40;
cellRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
Попробовала через bitmap
Источник: Stack Overflow на русском