无法将参数值从位图转换为byte []。 [英] Failed to convert parameter value from a bitmap to a byte[].

查看:66
本文介绍了无法将参数值从位图转换为byte []。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里创建Windows应用程序我正在使用数据网格视图并在图片框上显示产品图像但我在图像中插入数据库错误来自下面线



I am create windows application here i am using data grid view and display products images on picturebox but i am insert a data base in image error is came on below line

Failed to convert parameter value from a Bitmap to a Byte[].

 command.Parameters["@imgmodel"].Value = pictureBox1.Image;







请任何一位导游或帮助我。



我尝试过:






Please any one guide or help me.

What I have tried:

Failed to convert parameter value from a Bitmap to a Byte[].

推荐答案

你很幸运:大多数人在此之前陷入困境,只有在他们装满他们想到的数据库后才会发现是图像,但不是!



同样的问题,同样的解决方案:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ]
You're lucky: most people mess up well before that and only find out once they have loaded the DB full of what they think are images, but aren't!

Same problem though, and same solution: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]


创建一个MemoryStream并将图像保存到它,然后你可以转换MemoryStream到一个字节数组并将其传递给您的SQL参数:

Create a MemoryStream and save the Image to it, then you can convert the MemoryStream to a byte array and pass that to your SQL parameter:
using (MemoryStream ms = new MemoryStream())
{
    pictureBox1.Image.Save(ms, ImageFormat.Png);
    command.Parameters["@imgmodel"].Value = ms.ToArray();
}


这篇关于无法将参数值从位图转换为byte []。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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