Graphics.DrawImage выводит System.Byte[] вместо изображения

В БД есть таблица с полями, одно из которых типа image. В программе пытаюсь вывести на печать картинку из ячейки:

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// ...
if (cell.Value != DBNull.Value)
{
   var data = (Byte[])(cell.Value);
   var stream = new MemoryStream(data);
   Image img = new Bitmap(stream);
   e.Graphics.DrawImage(img, new RectangleF(x, y, width, height));
}
}

Если поле пусто, то ничего не выводится. Но если там находится картинка, то пишется System.Byte[]. Пробовал рисовать и через Image, и через Bitmap, но результат один и тот же.

При этом похожий алгоритм успешно добавляет картинку в элемент pictureBox:

if (row.Cells[4].Value != DBNull.Value)
{
var data = (Byte[])(row.Cells[4].Value);
var stream = new MemoryStream(data);
pictureBoxPhoto.Image = Image.FromStream(stream);
}
else pictureBoxPhoto.Image = null;

Функция для печати целиком:

 private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                int iLeftMargin = e.MarginBounds.Left;
                int iTopMargin = e.MarginBounds.Top;
                bool bMorePagesToPrint = false;
                int iTmpWidth = 0;

                if (bFirstPage)
                {
                    foreach (DataGridViewColumn GridCol in nationalityDataGridView.Columns)
                    {
                        iTmpWidth = (int)(Math.Floor((GridCol.Width /
                            (double)iTotalWidth * iTotalWidth *
                            (e.MarginBounds.Width / (double)iTotalWidth))));

                        iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                            GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;

                        arrColumnLefts.Add(iLeftMargin);
                        arrColumnWidths.Add(iTmpWidth);
                        iLeftMargin += iTmpWidth;
                    }
                }

                iRow = nationalityDataGridView.SelectedRows.Count > 0 ? nationalityDataGridView.SelectedRows[0].Index : nationalityDataGridView.Rows.Count; //

                if (iRow <= nationalityDataGridView.Rows.Count - 1) // while
                {
                    DataGridViewRow GridRow = nationalityDataGridView.Rows[iRow];
                    //iCellHeight = GridRow.Height + 5;
                    iCellHeight = pictureBoxPhoto.Height;
                    int iCount = 0;

                    if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        bNewPage = true;
                        bFirstPage = false;
                        bMorePagesToPrint = true;
                        //break;
                    }
                    else
                    {
                        if (bNewPage)
                        {
                            e.Graphics.DrawString("Отчёт",
                                new Font(nationalityDataGridView.Font, FontStyle.Bold),
                                Brushes.Black, e.MarginBounds.Left,
                                e.MarginBounds.Top - e.Graphics.MeasureString("Отчёт",
                                new Font(nationalityDataGridView.Font, FontStyle.Bold),
                                e.MarginBounds.Width).Height - 13);

                            String strDate = DateTime.Now.ToLongDateString() + " " +
                                DateTime.Now.ToShortTimeString();

                            e.Graphics.DrawString(strDate,
                                new Font(nationalityDataGridView.Font, FontStyle.Bold), Brushes.Black,
                                e.MarginBounds.Left +
                                (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
                                new Font(nationalityDataGridView.Font, FontStyle.Bold),
                                e.MarginBounds.Width).Width),
                                e.MarginBounds.Top - e.Graphics.MeasureString("Отчёт",
                                new Font(new Font(nationalityDataGridView.Font, FontStyle.Bold),
                                FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                            iTopMargin = e.MarginBounds.Top;

                            foreach (DataGridViewColumn GridCol in nationalityDataGridView.Columns)
                            {
                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                    new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                    (int)arrColumnWidths[iCount], iHeaderHeight));

                                e.Graphics.DrawRectangle(Pens.Black,
                                    new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                    (int)arrColumnWidths[iCount], iHeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText,
                                    GridCol.InheritedStyle.Font,
                                    new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                    new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
                                    (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);

                                iCount++;
                            }

                            bNewPage = false;
                            iTopMargin += iHeaderHeight;
                        }

                        iCount = 0;

                        foreach (DataGridViewCell cell in GridRow.Cells)
                        {
                            if (cell.Value != null)
                            {
                                if (iCount < 5)
                                {
                                    e.Graphics.DrawString(cell.Value.ToString(),
                                    cell.InheritedStyle.Font,
                                    new SolidBrush(cell.InheritedStyle.ForeColor),
                                    new RectangleF((int)arrColumnLefts[iCount],
                                    iTopMargin,
                                    (int)arrColumnWidths[iCount], iCellHeight),
                                    strFormat);
                                }
                                else if (iCount == 6)
                                {
                                    if (cell.Value != DBNull.Value)
                                    {
                                        var data = (Byte[])(cell.Value);
                                        var stream = new MemoryStream(data);
                                        var img = Image.FromStream(stream);
                                        e.Graphics.DrawImage(img, new RectangleF((int)arrColumnLefts[iCount], iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
                                    }
                                }
                            }

                            e.Graphics.DrawRectangle(Pens.Black,
                                new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                (int)arrColumnWidths[iCount], iCellHeight));

                            iCount++;
                        }
                    }

                    iRow++;
                    iTopMargin += iCellHeight;
                }

                if (bMorePagesToPrint)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
            }
        }

Ответы (0 шт):