我遇到以下代码的问题,用于将二进制源转换为位图图像以插入到我的图像列表中。我收到“参数无效”。
// 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 :(得分: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并将其读回是数据库的非常繁重的操作。