存储图片代码:
private void buttonX4_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Title = "请选择图片";
openfile.Filter = "图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if (DialogResult.OK == openfile.ShowDialog())
{
try
{
Bitmap bmp = new Bitmap(openfile.FileName);
pictureBox1.Image = bmp;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
pic = Convert.ToBase64String(arr); }
catch { }
} private void buttonX1_Click(object sender, EventArgs e)
{
mymeans.getsqlcom("insert into tb_student values ('" + textBoxX1.Text + "','" + textBoxX2.Text + "','" + textBoxX3.Text + "','" + comboBoxEx1.Text + "','" + textBoxX4.Text + "','" + textBoxX5.Text + "','" +pic + "','" + textBoxX6.Text + "','" + textBoxX7.Text + "')");}读取图片代码:
mymeans.conn_open();
dt=mymeans.getDataTable("select Photo from tb_student where StuId='"+textBoxX1.Text+"'");
pic = dt.Rows[0].ItemArray[0].ToString());
try
{
if (!string.IsNullOrEmpty(pic))
{
byte[] imageBytes = Convert.FromBase64String(pic);
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(memoryStream);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox1.Image = image;
}
}
catch { }}
大家给我看看 :
数据库中Photo字段为image类型
存储图片后出现 《二进制数据》但在读取数据的时候不显示图片
if (!string.IsNullOrEmpty(pic))//这句执行了//这一段好像没有执行
byte[] imageBytes = Convert.FromBase64String(pic);
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(memoryStream);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox1.Image = image;求教!求教大侠!
if (!string.IsNullOrEmpty(pic))
{ MessageBox.Show("你好!");//这句执行了 byte[] imageBytes = Convert.FromBase64String(pic); MessageBox.Show("你好!");//这句没有执行 MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(memoryStream);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox1.Image = image; }
去掉try....catch后出现以下错误:
输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非空白字符。
麻烦大侠给我看看怎么修改才正确!
pic = dt.Rows[0].ItemArray[0].ToString();
求大侠教我如歌修改这一句!