c#保存图片至SQL数据库

版权声明:当老鼠嘲笑猫的时候,身旁必有一个洞。 https://blog.csdn.net/qq_41138935/article/details/85407723
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图片
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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
                {
                    //读成二进制
                    byte[] bytes = File.ReadAllBytes(openfile.FileName);
                    //直接返这个存储到数据就行了cmd.Parameters.Add("@image", SqlDbType.Image).Value = bytes;

                    //输出二进制 在这里把数据中取到的值放在这里byte[] bytes=(byte[])model.image;
                    pictureBox1.Image = System.Drawing.Image.FromStream(new MemoryStream(bytes));
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

                    // 如果保存成文件:
                    //File.WriteAllBytes(@"d:\text.jpg", bytes);
                }
                catch { }
            }
        }
    }
}

放一个picbox和button。

猜你喜欢

转载自blog.csdn.net/qq_41138935/article/details/85407723
今日推荐