• 二进制数据将图片保存到数据库,并读取数据库二进制数据显示图片 墨尔本


    一. 浏览图片

    OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = @"E:\";
                ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files(*.*)|*.*";
                ofd.RestoreDirectory = true;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    picAddress = ofd.FileName;
                    Image imge = Image.FromFile(picAddress);
                    Bitmap bm = new Bitmap(imge, picBox.Width, picBox.Height);
                    picBox.Image = bm;
                }

    二.保存图片到数据库

    读取数据库并取得需要保存图片的字段,将图片转换为二进制数据保存至数据库中

     Byte[] pic = GetContent(图片的路径);
                    Comyindfo.Logo = pic;//数据库保存的字段

      /// <summary>
            /// 将图片的文件转化成二进制数据保存到数据库
            /// </summary>
            /// <param name="filepath"></param>
            /// <returns></returns>

    public static Byte[] GetContent(string filepath)//将指定路径下的文件转换成二进制代码,用于传输到数据库
            {
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                Byte[] byData = new Byte[fs.Length];//新建用于保存文件流的字节数组
                fs.Read(byData, 0, byData.Length);//读取文件流
                fs.Close();
                return byData;
            }

    三.从数据库读取二进制数据并显示图片

     byte[] imagebytes = item.Logo;//读取数据库的字段
                    MemoryStream ms = new MemoryStream(imagebytes);
                    Bitmap bmpt = new Bitmap(ms);
                    pictureBox1.Image = bmpt;

  • 相关阅读:
    POJ 2756 Autumn is a Genius 大数加减法
    RoboGuice注入框架简单应用
    Android 从相冊获取近期拍摄的多张照片(获取相机拍照所存储的照片)
    不可不知的DIP、IoC、DI以及IoC容器
    Codeforces Round #156 (Div. 2)---A. Greg&#39;s Workout
    zend framework将zip格式的压缩文件导入并解压到指定文件
    TreeSet排序
    Bee Framework_百度百科
    duck
    anglehack参赛总结
  • 原文地址:https://www.cnblogs.com/zenglishan/p/7347859.html
Copyright © 2020-2023  润新知