zoukankan      html  css  js  c++  java
  • 二进制图片、二进制pdf 简单的数据库和客户端的交互

    本文讲述三点:

    1.二进制图片 数据库和客户端的交互

    2.二进制pdf 数据库和客户端的交互

    3.一个简单的winform打开pdf的方案

    前几天辞职了么事做  去书店看书 看到二进制图片上传觉得很简单

    图片和pdf都可以显示到winfomr窗体 并且可以下载到本地

    数据库中存image和pdf的字段都是 image类型的

    1.二进制图片 数据库和客户端的交互(下载到本地参考pdf那个例子)

     private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    //网址不支持  只支持本地文件
                    string s = @"E:\MyCode\erjinzhi\erjinzhi\erjinzhi\image\youcai.jpg";
                    FileStream f = new FileStream(s, FileMode.Open, FileAccess.Read);
                    BinaryReader b = new BinaryReader(f);
                    byte[] byteImage = b.ReadBytes((int)f.Length);
    
                    //保存到数据库中 SqlDbType.Image类型
                    SqlConnection conn = new SqlConnection("server=.;uid=sa;password=sa;database=test1");
                    SqlCommand com = new SqlCommand();
                    com.Connection = conn;
                    com.CommandType = CommandType.Text;
                    com.CommandText = "insert into [user] values(@byteImage)";
                    com.Parameters.Add("@byteImage", SqlDbType.Image).Value = byteImage;
                    conn.Open();
                    int result=com.ExecuteNonQuery();
                    conn.Close();
                    if (result > 0)
                    {
                        com = new SqlCommand();
                        com.Connection = conn;
                        com.CommandText = "select image from [user] where id=@id";
                        com.CommandType = CommandType.Text;
                        com.Parameters.Add("@id", SqlDbType.Int).Value = 1;
                        conn.Open();
                        object obj = com.ExecuteScalar();
                        conn.Close();
                        byteImage = (byte[])obj;
                        MemoryStream m = new MemoryStream(byteImage);
                        //给图片控件设置image
                        pictureBox2.Image = Image.FromStream(m);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

    2.二进制PDF 数据库和客户端的交互(显示到界面参考下一个实例)

    //可以下载到本地之后 把本地地址赋值给axAcroPDF1.src

     private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    //pdf如果大的话 你要把数据库超时时间设置大点 我尝试100M的pdf 结果超时错误
                    //pdf加密的话好像也会报错 没有试
                    string s = @"F:\桌面\firebug-Firefox_FireBug_调试技巧.pdf";
                    FileStream f = new FileStream(s, FileMode.Open, FileAccess.Read);
                    BinaryReader b = new BinaryReader(f);
                    byte[] bytePdf = b.ReadBytes((int)f.Length);
    
                    //保存到数据库中 SqlDbType.Image类型
                    SqlConnection conn = new SqlConnection("server=.;uid=sa;password=sa;database=test1");
                    SqlCommand com = new SqlCommand();
                    com.Connection = conn;
                    com.CommandType = CommandType.Text;
                    com.CommandText = "insert into [user] values(@bytePdf)";
                    com.Parameters.Add("@bytePdf", SqlDbType.Image).Value = bytePdf;
                    conn.Open();
                    int result = com.ExecuteNonQuery();
                    conn.Close();
                    if (result > 0)
                    {
                        com = new SqlCommand();
                        com.Connection = conn;
                        com.CommandText = "select image from [user] where id=@id";
                        com.CommandType = CommandType.Text;
                        com.Parameters.Add("@id", SqlDbType.Int).Value = 6;
                        conn.Open();
                        object obj = com.ExecuteScalar();
                        conn.Close();
                        //pdf读取数据库中的二进制 然后报错到本地
                        //参考:http://topic.csdn.net/u/20080415/11/048deb7e-b0c2-4a20-94a5-88be74ce47f0.html
                        bytePdf = (byte[])obj;
                        string filepath = @"E:\xxx.pdf";
                        FileStream fs;
                        if (File.Exists(filepath))
                        {
                            fs = new FileStream(filepath, FileMode.Truncate);
                        }
                        else
                        {
                            fs = new FileStream(filepath, FileMode.CreateNew);
                        }
                        BinaryWriter br = new BinaryWriter(fs);
                        br.Write(bytePdf, 0, bytePdf.Length);
                        br.Close();
                        fs.Close();
                        Process.Start(filepath);
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                
            }

    3.pdf显示到winform窗体的简单的方案

     //在winform窗体中查看PDF
               // 打开工具箱 
                //右键 选择项  com组建选择adobe pdf reader 
                //设置pdf地址src
                //http://dingxq.blog.163.com/blog/static/13052576120108805924575/
                axAcroPDF1.src = @"E:\xxx.pdf";
  • 相关阅读:
    EasyNVR摄像机网页无插件直播方案H5前端构建之:通道内部搜索功能的实现方案与代码
    EasyNVR摄像机网页无插件直播方案H5前端构建之:bootstrap弹窗功能的实现方案与代码
    EasyNVR摄像机网页无插件直播方案H5前端构建之:bootstrap-datepicker日历插件的实时动态展现
    EasyNVR摄像机网页无插件直播方案H5前端构建之:如何播放HLS(m3u8)直播流
    EasyNVR摄像机网页无插件直播方案H5前端构建之:区分页面是自跳转页面还是分享页面
    EasyNVR摄像机网页无插件直播方案H5前端构建之:如何区分PC端和移动端
    EasyDSS高性能流媒体服务器开发RTMP直播同步输出HLS(m3u8)录像功能实现时移回放的方案
    Stack Overflow: The Architecture
    The week in .NET
    十分钟轻松让你认识Entity Framework 7
  • 原文地址:https://www.cnblogs.com/0banana0/p/2487562.html
Copyright © 2011-2022 走看看