读取数据库中的image类型后的保存方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取数据库中的image类型后的保存方式相关的知识,希望对你有一定的参考价值。

我数据库中的FileData表里的image字段里面存放的是doc或者是exe类型的文件数据。

我怎么把它读出来保存到本地,比如保存为:“文件.doc”或者是“文件.exe”。
是的,二进制的数,image类型的数据。

System.Data.OleDb.OleDbDataReader.GetBytes 方法获取 byte[] 二进制数据。
System.IO.FileStream.Write 方法写入 byte[] 二进制数据。

请参见 MSDN 的详细参数说明,我不粘贴 MSDN 了。

注意:
FileData 表里的 image 字段里面存放的文件特别大(如:几百兆大小),建议不要 GetBytes 方法一次性读取到内存中,这样不仅很耗内存,并且速度也特别慢。

合理的方法:
设立一个循环,分段读取(如:一次读取 1MB)二进制数据,再调用 Write 把刚才读取的数据写入文件,循环到直至数据全部读取完毕。

PS:
通过 GetBytes 的返回值可以判断数据是否读取完毕(判断是否为零)。该方法的返回值表示读取到的字节数。

提示:
假设您一次读取 1000 字节的数据,而数据字段已没有 1000 字节的数据则 GetBytes 返回值 < 1000 否则 GetBytes 返回值 == 1000
参考技术A 只要确认数据存入时没有加密或什么转换操作,那么可以直接这样.
FileStream fs =new FileStream(@"路径\文件.exe",FileMode.Create)

fs.Write(image字节数据)
fs.Close()本回答被提问者采纳
参考技术B

     保存图片到数据库,C# 代码如下: 

    private void button2_Click_1(object sender, System.EventArgs e)

    string pathName;

    if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)

    pathName = this.openFileDialog1.FileName;

    System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);

    this.pictureBox1.Image = img;

    //将图像读入到字节数组

    System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

    byte[] buffByte = new byte[fs.Length];

    fs.Read(buffByte,0,(int)fs.Length);

    fs.Close();

    fs = null;

    //建立Command命令

    string comm = @"Insert into table1(img,name) values(@img,@name)";

    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();

    this.sqlCommand1.CommandType = System.Data.CommandType.Text ;

    this.sqlCommand1.CommandText = comm;

    this.sqlCommand1.Connection = this.sqlConnection1 ;

    //创建Parameter

    this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);

    this.sqlCommand1.Parameters[0].Value = http://www.jb51.net/ianakin/archive/2012/02/02/buffByte;

    this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

    this.sqlCommand1.Parameters[1].Value =http://www.jb51.net/ianakin/archive/2012/02/02/pathName.Substring(pathName.LastIndexOf("\\\\")+1);

    try

    this.sqlConnection1.Open();

    this.sqlCommand1.ExecuteNonQuery();

    this.sqlConnection1.Close();

    catch(System.Exception ee)

    MessageBox.Show(ee.Message );

    buffByte = null;

    this.FillListBox();

    读取图片代码如下:

    SqlConnection conn=new SqlConnection(@"data source=test;uid=sa;pwd=cym;database=testbase");

    conn.Open();

    SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);

    SqlDataReader reader=cmd.ExecuteReader();

    reader.Read();

    MemoryStream buf=new MemoryStream((byte[])reader[0]);

    Image image=Image.FromStream(buf,true);

    pictureBox1.Image=image;

参考技术C imageBytes = (byte[])sqlDr.GetValue(0);
MemoryStream ms = new MemoryStream(imageBytes);
Bitmap bmpt = new Bitmap(ms);
pictureBox1.Image = bmpt;
参考技术D 二进制的数吧

delphi 中如何将图片数据保存到dat文件里的,然后读取出来,并在Image控件中显示。

参考技术A 先定义好图片的一个类型
TImageType = record
ImageName : string;
width : integer;
height : integer;
ImageData : array[0..49,0..49] of byte;
end;

存图片:
procedure TForm1.SaveImage(path: string);
var
bit : TBitmap;
i,j : integer;
myImage : array[0..999] of TImageType;
FN : string;
dat : TFileStream;
begin
bit := TBitmap.Create;
bit.LoadFromFile(path);
myImage[0].ImageName := 'test';
myImage[0].width := bit.Width;
myImage[0].height := bit.Height;
for i := 0 to bit.Width - 1 do
begin
for j:= 0 to bit.Height - 1 do
begin
myImage[0].ImageData[i,j] := bit.Canvas.Pixels[i,j];
end;
end;

FN := 'C:/test.dat';
dat := TFileStream.Create(FN,fmOpenWrite);
dat.Write(myImage,sizeof(myImage));
dat.Free;
end;

读图片

procedure TForm1.LoadImagefromDat(path: string;image :Timage);
var
dat : TFileStream;
myImage : array [0..999] of TImageType;
i,j : integer;
begin
dat := TFileStream.Create(path,fmOpenRead);
dat.Read(myImage,sizeof(myImage));
dat.Free;

for i := 0 to myimage[0].width - 1 do
begin
for j:= 0 to myImage[0].height - 1 do
begin
image.Canvas.Pixels[i,j] := myImage[0].imagedata[i,j];
end;
end;

end;

未经测试,大致流程应该差不多了追问

谢谢你的答案,但程序在运行时提示出错,stack overflow

追答

是栈溢出了,

myImage : array [0..999] of TImageType;
改成 myImage : array [0..999] of PImageType;
只申明指针试下
用时再分配内存 getmem(myimage[0],sizeof(Timagetype))给指针赋值

追问

我写入的图片是很大的,有1024*768这么大,这样的写入方法会很慢,有没有更快一点的方法。

追答

写入都是逐点写入了,没什么更快的了,或你可以调整小点分辨率,每2到3个点录一个,只是清淅度低点

参考技术B savetofile可以保存成文件,保存时文件名改成DAT就可以
相反的
loadfromfile可以从文件中加载图像追问

savetofile可以保存成Dat文件,但读取就不可以了,而且我是想把大量的图片保存在一个DAT文件里,并读取出来,Image控件中显示

追答

只保存到一个文件的话,只能用数据库,鉴于你只要一个文件的话,也不要太复杂的数据库,用ACCESS就可以了,把图片用二进制方式存入数据库就可以了

参考技术C 面版的添加删除,点添加删除windows组件,在对话框里其他的勾去掉,勾选windows media player点下一步安装.试试看

windows组件不要删,否则会出很多问题.追问

我刚刚测试了一下,把array[0..999] of TImageType;改为array[0..255] of TImageType就没事了,但我写入的图片是很大的,有1024*768这么大,这样的写入方法会很慢,有没有更快一点的方法。

参考技术D //以下代码适合TBitmap类型
//保存
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.Bitmap.SaveToFile(aFile);
end;
//加载
procedure TForm1.BitBtn1Click(Sender: TObject);
var
aStream: TMemoryStream;
begin
aStream := TMemoryStream.Create;
aStream.Position := 0;
aStream.LoadFromFile(aFile);
Image2.Picture.Bitmap.LoadFromStream(aStream);
aStream.Free;end;追问

这样只可以存取一张图片,我需要存取大量的图片。只用一个DAT文件存取。

追答

针对你的题目,已经在空间中发表一篇解决方法,请看:
http://hi.baidu.com/yueyun889/blog/item/7851b31e44773c0b403417f0.html

本回答被提问者采纳
第5个回答  2011-03-08 多个图片存储到一个.dat文件中比较麻烦,图片格式是否是统一的?

以上是关于读取数据库中的image类型后的保存方式的主要内容,如果未能解决你的问题,请参考以下文章

C#怎样读取数据库中Image类型?在线等!!

图片保存到数据库以及C#读取图片

delphi 中如何将图片数据保存到dat文件里的,然后读取出来,并在Image控件中显示。

gdi+从文件读入图片再保存

将抽象数据类型保存和加载到 json 文件并从游戏中的文件中读取 Haskell

WPF的DataGrid数据保存与更改