文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#将照片或图片转化为byte[]存入数据库,从数据库中读照片

C#将照片或图片转化为byte[]存入数据库,从数据库中读照片

时间:2010-09-09  来源:悠冉

1. 写入数据库:

 public static byte[] GetBytesByImage(PictureBox pb)
{
byte[] photo_byte= null;

if (!pb.Image.Equals(null))
{
using (MemoryStream ms = new MemoryStream())
{
Bitmap bmp
= new Bitmap(pb.Image);
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
photo_byte
= new byte[ms.Length];
ms.Position
= 0;
ms.Read(photo_byte,
0, Convert.ToInt32(ms.Length));
bmp.Dispose();
}
}

return photo_byte;
}
2.将实际位置中的照片转化为byte[]类型写入数据库中:
 public static byte[] GetBytesByImagePath(string strFile)
{
byte[] photo_byte = null;
using (FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
photo_byte
= br.ReadBytes((int)fs.Length);
}
}

return photo_byte;
}
3. 读取byte[]并转化为图片:
 public static Image GetImageByBytes(byte[] bytes)
{
Image photo
= null;
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Write(bytes,
0, bytes.Length);
photo
= Image.FromStream(ms, true);
}

return photo;
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载