如何将System.Byte []转换为要显示到Word C#的图像

时间:2016-05-23 08:14:03

标签: c# datagridview

我正在努力解决如何处理转换我的表中的System.Byte []数组来自我的Datagridview,请参阅包含仅显示Sytem.Byte []

的图像的列

enter image description here

这是我正在使用的代码,但它仍然只显示文本。

private void ToCsV(DataGridView DGV, string filename)
    {
        try
        {
            if (DGV.Rows.Count != 0)
            {


                int RowCount = DGV.Rows.Count;
                int ColumnCount = DGV.Columns.Count;
                Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];

                //add rows
                int r = 0;
                for (int c = 0; c <= ColumnCount - 1; c++)
                {
                    for (r = 0; r <= RowCount - 1; r++)
                    {
                        DataArray[r, c] = DGV.Rows[r].Cells[c].Value;
                    } //end row loop
                } //end column loop



                Document oDoc = new Document();
                oDoc.Application.Visible = true;



                //page orintation
                oDoc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;


                dynamic oRange = oDoc.Content.Application.Selection.Range;
                string oTemp = "";
                for (r = 0; r <= RowCount - 1; r++)
                {
                    for (int c = 0; c <= ColumnCount - 1; c++)
                    {
                        oTemp = oTemp + DataArray[r, c] + "\t";

                    }
                }

                //table format
                oRange.Text = oTemp;

                object Separator = WdTableFieldSeparator.wdSeparateByTabs;
                object ApplyBorders = true;
                object AutoFit = true;
                object AutoFitBehavior = WdAutoFitBehavior.wdAutoFitContent;

                oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
                                      Type.Missing, Type.Missing, ref ApplyBorders,
                                      Type.Missing, Type.Missing, Type.Missing,
                                      Type.Missing, Type.Missing, Type.Missing,
                                      Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);

                oRange.Select();

                oDoc.Application.Selection.Tables[1].Select();
                oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
                oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;
                oDoc.Application.Selection.Tables[1].Rows[1].Select();
                oDoc.Application.Selection.InsertRowsAbove(1);
                oDoc.Application.Selection.Tables[1].Rows[1].Select();

                //header row style
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Tahoma";
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;

                //add header row manually
                for (int c = 0; c <= ColumnCount - 1; c++)
                {
                    oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = DGV.Columns[c].HeaderText;
                }

                //table style 
                oDoc.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5");
                oDoc.Application.Selection.Tables[1].Rows[1].Select();
                oDoc.Application.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //header text
                foreach (Section section in oDoc.Application.ActiveDocument.Sections)
                {
                    Range headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    headerRange.Fields.Add(headerRange, WdFieldType.wdFieldPage);
                    headerRange.Text = "your header text";
                    headerRange.Font.Size = 16;
                    headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                }

               /* string fileName = @"C:\Users\JethroPaulo\Desktop\a314bf90a43cb49873d014b00bb3672b.jpg";  //the picture file to be inserted
                Object oMissed = oDoc.Paragraphs[2].Range; //the position you want to insert
                Object oLinkToFile = false;  //default
                Object oSaveWithDocument = true;//default
                oDoc.InlineShapes.AddPicture(fileName, ref  oLinkToFile, ref  oSaveWithDocument, ref  oMissed);

                //Insert text
                Object oMissing = System.Reflection.Missing.Value;
                var oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara1.Range.Text = "First Text";
                oPara1.Range.InsertParagraphAfter();


                Image sparePicture = fetch;
                Clipboard.SetDataObject(sparePicture);
                var oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara2.Range.Paste();
                oPara2.Range.InsertParagraphAfter();*/
                //save the file
                oDoc.SaveAs(filename);

                //NASSIM LOUCHANI
            }
        }
        catch (Exception ex)
        {
            DialogResult result = MessageBox.Show(ex.Message.ToString(), "Critical Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }
    }

3 个答案:

答案 0 :(得分:1)

您没有复制my answer exactly,但您错过了<GridView android:id="@+id/gridview_android_example" android:layout_width="wrap_content" ... />

ByteArrayToImage

...

Image sparePicture = ByteArrayToImage(fetch); //<-- This is what you're missing
Clipboard.SetDataObject(sparePicture);
var oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara2.Range.Paste();
oPara2.Range.InsertParagraphAfter();

你没有显示public Image ByteArrayToImage(byte[] byteArrayIn) { using (MemoryStream ms = new MemoryStream(byteArrayIn)) { Image returnImage = Image.FromStream(ms); return returnImage; } } 是什么,但我假设它是fetch,所以要实现这个:

byte[]

答案 1 :(得分:0)

先生。杰里米的想法是一个很大的帮助。我发现的缺失部分如下:

  1. 对象缺失值 - 此对象是指具有指定要放置图像的单元格的表。 [Document.Table [1] .Cells(row,col).Range]
  2. Image sparePicture - 而不是使用Object [,] DataArray(参见上面的代码),我使用了一个Image对象来存储图像数组,然后在剪贴板上设置并粘贴它。
  3. InsertParagrahAfter() - 使用此方法,我将图像逐个插入指定的单元格,就像在dataGridview中一样。

           for (int c = 0; c <= ColumnCount - 1; c++)
                {
                    for (r = 0; r <= RowCount - 1; r++)
                    {
                        if (c == 5)
                        {
    
                           Object oMissing = oDoc.Tables[1].Cell(r + 2, 6).Range; //the position where you want to put the images
    
                           Image sparePicture = ByteArrayToImage((byte[])DGV.Rows[r].Cells[c].Value);
                            Clipboard.SetImage(sparePicture);
                            Word.Paragraph oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);
                            oPara2.Range.Paste();
                            oPara2.Range.InsertParagraphAfter();
                        }
                    }
                }
    

答案 2 :(得分:0)

许多小时后,我创建了Encode和Serialize,这将返回一个字符串,其中包含您应用于img的src。

    string img_src = data.Rows[0].IsNull("PHOTO") ? string.Format("") : string.Format("data:image/jpeg;base64,{0}", data.Rows[0]["PHOTO"].EncodeAndSerialize().Replace("\"", ""));
----------------------------    
        public static string EncodeAndSerialize<T>(this T obj) where T : new()
                {
                    var settings = new JsonSerializerSettings
                    {
                        ContractResolver = new HtmlEncodeResolver(),
                        Formatting = Newtonsoft.Json.Formatting.None
                    };
                    return JsonConvert.SerializeObject(obj, settings);
                }