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();
}
}
}
任何人都可以幫助我瞭解爲什麼我收到此錯誤。我是計算機編程新手,看不出自己爲什麼。
我其實有這個代碼。但窗戶切斷了。滾動條將顯示其餘部分。 – 2012-03-26 20:01:23
然後我認爲問題在於你沒有真正指定圖像,你需要遍歷從數據庫中選擇的每個圖像。 – 2012-03-26 20:06:02
我已經修改了代碼,希望現在它可以爲您工作。 – 2012-03-26 20:09:08