2012-03-26 56 views
1

我与下面的代码转换为插入到我的图像列表的位图图像的二进制代码的问题。我收到“参数无效”。将二进制转换为图像列表的位图图像。代码错误

// Retrieving data from Database 
private void Form1_Load(object sender, EventArgs e) 
    { 
     vcon.Open(); 
     string get = "Select Path, Images FROM IMGSTR"; 
     OleDbCommand cmdget = new OleDbCommand(get, vcon); 
     OleDbDataReader reader; 
     reader = cmdget.ExecuteReader(); 

     while (reader.Read()) 
     { 
      string path = reader["Path"].ToString(); 

      //here is where I am receiving an error 
      this.imageList1.Images.Add(Image.FromStream(new MemoryStream((byte[]) reader["Images"]))); 


      //creates rows in my listview to display images 
      Graphics theGraphics = Graphics.FromHwnd(this.Handle); 
      int count = this.listView1.Items.Count; 
      for (; count < this.imageList1.Images.Count; count++) 
      { 
       listView1.Items.Add("", count); 
      } 


     } 



    //Add Photos and Path to Database 
    private void importImagesToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     FolderBrowserDialog dig = new FolderBrowserDialog(); 

     if (dig.ShowDialog() == DialogResult.OK) 
     { 
      MessageBox.Show("This May take a while. Depending on the size and amount of your photos. Click Ok to continue", "! ! !"); 
      foreach (String files in Directory.GetFiles(dig.SelectedPath)) 
      { 
       if (files.EndsWith(".JPG")) 
       { 
        //convert .jpg to thumbnail 
        Image image = new Bitmap(files); 
        Image pThumbnail = image.GetThumbnailImage(100, 100, null, new IntPtr()); 

        //Insert Path and Image into database 
        string cmdstr = "INSERT into IMGSTR(Path, Images) values(path, image)"; 
        OleDbCommand com = new OleDbCommand(cmdstr, vcon); 
        com.Parameters.AddWithValue("path", files); 

        //Storing Image into MS database as Ole Object 
        com.Parameters.AddWithValue("image", pThumbnail); 

        com.ExecuteNonQuery(); 

        image.Dispose(); 
       } 
      } 
     } 

任何人都可以帮助我了解为什么我收到此错误。我是计算机编程新手,看不出自己为什么。

回答

0
//you are casting the blob from pulled from the db to a byte array 
while(reader.Read()) 
{ 
    this.imageList1.Images.Add(Image.FromStream(
     new MemoryStream((byte[] reader["Images"]) 
} 

但以我的经验,图像往往会被存储在服务器上,又添加了一个路径(链接)数据库中的文件,如添加的blob和回读是很重的操作数据库。

+0

我其实有这个代码。但窗户切断了。滚动条将显示其余部分。 – 2012-03-26 20:01:23

+0

然后我认为问题在于你没有真正指定图像,你需要遍历从数据库中选择的每个图像。 – 2012-03-26 20:06:02

+0

我已经修改了代码,希望现在它可以为您工作。 – 2012-03-26 20:09:08

相关问题