对图像进行存储和显示
2005-02-01 23:08
309 查看
以下是在.net中对图像进行存储和显示的方法,其原理是把读入的图像字节数组数据转化为string类型保存在数据库中;在显示时在由字符型转化为字节数组写入页面。
具体方法如下:
一、保存图片
以二进制流的形式读入图片数据,转化为string保存入数据库
private string GetPhotoID()
{
string photoid = Guid.NewGuid().ToString();
}
private string GetPhotoType(string photoname)
{
string type = "";
int position = photoname.LastIndexOf(".");
if(position != -1)
type = photoname.Substring(post+1);
type = type.ToLower().Trim();
if(type != "bmp" && type != "jpg" && type != "jpeg" && type != "gif")
return null;
return type;
}
private string GetPhotoValue()
{
int datalength = (int) this.inputfile.PostedFile.InputStream.Length;
byte[] _data = new BinaryReader(this.inputfile.PostedFile.InputStream).ReadBytes(datalength);
string photodata = Convert.ToBase64String(_data);
return photodata;
}
private string SavePhoto()
{
if(this.inputfile.PostedFile.ContentLength>1024*1024)
return;
string photoname = this.inputfile.PostedFile.FileName;
string photoid = GetPhotoID();
string phototype = GetPhotoType(photoname)
if(phototype == null)
return;
string photovalue = GetPhotoValue();
string sqlstring = "Insert Into tablename (columnname...) values('"+photoID+"','"+phototype+"','"+photovalue+"')";
string connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/database/....mdb";
OleDbConnection myconnect = new OleDbConnection(connectstr);
myconnect.Open();
OleDbCommand mycommand = new OleDbCommand(sqlstring,myconnect);
mycommand.ExecuteNonQuery();
myconnect.Close();
}
二、显示图片
把数据库中的值转化为二进制流输出
private void ShowPhotp()
{
string type,values;
int count;
string connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/database/....mdb";
OleDbConnection myconnect = new OleDbConnection(connectstr);
string sqlstring = "Select columns From tablename where photoID = '"+this.photoID+"' ";
OleDbDataAdapter myadapter = new OleDbDataAdapter(sqlstring,myconnect);
DataSet myset = new DataSet();
myadapter.Fill(myset);
if(myset.Tables[0].Rows.Count!=0)
{
type = myset.Tables[0].Rows[0][1].ToString();
values = myset.Tables[0].Rows[0][2].ToString();
count = values.Length;
}
else
{
return;
}
byte[] _bytes = new byte[count];
_bytes = Convert.FromBase64String(values);
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
switch(type)
{
case "jpg":
Response.ContentType = "image/JPEG";
break;
case "bmp":
Response.ContentType = "image/BMP";
break;
default:
return;
}
Response.OutputStream.Write(_bytes,0,count);
Response.End();
}
大家还有什么好的方法一起共享阿!!
具体方法如下:
一、保存图片
以二进制流的形式读入图片数据,转化为string保存入数据库
private string GetPhotoID()
{
string photoid = Guid.NewGuid().ToString();
}
private string GetPhotoType(string photoname)
{
string type = "";
int position = photoname.LastIndexOf(".");
if(position != -1)
type = photoname.Substring(post+1);
type = type.ToLower().Trim();
if(type != "bmp" && type != "jpg" && type != "jpeg" && type != "gif")
return null;
return type;
}
private string GetPhotoValue()
{
int datalength = (int) this.inputfile.PostedFile.InputStream.Length;
byte[] _data = new BinaryReader(this.inputfile.PostedFile.InputStream).ReadBytes(datalength);
string photodata = Convert.ToBase64String(_data);
return photodata;
}
private string SavePhoto()
{
if(this.inputfile.PostedFile.ContentLength>1024*1024)
return;
string photoname = this.inputfile.PostedFile.FileName;
string photoid = GetPhotoID();
string phototype = GetPhotoType(photoname)
if(phototype == null)
return;
string photovalue = GetPhotoValue();
string sqlstring = "Insert Into tablename (columnname...) values('"+photoID+"','"+phototype+"','"+photovalue+"')";
string connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/database/....mdb";
OleDbConnection myconnect = new OleDbConnection(connectstr);
myconnect.Open();
OleDbCommand mycommand = new OleDbCommand(sqlstring,myconnect);
mycommand.ExecuteNonQuery();
myconnect.Close();
}
二、显示图片
把数据库中的值转化为二进制流输出
private void ShowPhotp()
{
string type,values;
int count;
string connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/database/....mdb";
OleDbConnection myconnect = new OleDbConnection(connectstr);
string sqlstring = "Select columns From tablename where photoID = '"+this.photoID+"' ";
OleDbDataAdapter myadapter = new OleDbDataAdapter(sqlstring,myconnect);
DataSet myset = new DataSet();
myadapter.Fill(myset);
if(myset.Tables[0].Rows.Count!=0)
{
type = myset.Tables[0].Rows[0][1].ToString();
values = myset.Tables[0].Rows[0][2].ToString();
count = values.Length;
}
else
{
return;
}
byte[] _bytes = new byte[count];
_bytes = Convert.FromBase64String(values);
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
switch(type)
{
case "jpg":
Response.ContentType = "image/JPEG";
break;
case "bmp":
Response.ContentType = "image/BMP";
break;
default:
return;
}
Response.OutputStream.Write(_bytes,0,count);
Response.End();
}
大家还有什么好的方法一起共享阿!!
相关文章推荐
- 在C#的pictureBox显示的图像某一指定区域进行透明化处理
- 一个 Qt 显示图片的控件(继承QWidget,使用QPixmap记录图像,最后在paintEvent进行绘制,可缩放)
- [转]数据库存储图像及使用Image控件显示
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
- MFC+Halcon混合编程,图像读入,显示和存储
- 数据库存储图像及使用Image控件显示
- Access数据库中图像的存储和显示
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- 要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布上,这样才可以显示在手机屏幕上。Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设置,主要方法(没有全部列
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
- VB对图像数据存储显示
- 一个查询数据库所有表的存储过程(允许按表名模糊查询一级按显示数量进行分页)
- 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。 程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。 请使用3个数组处理函数来分别进行输入、显示和计算
- 使用halcon(版本10.0 64位)读入图像并进行显示,使用ChartAxis类实现图像直方图的显示?
- fmri的图像数据在matlab中显示,利用imagesc工具进行显示,自带数据集-by 西南大学xulei教授
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- ROS中利用opencv3完成四个视频流拼接成一副图像显示,并添加track条进行参数调节
- 利用opencv进行图像文件读入和显示
- 5-python图像处理opencv(1.读图,显示,转换,存储)
- 关于存储在Sql中图像得显示