如何将bytes []转换为图像? [英] how to convert bytes[] to image?
本文介绍了如何将bytes []转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,但我有一个错误,参数(MS)无效。并且我在最后使用了该函数,但我再次发现该参数无效的错误!
This is my code, but i have an error that , the parameter(MS) is not valid. and i used the function at the last , but again i had that error that parameter is not valid!
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + bankaddress);
OleDbCommand com = new OleDbCommand();
con.Open();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "SELECT [Attachment],[FileName] From Dan_Attach WHERE ID=94";
OleDbDataReader Reader2 = com.ExecuteReader();
while (Reader2.Read())
{
MessageBox.Show(Reader2.GetValue(1).ToString());
byte[] pic = new byte[2048];
Reader2.GetBytes(0,0,pic,0,2048);
MemoryStream MS = new MemoryStream(pic);
Image returnImage = Image.FromStream(MS);
pictureBox1.Image = returnImage;
con.Close();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
推荐答案
看看我是怎么做的:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 你不能只加载一小部分数据并假设它是一个有效的图像 - 它不是'' t。
Have a look at how I do it: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - you can''t just load a small protion of the data and assume that is a valid image - it isn''t.
请参阅Prasad Khandekar对该问题的评论。您可以在System.Data.DataTableReader.GetBytes
上的MSDN帮助页面中看到此技术的示例:
http://msdn.microsoft.com/en-us/library/system.data.datatablereader.getbytes .aspx [ ^ ]。
当您开始硬编码所有直接常数时,您的方向错误索引,连接字符串和查询,但硬编码2048无法用任何逻辑解释。
这不是存储和检索图像的唯一方法。请检查: http://www.codeproject .com / search.aspx?q =%28database + OR + SQL%29 +%28image + OR + images%29 [ ^ ] :-)。
-SA
Please see the comment to the question by Prasad Khandekar. You can see the at the example of this technique in the MSDN help page onSystem.Data.DataTableReader.GetBytes
:
http://msdn.microsoft.com/en-us/library/system.data.datatablereader.getbytes.aspx[^].
Your went in a wrong directions when you started to hard-code all of your immediate constants like table index, connection string and query, but hard-code 2048 cannot be explained by any logic.
This is not the only way to store and retrieve images. Please check: http://www.codeproject.com/search.aspx?q=%28database+OR+SQL%29+%28image+OR+images%29[^] :-).
—SA
这篇关于如何将bytes []转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文