无法将'System.String'类型的对象强制转换为'System.Byte []' [英] Unable to cast object of type 'System.String' to type 'System.Byte[]'

查看:137
本文介绍了无法将'System.String'类型的对象强制转换为'System.Byte []'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
{
    PrintDocument prnt = new PrintDocument();
    PrintPreviewDialog printD = new PrintPreviewDialog();

    printD.Document = prnt;
    prnt.PrintPage += prnt_PrintPage;
    prnt.DefaultPageSettings.PaperSize = new PaperSize("short", 850, 1100);
    prnt.DefaultPageSettings.Landscape = false;
    printD.Show();
}

void prnt_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Graphics grap = e.Graphics;
    int y = 210;

    for (int o = line; o < dataGridView2.RowCount - 1; o++)
    {
        grap.DrawString("BluRe's Pet Store", new Font("Verdana", 18), Brushes.Black, new Point(300, 50));
        grap.DrawString("Cats Information", new Font("Verdana", 18), Brushes.Black, new Point(300, 80));

        grap.DrawString("Picture", new Font("arial", 14), Brushes.Teal, new Point(50, 150));
        grap.DrawString("Name", new Font("arial", 14), Brushes.Teal, new Point(230, 150));
        grap.DrawString("Breed", new Font("arial", 14), Brushes.Teal, new Point(400, 150));
        grap.DrawString("Type", new Font("arial", 14), Brushes.Teal, new Point(550, 150));
        grap.DrawString("Information", new Font("arial", 14), Brushes.Teal, new Point(710, 150));

        byte[] gambar = new byte[0];
        gambar = (byte[])(dataGridView2.CurrentRow.Cells[o].Value);//the error occur
        MemoryStream ms = new MemoryStream(gambar);
        Bitmap bm = new Bitmap(ms);
        //e.Graphics.DrawImage(bm, 50, y);

        grap.DrawImage(bm, 40, y);
        grap.DrawString(dataGridView2.Rows[o].Cells["Name"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(260, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Breed"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(430, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Type"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(530, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Information"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(710, y));

        y = y + 40;
        line++;
        if (line % 12 == 0)
        {
            break;
        }
    }

    if (line < dataGridView2.RowCount - 1)
    {
        page = page + 1;
        e.HasMorePages = true;
    }
    else
    {
        line = 0;
        e.HasMorePages = false;
        page = 1;
    } 
}

推荐答案

这意味着您在数据库设计中犯了一个大错:您将图像存储在NVARCHAR或VARCHAR字段中:您不能这样做并可靠地访问数据,因为它们不是绝对字段 - 它们是基于字符的。你需要将它们存储在一个基于字节的列中。



见这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它会告诉你该怎么做。
What that means is that you have made a big mistake in your DB design: you are storing your images in NVARCHAR or VARCHAR fields: you can't do that and reliably access the data because they are not "absolute" fields - they are character based. You need to store them in a byte based column.

See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows you what to do.


这篇关于无法将'System.String'类型的对象强制转换为'System.Byte []'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆