Winfin图片保存
前言
我们都知道,搞编程的的人,一般的话,都是会文件和数据的基本操作,因为这个是每一门编程语言的入门基础了,如果就是连文件的基本操作都掌握不了的话,那么就是对编程入门的要求都没有达到,如果还想要了解的话,那么请看下文,而今天我主要写的这一篇文章就是和图片的基本操作有关。
(1)搭建三层架构
三层架构是什么我在这里就不必多说了,如果想了解的话,那么请访问: https://blog.****.net/qqj3066574300/article/details/81410136
,这个也是一些带入不懂三层的人入门而已。如果想更深的了解的话,那么自己可以去网上查看别人是什么解释的,反正都是学习,多看一些好的文章,对自己的见识也是有帮助的。
(2)编写实体类(Model)
Phone实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// 图片信息
/// </summary>
public class Phone
{
/// <summary>
/// 无参构造
/// </summary>
public Phone()
{
}
/// <summary>
/// 有参构造
/// </summary>
/// <param name="phoneId">相册ID</param>
/// <param name="phoneName">相册名称</param>
/// <param name="phoneClass">相册类型</param>
/// <param name="phonePath">相册路径</param>
/// <param name="phoneReak">相册简介</param>
/// <param name="data">相册入存时间</param>
public Phone(string phoneId, string phoneName, string phoneClass, string phonePath, string phoneState,string phoneReak, DateTime data)
{
PhoneId = phoneId;
PhoneName = phoneName;
PhoneClass = phoneClass;
PhonePath = phonePath;
PhoneState = phoneState;
PhoneReak = phoneReak;
Data = data;
}
/// <summary>
/// 相册ID
/// </summary>
public string PhoneId { get; set; }
/// <summary>
/// 相册名称
/// </summary>
public string PhoneName { get; set; }
/// <summary>
/// 相册类型
/// </summary>
public string PhoneClass { get; set; }
/// <summary>
/// 相册路径
/// </summary>
public string PhonePath { get; set; }
/// <summary>
///图片状态
/// </summary>
public string PhoneState { get; set; }
/// <summary>
/// 相册简介
/// </summary>
public string PhoneReak { get; set; }
/// <summary>
/// 相册入存时间
/// </summary>
public DateTime Data { get; set; }
}
}
(2)编写数据访问层(DAL)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//添加引用
using Model;
using MySql.Data.MySqlClient;
namespace DAL
{
/// <summary>
/// 相册
/// </summary>
public class PhoneDAL
{
/// <summary>
/// 方法:添加相册
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public int PhoneAdd(Phone phone)
{
MySqlParameter[] parameters = new MySqlParameter[]
{
new MySqlParameter("PhoneName",phone.PhoneName),
new MySqlParameter("PhoneClass",phone.PhoneClass),
new MySqlParameter("PhonePath",phone.PhonePath),
new MySqlParameter("PhoneState","正常"),
new MySqlParameter("PhoneReak",phone.PhoneReak),
new MySqlParameter("Data",DateTime.Now)
};
return DBHelper.ExecuteNonQuery("insert into Phone(PhoneId,PhoneName,PhoneClass,PhonePath,PhoneState,PhoneReak,Data) values (uuid(),@PhoneName,@PhoneClass,@PhonePath,@PhoneState,@PhoneReak,@Data)", parameters);
}
}
}
DAL层中,我用到了DBHelper类,如果没有DBHelper的话,那么请链接:https://blog.****.net/qqj3066574300/article/details/88918104
(3)业务逻辑层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//添加引用
using Model;
using DAL;
namespace BLL
{
public class PhoneBLL
{
//实例化PhoneDAL
PhoneDAL cd = new PhoneDAL();
/// <summary>
/// 方法:添加相册
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public int PhoneAdd(Phone phone)
{
return cd.PhoneAdd(phone);
}
}
}
(4)编写插入图片的功能
这个是界面
如下是整个界面的代码
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.IO;
using Model;
using BLL;
namespace Phone
{
public partial class frmAddPhone : Form
{
public frmAddPhone()
{
InitializeComponent();
}
/// <summary>
/// 选择图片预览
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Model.Phone phone = new Model.Phone();//实例化Phone
PhoneBLL cd = new PhoneBLL();//实例化PhoneBLL
string image = "";
private void btnImg_Click(object sender, EventArgs e)
{
//创建文件弹出选择窗口(包括文件名)对象
OpenFileDialog openFileDialog = new OpenFileDialog();
//判断选择的路径
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//PictureBox控件显示图片
PicImg.Load(openFileDialog.FileName);
//获取用户选择文件的后缀名
string extension = Path.GetExtension(openFileDialog.FileName);
//声明允许的后缀名
string[] str = new string[] { ".gif", ".jpge", ".jpg", ".png" };
if (!str.Contains(extension))
{
MessageBox.Show("仅能上传gif,jpge,jpg格式的图片!");
}
else
{
//获取用户选择的文件,并判断文件大小不能超过20K,fileInfo.Length是以字节为单位的
FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
if (fileInfo.Length > 2000 * 1024 * 1024)
{
MessageBox.Show("上传的图片不能大于2M");
return;
}
else
{
//获取当前文件夹路径
string currPath = Application.StartupPath;
//检查是否存在文件夹
string subPath = currPath + "/pic/";
if (Directory.Exists(subPath))
{
Directory.CreateDirectory(subPath);
}
subPath = image + extension;
image += extension;
PicImg.Image.Save(Application.StartupPath + "\\pic\\" + image);
}
}
}
}
/// <summary>
/// 提交按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUp_Click(object sender, EventArgs e)
{
if (PicImg.Image == null)
{
MessageBox.Show("图片不能为空!");
return;
}
if (txtName.Text == "")
{
MessageBox.Show("图片名称不能为空!");
return;
}
if (comClass.Text == "")
{
MessageBox.Show("图片类型不能为空!");
return;
}
if (rbtReak.Text == "")
{
MessageBox.Show("图片简介不能为空!");
return;
}
phone.PhoneName = txtName.Text.Trim();
phone.PhoneClass = comClass.Text.Trim();
phone.PhoneReak = rbtReak.Text.Trim();
phone.PhonePath = image;
cd.PhoneAdd(phone);
MessageBox.Show("添加成功!");
Click();
}
/// <summary>
/// 取消按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClick_Click(object sender, EventArgs e)
{
Click();//清空控件上所有的内容
}
/// <summary>
/// 清空控件内容
/// </summary>
public void Click()
{
PicImg.Image = null;
txtName.Text = "";
rbtReak.Text = "";
comClass.Text = "";
}
}
}
写完这个,就可以去项目中的pic文件夹里面看看图片没有成功的加入进去,如果pic文件夹里面有刚刚加入的图片,那么再到数据库中查看是否有自己刚刚添加的数据,如果有的话,那么图片,和文字同时添加的功能就可以实现了。
尾言
该说的也说了,改写的也写了,希望这一篇小小的文章能够帮助到有困惑的编程爱好者,如果觉得这一篇文章写得好,或者有帮助的话,记得帮我点个赞。