2016-07-28 12 views
0

我想从数据库中读取所有图像并将它们保存为jpg文件,我所做的是创建一个函数,每个图像都会收到它的ID作为名称。我的表看起来像这样的数据存在。循环来自数据库的所有图像,并将它们保存为JPG文件

ImageCode Image 

DHS001  Long Binary Data 
DHS002  Long Binary Data 
DHS003  Long Binary Data 
DHS004  Long Binary Data 
DHS005  Long Binary Data 

我的确写了一个函数有两个参数,图片名称和二进制数据可以读取图像,然后将其保存到特定的路径,但问题是它很好执行读取和写入,但图像保持不变,但图像的编号是确定

public void _setimage(string imgCode, byte[] byteArrayIn) 
{ 
    try 
    { 
     MemoryStream ms = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length); 
     ms.Write(byteArrayIn, 0, byteArrayIn.Length); 
     Image returnImage = Image.FromStream(ms, true); 

     finalImage.Save("D:\\" + imgCode + ".jpg", ImageFormat.Jpeg); 

     img = null; 
     finalImage = null; 
     ms.Close(); 
    } 
    catch 
    { 
    } 
} 

,而我使用的循环将图像从数据库中的功能发生如下

public void createPhoto() 
{ 
    if (con.State == ConnectionState.Open) { con.Close(); } 
    SqlcommandString = "select ImageCode Image from ImageTable order by Barcode"; 
    OleDbCommand cmd = new OleDbCommand(SqlcommandString, con); 
    con.Open(); 
    OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "Personaldetails"); 
    OleDbDataReader dr = cmd.ExecuteReader(); 
    byte[] bimg; 
    while (dr.Read()) 
    { 
     bimg = (byte[])ds.Tables[0].Rows[0][1]; 

     // this here is where i call the save image function to execute 
     _setimage(dr.GetValue(0).ToString(), bimg); 
    } 

    dr.Close(); 
    cmd.Dispose(); 
    con.Close(); 
} 

回答

0

你必须通过你的T迭代能够获得每个图像的行。例如:

foreach (DataTable table in ds.Tables) 
{ 
    foreach (DataRow row in table.Rows) 
    { 
     _setimage(row[0], row[1]); 
    } 
} 

你只是得到表的第一行。如果迭代DataSet,则不需要在(dr.Read())时执行以下操作。我还假定你正在使用C#,我上面看到。

+0

非常感谢你的工作 – Bels

+0

@Bels - 如果有效,你能接受我的回答吗? –

+0

实际上,我从那个@ mgm-squared – Bels

相关问题