图像数据类型问题 [英] Image datatype problem
问题描述
你好
我在sql表中有一个图像数据类型.我正在通过以下几行添加值.
Hello
I have an image datatype in sql table. and i am adding values through folowing lines.
cmd.Parameters.Add("@Image", SqlDbType.Image).Value = convert.tobyte(txtImage.Text.Trim())
现在由于图像,我遇到了转换功能错误
从"System.String"到"System.Byte []"的转换无效.
请帮忙
我有图片上传功能.正确.
请谁能帮我这个忙.
谁能给我上面一行的正确代码.
Now because of the image i am getting convert function error
Invalid cast from ''System.String'' to ''System.Byte[]''.
Please help
I have a upload function for the image. which is correct.
Please can anyone help me with this.
can anyone give me the correct code for the above line.
推荐答案
尝试以下代码:
Try below code:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
现在将字节数组保存在数据库中.
在将图像保存到数据库时调用上述方法:
Now save the byte array in the database.
Call the above method while saving image in the database:
command.Parameters.Add("@IMAGE", SqlDbType.VarBinary).Value = ConvertImageToByteArray(PictureBox1.Image);
从数据库中检索图像时,再次将字节数组转换为图像.
While retriving the image from the database, again convert the byte array to image.
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
朋友,
检查轻松的代码,这对我有用....
Hi friend,
Check the fallowing code, this is working for me....
if (fileupload1.HasFile)
{
byte [] productImage = fileupload1.FileBytes;
con.Open();
SqlCommand cmd = new SqlCommand("SP_SetUserDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", txtuname.Text);
cmd.Parameters.AddWithValue("@EmailID", txtEmail.Text);
cmd.Parameters.AddWithValue("@Password", txtpass.Text);
cmd.Parameters.AddWithValue("@Image", productImage);
cmd.Parameters.AddWithValue("@Hobbies", str);
cmd.Parameters.AddWithValue("@country", txtcountry.Text);
cmd.Parameters.AddWithValue("@Status", sts.ToString());
//cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Gridview1.DataSource = ds;
//Gridview1.DataBind();
BindGrid();
//con.Close();
}
这篇关于图像数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!