将图像控件中的图像转换为字节 [英] Convert image in image control to byte
本文介绍了将图像控件中的图像转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们,
我正在尝试将Asp.Net图像控件中的图像转换为字节,以将图像控件中的图像保存到数据库.
我尝试了以下代码,但无法正常工作,请帮帮我.
Hi friends,
I am trying to convert image in Asp.Net Image control to byte to save the image in image control to database.
I tried this following code but its not working please help me.
byte[] imagebyt = new byte[Image1.ImageUrl.Length + 1];
MemoryStream ms = new MemoryStream(imagebyt,0,imagebyt.Length);
cmd = new MySqlCommand("Insert Into db_image (Image) values (@imag)", con);
cmd.Parameters.AddWithValue("imag", imagebyt);
cmd.ExecuteNonQuery();
推荐答案
尝试一下
Try this
byte[] imagebyt = new byte[Image1.ImageUrl.FileBytes.Length+1];
imagebyt = Image1.Imageurl.FileBytes;
MemoryStream ms = new MemoryStream(imagebyt,0,imagebyt.Length);
cmd = new MySqlCommand("Insert Into db_image (Image) values (@imag)", con);
cmd.Parameters.AddWithValue("imag", ms.GetBuffer());
cmd.ExecuteNonQuery();
您的问题是您设置了字节数组的长度,但实际上并没有告诉他们将图像字节放在那里
Your problem was that you set the length of the byte array but didnt actually tell them to put your image bytes there
这篇关于将图像控件中的图像转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文