Winform中怎样从Picbox中读取图片,然后将读取的图片存入数据库中,求代码实例???
解决方案 »
- c# 点 事件触发
- 为什么我的程序要运行两次,我做的修改才起作用?
- 如何在C#中简单的实现类似java中ibatis那样的功能,急呀,高手都进来看看?
- 求解“System.Data.OleDb.OleDbException”类型的异常,咋处理?跪谢!
- XSD事务处理
- 在阅读 .aspx文件遇到的问题
- 关于163,smtp问题
- ASP NET的代码分离??
- 做了个window Client程序,要用本地程序连接远程服务器,取回远程服务器的事件信息,如何?
- 用C#编写的windows Service中,能否访问Web应用的Application变量值?
- SQl存储过程中,如何使用IN
- 禁用鼠标移出事件
读取时把Byte转换为Bitmap
private static byte[] ImageDatabytes(Bitmap myBitmap)
{
try
{
using (MemoryStream curImageStream = new MemoryStream())
{
myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
curImageStream.Flush();
byte[] bmpBytes = curImageStream.ToArray();
return bmpBytes;
}
}
catch
{
return null;
}
} public static Bitmap GetImage(byte[] ImageDatas)
{
try
{
using (MemoryStream ImageMS = new MemoryStream())
{
ImageMS.Write(ImageDatas, 0, ImageDatas.Length);
Bitmap resultBitmap = new Bitmap(ImageMS);
return resultBitmap;
}
}
catch
{
return null;
}
}
读取时把Byte转换为Bitmap
private static byte[] ImageDatabytes(Bitmap myBitmap)
{
try
{
using (MemoryStream curImageStream = new MemoryStream())
{
myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
curImageStream.Flush();
byte[] bmpBytes = curImageStream.ToArray();
return bmpBytes;
}
}
catch
{
return null;
}
} public static Bitmap GetImage(byte[] ImageDatas)
{
try
{
using (MemoryStream ImageMS = new MemoryStream())
{
ImageMS.Write(ImageDatas, 0, ImageDatas.Length);
Bitmap resultBitmap = new Bitmap(ImageMS);
return resultBitmap;
}
}
catch
{
return null;
}
}
摆脱各位大侠,小弟问的是:Winform中怎样从Picbox中读取图片,然后将读取的图片存数据库中,是PicBox
1、Bitmap bitmap =new Bitmap(picBox.Image);//提取图片转化为Bitmap
2、byte[] bytes=ImageDatabytes(bitmap)//将bitmap转化为byte[];
3、insert into ...//插入数据库
4、select ...//提取数据
5、Bitmap bitmap=GetImage(btyes)//将数据库中提取数据转化为bitmap
6、picBox.Image=bitmap//设置显示图片属性不知道LZ说的是不是这个意思啊。时间不早了,洗洗睡了。
第1种方式:
DataRow row = ds.Tables[0].Rows[0];
byte[] bPhoto = new byte[0];
bPhoto = (byte[])row["photo"];
//int arraySize = bPhoto.GetUpperBound(0); MemoryStream memstr = new MemoryStream(bPhoto);
pictureBox1.Image = Image.FromStream(memstr, true);
第2种方式:
imagebytes = (byte[])dr["guatu"];
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
B、图片存入数据库
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
}