winform中,上传图片然后将图片以二进制形式 保存到数据库中怎么做?
展开全部
下面有一段代码,在我一个项目里抽出来的,没有完全T出来,你应该看得懂了:
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
ofdSelectPic.DefaultExt = "*.bmp;*.jpg;*.jpeg;*.gif;*.png;";
ofdSelectPic.Filter = "*.jpg,*.gif,*.bmp,*.png,*.jpeg|*.jpg;*.gif;*.bmp;*.png;*.jpeg";
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
string filepath = ofdSelectPic.FileName;
string extension = Path.GetExtension(filepath);
if (extension == ".jpg" || extension == ".gif" || extension == ".bmp" || extension == ".jpeg" || extension == ".png")
{
if (SelectImageIndex == 1)
{
picSignature.Image = Image.FromFile(filepath);
}
else if (SelectImageIndex == 2)
{
picphotoid1.Image = Image.FromFile(filepath);
}
else if (SelectImageIndex == 3)
{
picphoto2.Image = Image.FromFile(filepath);
}
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
ms.Close();
return picbyte;
}
else
{
MessageBox.Show("you can only select the image type file!");
return null;
}
}
else
{
return null;
}
}
那个SQL数据库里字段设置成image或者binary都行。
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
ofdSelectPic.DefaultExt = "*.bmp;*.jpg;*.jpeg;*.gif;*.png;";
ofdSelectPic.Filter = "*.jpg,*.gif,*.bmp,*.png,*.jpeg|*.jpg;*.gif;*.bmp;*.png;*.jpeg";
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
string filepath = ofdSelectPic.FileName;
string extension = Path.GetExtension(filepath);
if (extension == ".jpg" || extension == ".gif" || extension == ".bmp" || extension == ".jpeg" || extension == ".png")
{
if (SelectImageIndex == 1)
{
picSignature.Image = Image.FromFile(filepath);
}
else if (SelectImageIndex == 2)
{
picphotoid1.Image = Image.FromFile(filepath);
}
else if (SelectImageIndex == 3)
{
picphoto2.Image = Image.FromFile(filepath);
}
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
ms.Close();
return picbyte;
}
else
{
MessageBox.Show("you can only select the image type file!");
return null;
}
}
else
{
return null;
}
}
那个SQL数据库里字段设置成image或者binary都行。
展开全部
public Byte[] getphoto(string photopath)
{
string str=photopath;
FileStream file=new FileStream(str,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(file);
Byte[] bytBLOBData=br.ReadBytes((int)file.length);
br.Close();
file.Close();
return bytBLOBData;
}
{
string str=photopath;
FileStream file=new FileStream(str,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(file);
Byte[] bytBLOBData=br.ReadBytes((int)file.length);
br.Close();
file.Close();
return bytBLOBData;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public Byte[] getphoto(string photopath)
{
string str=photopath;
FileStream file=new FileStream(str,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(file);
Byte[] bytBLOBData=br.ReadBytes((int)file.length);
br.Close();
file.Close();
return bytBLOBData;
{
string str=photopath;
FileStream file=new FileStream(str,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(file);
Byte[] bytBLOBData=br.ReadBytes((int)file.length);
br.Close();
file.Close();
return bytBLOBData;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询