1.图片操作笔记,只是简单的Demo,设计图片读取本地,保存本地、读取数据库流,将图片转换流保存到数据库中,因为Oracle我电脑没安装,所以用SQLServer,原理都一样。
备注:控件为封装控件,原生态会报错,都差不多。
上代码
/************************************************* * 描述: * * Author:YS * Date:2020/5/15 19:14:42 * Update: * ************************************************/ using CoreX.Ctrls.BaseForm; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class ImageOperation : DetailForm { #region Private Fields #endregion #region Properties #endregion #region Events #endregion #region Constructors public ImageOperation() { InitializeComponent(); } #endregion #region Methods /// <summary> /// 选择本地图片 /// </summary> private void OpenLocal() { OpenFileDialog OpenFileDialog1 = new OpenFileDialog(); OpenFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|bmp文件(*.bmp)|*.bmp|ico文件(*.ico)|*.ico|png文件(*.png)|*.png|gif文件(*.gif)|*.gif"; if (OpenFileDialog1.ShowDialog() == DialogResult.OK) { Bitmap image = new Bitmap(OpenFileDialog1.FileName); pictureBox1.Image = image; } } /// <summary> /// 图片保存本地 /// </summary> private void SaveLocal() { if (pictureBox1.Image == null) { MessageBox.Show("没有加载图片", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { string str; Bitmap box1 = new Bitmap(pictureBox1.Image); SaveFileDialog SaveFileDialog1 = new SaveFileDialog(); SaveFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|bmp文件(*.bmp)|*.bmp|ico文件(*.ico)|*.ico|png文件(*.png)|*.png|gif文件(*.gif)|*.gif"; SaveFileDialog1.ShowDialog(); str = SaveFileDialog1.FileName; if(str!=null && str.Trim().Length>0) { box1.Save(str); } } } /// <summary> /// 图片保存到数据库(SQLserver or Oracle) /// </summary> private void SaveDataBase() { if (pictureBox1.Image == null) { MessageBox.Show("没有加载图片", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } byte[] data = PhotoImageInsert(pictureBox1.Image); //StringBuilder sql =new StringBuilder($"insert into Tb_Data(Img) Values({data})"); string sql = "insert into Tb_Data(Img) Values(@image)"; SqlParameter[] pms = new SqlParameter[] { new SqlParameter("@image",SqlDbType.Image){Value= data}, }; SqlHelper.ExecuteNonQuery(sql.ToString(), CommandType.Text, pms); MessageBox.Show("保存成功!"); } /// <summary> /// 图片转二进制 /// </summary> /// <param name="imgPhoto">图片对象</param> /// <returns>二进制</returns> public static byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //将Image转换成流数据,并保存为byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; } /// <summary> /// 读取数据库图片 /// </summary> private void GetData() { string sql = "select img from Tb_Data where id=(select MAX(id) from Tb_Data )"; using (SqlDataReader datas = SqlHelper.ExecuteReader(sql.ToString(), CommandType.Text)) { if (datas.HasRows) { while (datas.Read()) { byte[] dtResult = null; Stream sr = datas.GetStream(0); dtResult = new byte[sr.Length]; sr.Read(dtResult, 0, dtResult.Length); sr.Dispose(); Image re = BytesToImage(dtResult); pictureBox1.Image = re; } } } } /// <summary> /// 转换图片 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static Image BytesToImage(byte[] bytes) { MemoryStream ms = new MemoryStream(bytes); Image img = Image.FromStream(ms); return img; } #endregion #region Control Events /// <summary> /// 读取本地图片 /// </summary> private void btnOpn_Click(object sender, EventArgs e) { OpenLocal(); } /// <summary> /// 图片保存到本地 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveLocal_Click(object sender, EventArgs e) { SaveLocal(); } /// <summary> /// 保存到数据库 /// </summary> private void btnSaveDataBase_Click(object sender, EventArgs e) { SaveDataBase(); } /// <summary> /// 读取数据库图片 /// </summary> private void btnOpenDataBaseData_Click(object sender, EventArgs e) { GetData(); } #endregion } }