[c-sharp] view plaincopyprint?
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
}
第二种:首先将照片转化为byte[]类型,然后在写入数据,代码如下;
[c-sharp] view plaincopyprint?
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
}
第三种:直接读取byte[]并转化为图片;
[c-sharp] view plaincopyprint?
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
}
mysql中有2种字段类型来存储二进制原始数据。1、binary和varbinary,适合存储少量的二进制数据2、blob适合存储大量的数据输入时可以用INSERT INTO table (name) VALUE( x“0123456789ABCDEF“ )这样的16进制串,也可以JDBC的setBlob等方法输入。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)