zoukankan      html  css  js  c++  java
  • 8、图片的上传与读取

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace WindowsFormsApplication1
    {
        public partial class FormImage : Form
        {
            public FormImage()
            {
                InitializeComponent();
            }
         //上传
            private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif"; 
                DialogResult dia = openFileDialog1.ShowDialog();
                if (dia == DialogResult.OK)
                {
                    string filename = openFileDialog1.FileName;
                    FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read);//将图片读入流中
                    byte[] imagebytes = new byte[fs.Length];//二进制数组,用以临时存储图像的二进制编码
                    BinaryReader br = new BinaryReader(fs);//二进制读取器
                    imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//将图片读入到二进制数组中
                    //开始连接数据库存入数据库中
                    SqlConnection conn = new SqlConnection("server=.;database=schoolData;user=sa;pwd=");
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into tutable values(@image)";
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@image",imagebytes);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("图片上传成功");
                }
            }
            //读取
            private void button2_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection("server=.;database=schoolData;user=sa;pwd=");
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select top 1 *from tutable";
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                byte[] imgbytes = (byte[])dr["tuxiang"];
                //将图像写入内存
                MemoryStream ms = new MemoryStream(imgbytes, 0, imgbytes.Length);
                ms.Write(imgbytes, 0, imgbytes.Length);
                Image img = Image.FromStream(ms);
                this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                this.pictureBox1.Image = img;
            }
        }
    }
  • 相关阅读:
    PyDev for eclipse 插件下载地址
    Impala SQL 语言元素(翻译)[转载]
    oracle9i-11.2安装包及补丁包下载链接
    oracle link的创建过程
    Oracle 查询历史数据(转帖)
    Alpha、Beta、RC、GA版本的区别
    oracle wm_concat(column)函数的使用
    Quest.Central.for.DB2.v5.0.2.4下载地址
    Hadoop 管理工具HUE配置-初始配置
    maven编译下载源码
  • 原文地址:https://www.cnblogs.com/XMH1217423419/p/4300037.html
Copyright © 2011-2022 走看看