zoukankan      html  css  js  c++  java
  • C#

    1、创建数据表

    CREATE TABLE Tb_pic
    (
    	ID int primary key identity(1, 1) not null,
    	PictureBox varchar(max) 
    )
    

    运行效果:

    2、代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using System.Data.SqlClient;
    using System.Configuration;
    using System.IO;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            //定义字节数组,用来存储图片
            byte[] arr;
    
            //抽取表中的ID字段,绑定combBox数据
            public void GetID()
            {
                this.comboBox1.Items.Clear();
                string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                string sql = "select ID from Tb_pic";
                SqlConnection con = new SqlConnection(constring);
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    this.comboBox1.Items.Add(sdr["ID"].ToString());
                }
                this.comboBox1.SelectedIndex = 0;
            }
    
            /// <summary>
            /// 加载事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form1_Load(object sender, EventArgs e)
            {
                GetID();
            }
    
            
            /// <summary>
            /// 浏览事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Title = "请选择客户端longin的图片";
                openfile.Filter = "Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
                if (DialogResult.OK == openfile.ShowDialog())
                {
                    try
                    {
                        Bitmap bmp = new Bitmap(openfile.FileName);
                        pictureBox1.Image = bmp;
                        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                        MemoryStream ms = new MemoryStream();
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                        arr = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(arr, 0, (int)ms.Length);
                        ms.Close();
                    }
                    catch { }
                }
            }
    
            /// <summary>
            /// 保存事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                if (arr == null)
                {
                    MessageBox.Show("照片为空!","提示");
                    return;
                }
    
                //直接返这个值放到数据就行了
    
                string sql = "insert into Tb_pic (PictureBox) values (@pic)";
    
    
                SqlParameter[] para = new SqlParameter[]
                        {
                            new SqlParameter("@pic", Convert.ToBase64String(arr))
                        };
    
                string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    
                SqlConnection con = new SqlConnection(constring);
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(para);
                int i = cmd.ExecuteNonQuery();
                con.Close();
    
                if (i == 1)
                {
                    MessageBox.Show("添加成功!", "提示");
                    GetID();
                }
                else
                {
                    MessageBox.Show("添加失败!", "提示");
                }
            }
    
            /// <summary>
            /// 显示事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, EventArgs e)
            {
                if (this.comboBox1.Items.Count <= 0)
                {
                    MessageBox.Show("无数据!", "提示");
                    return;
                }
    
                try
                {
                    string sql = "select PictureBox from Tb_pic where ID = '" + this.comboBox1.SelectedItem.ToString() + "'";
                    string constring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    
                    SqlConnection con = new SqlConnection(constring);
                    con.Open();
                    SqlCommand cmd = new SqlCommand(sql, con);
    
                    string pic = (string)cmd.ExecuteScalar();
    
                    // pic=........这一句换成从数据库里读取就可以了
                    //判断是否为空,为空时的不执行
                    if (!string.IsNullOrEmpty(pic))
                    {
                        //直接返Base64码转成数组
                        byte[] imageBytes = Convert.FromBase64String(pic);
                        //读入MemoryStream对象
                        MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
                        memoryStream.Write(imageBytes, 0, imageBytes.Length);
                        //转成图片
                        Image image = Image.FromStream(memoryStream);
    
                        //memoryStream.Close();//不要加上这一句否则就不对了
    
                        // 将图片放置在 PictureBox 中
                        this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                        this.pictureBox2.Image = image;
                    }
                }
                catch { }
            }
    
            /// <summary>
            /// 取消选中图片事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button4_Click(object sender, EventArgs e)
            {
                arr = null;
                this.pictureBox1.Image = null;
            }
        }
    }
    

    参考链接:http://www.sufeinet.com/thread-1261-1-1.html

  • 相关阅读:
    系统UINavigationController使用相关参考
    iOS工程集成支付宝错误Undefined symbols for architecture armv7
    Xcode工程使用CocoaPods管理第三方库新建工程时出现错误
    Xcode报错:“Your build settings specify a provisioning profile with the UUID..... however, no such provisioning profile was found”
    iOS开发问题之Could not instantiate class named NSLayoutConstraint
    UICollectionView使用相关博文链接
    UIRefreshControl的使用
    iOS沙盒路径的查看和使用
    【汇总】涉及iOS&iPhone开发相关文章汇总
    【汇总】iOS开发及Xcode使用中遇到的一些报错问题汇总
  • 原文地址:https://www.cnblogs.com/KTblog/p/4593065.html
Copyright © 2011-2022 走看看