如何将图片转化为二进制数组

a755_ylzl 2014-07-15 08:27:25

using (SqlConnection cn = new SqlConnection("Server=.;UID=sa;Pwd=123456;Database=hzmkma_crm"))
{
using (SqlCommand cmd = new SqlCommand(" UPDATE makam_admin SET portrait=@pic where username=@username", cn))
{
cmd.Parameters.Add("@username", SqlDbType.NChar).Value = UserHelper.uuname;
cmd.Parameters.Add("@pic", SqlDbType.Image).Value = new byte[] { };
cn.Open();
int num= cmd.ExecuteNonQuery();
if (num > 0)
{
MessageBox.Show("图片插入成功!");
}
}
}




是 winfrom 不是web 是窗体 不是页面

cmd.Parameters.Add("@pic", SqlDbType.Image).Value = new byte[] { }; 这里要怎么操作才能正确才能把PictureBox的值 获取出来?



...全文
781 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
大古队员 2015-09-28
  • 打赏
  • 举报
回复
@Cnwanglin 草泥马
Cnwanglin 2014-07-15
  • 打赏
  • 举报
回复
Cnwanglin 2014-07-15
  • 打赏
  • 举报
回复
讲的这么详细都不行 就差我替你干活了
a755_ylzl 2014-07-15
  • 打赏
  • 举报
回复
FileStream fs = new FileStream(@"C:/e06d3510ebfde555cb80c4de.jpg", FileMode.Open); 后面要不是固定的图片 要怎么改呢? "C:/e06d3510ebfde555cb80c4de.jpg", 改成 picbox1.ImageLocation ?
a755_ylzl 2014-07-15
  • 打赏
  • 举报
回复
picturebox 去哪里了? images 是怎么出来的?
a755_ylzl 2014-07-15
  • 打赏
  • 举报
回复
什么什么啊? 确定是winform windows 窗体用的
熙风 2014-07-15
  • 打赏
  • 举报
回复

        private byte[] SetImageToByteArray(string fileName)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
                Bitmap bt = new Bitmap(fs);
                int streamLength = (int)fs.Length;
                byte[] image = new byte[streamLength];
                fs.Read(image, 0, streamLength);
                return image;
            }
            catch (Exception)
            {
                throw;

            }
            finally
            {
                fs.Close();
            }
        }
Cnwanglin 2014-07-15
  • 打赏
  • 举报
回复
picturebox.image
Cnwanglin 2014-07-15
  • 打赏
  • 举报
回复
二进制数组和图像相互转换的函数: //图像转换成二进制 private byte[] ImageToByte(Image Picture) { MemoryStream ms = new MemoryStream(); if (Picture == null) return new byte[ms.Length]; Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] BPicture = new byte[ms.Length]; BPicture = ms.GetBuffer(); return BPicture; } //二进制转换为图像 private Image ByteToImage(byte[] btImage) { if (btImage.Length == 0) return null; System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage); System.Drawing.Image image = System.Drawing.Image.FromStream(ms); return image; } 1、定义对象 public class PicClass { public List<byte[]> img { get; set; } //图片 } //生成图片的方法 PicClass pc = new PicClass(); List<byte[]> imgs = new List<byte[]>(); byte[] img = new byte[0]; img = (byte[])pic.image; //pic.image是返回的图片的二进制数组 imgs.Add(img); pc.img = imgs; Image image = ByteToImage(btimage); //将二进制数组转换成image图片 pictureBox1.Image = image; //图片显示在picturebox框里 //生成视频的方法 List<byte[]> imgs = new List<byte[]>(); myPic pic = new myPic(); //服务定义的对象 pic=client.queryVioVideo("", "", "", ""); //调用服务获取视频流 byte[] img = new byte[0]; img = (byte[])pic.video; //pic.video是接收的视频的二进制数组 PicClass pc = new PicClass(); imgs.Add(img); pc.img=imgs; File.WriteAllBytes("D://1.avi", pc.img.First()); //将视频流写入到文件里

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧