C#中WINFORM开发获取图片二进制保存并在DataGridView显示的方法
调用OpenFileDialog打开选择图片并获取图片二进制保存到数据库
#region 图片转二进制 002
OpenFileDialog ofd= new OpenFileDialog();
ofd.Title = "请选择图片";
ofd.InitialDirectory = Path.GetDirectoryName(ofd.FileName);
ofd.Filter = "(*.jpg/png)|*.jpg;*.png";
ofd.Multiselect = false;
if (ofd.ShowDialog().Equals(DialogResult.OK))
{
string path=ofd.FileName;
byte[] imageBytes = File.ReadAllBytes(path);
string str =@"update componentsupplier set cslogo=@fs ";
DbHelperPgSql.ExecuteSqlInsertImg(str,imageBytes);
}
#endregion
在DataGridView中指定的字段展示图片
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if(e.RowIndex>=0 && e.ColumnIndex >= 0)
{
if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("cslogo"))
{
if (e.Value != null)
{
// 转换为 Image
using (MemoryStream ms = new MemoryStream((byte[])e.Value))
{
Image image = Image.FromStream(ms);
e.Value = image;
}
}
}
}
}
catch
{
}
}
查看更多关于C#中WINFORM开发获取图片二进制保存并在DataGridView显示的方法的详细内容...