winform保存图片到数据库
/// <summary>
/// 保存
/// </summary>
private void btn_SaveLogo_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbx_LogoPath.Text) && File.Exists(tbx_LogoPath.Text))
{
try
{
byte[] imageByte = null;
FileStream fs = new FileStream(tbx_LogoPath.Text, FileMode.Open, FileAccess.Read);//图片转为数据流
imageByte = new byte[fs.Length];//定义数据长度
fs.Read(imageByte, 0, imageByte.Length);//读取数据流到数组
fs.Close();
string imageStream = Convert.ToBase64String(imageByte); //数据转字符串类型
CustomerMethod.SetParamName("logoPath", imageStream);//保存到数据库
MessageBox.Show("保存成功");
frmMain._logoUpdate = true;
}
catch
{
MessageBox.Show("保存失败");
}
}
else
{
MessageBox.Show("文件不存在!");
}
}
/// <summary>
/// 显示
/// </summary>
private void DisplayImage()
{
string logoPath = CustomerMethod.GetParamValue("logoPath");
if (!string.IsNullOrEmpty(logoPath))
{
byte[] imageByte = Convert.FromBase64String(logoPath);
MemoryStream ms = new MemoryStream(imageByte);
pb_Logo.Image = Image.FromStream(ms);
}
}

浙公网安备 33010602011771号