日期:2014-05-17  浏览次数:21375 次

c# 图片转换成二进制,二进制转换成图片
我想把图片转换成二进制存到数据库里

------解决方案--------------------
大概是这样的

 Image _Image = Image.FromFile(@"C:\1.jpg");

System.IO.MemoryStream _ImageMem = new System.IO.MemoryStream();
_Image.Save(_ImageMem, ImageFormat.Bmp);
byte[] _ImageBytes = _ImageMem.GetBuffer();
//获取流 这里其实应该是直接用文件方式获取..如System.IO.File.ReadAllBytes(@"C:\1.jpg") 

SqlCommand _SqlCommand = new SqlCommand("Insert into ImageTable(name,image)values(@name,@image)");
_SqlCommand.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 20));
_SqlCommand.Parameters.Add(new SqlParameter("@image", SqlDbType.Image));

_SqlCommand.Parameters[0].Value = "ImageName";
_SqlCommand.Parameters[1].Value = _ImageBytes;
执行这个SQLCOMMAND
------解决方案--------------------
C# code

    public static class BitmapHelper
    {
        public static Bitmap BytesToBitmap(byte[] Bytes)
        {
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream(Bytes);
                return new Bitmap((Image)new Bitmap(stream));
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        public static byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms, Bitmap.RawFormat);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }
    }

------解决方案--------------------
C# code
//保存图片: 
                   SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");  
                conn.Open();  
                SqlCommand cmd = new SqlCommand("insert into image values(@i)", conn);  
                byte[] ib = new byte[60000];  
                FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read);  
                fs.Read(ib, 0, 60000);  
                cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);  
                cmd.Parameters["@i"].Value = ib;  
                cmd.ExecuteNonQuery();  
                conn.Close();  
                MessageBox.Show("保存成功");  
//显示图片: 
              SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");  
            conn.Open();  
            SqlCommand cmd = new SqlCommand("select image1 from image", conn);  
            SqlDataReader reader = cmd.ExecuteReader();  
            reader.Read();  
            while (reader.Read())  
            {  
                for (int i = 0; i  < reader.FieldCount; i++)  
                {  
                    MemoryStream buf = new MemoryStream((byte[])reader[i]);  
                    Image image = Image.FromStream(buf,true);  
                    this.pictureBox1.Image = image;  
                }  
            }

------解决方案--------------------
1.任何文件以二进制存入Access数据库:
……
if (opFlDlg.ShowDialog() == DialogResult.OK)
{
Stream fl = null;