在C#中使用image.fromstream时参数无效

时间:2015-09-11 16:48:51

标签: c# ms-access visual-studio-2013

我有来自互联网上的教程的代码我一步一步地使用教程,但我的代码非常有用并且给了我这个错误: 类型为' System.ArgumentException'的未处理异常发生在System.Drawing.dll

附加信息:参数无效。

这是我的代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO; 

namespace image_retriver
{
    public partial class Form1 : Form
    {
        OleDbConnection connection = new OleDbConnection();
        OleDbDataAdapter adapter;
        DataTable localdatatable=new DataTable ();
        int row_positon = 0;
        int row_number = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void connect_to_database()
        {
            connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb";
            connection.Open();
            adapter = new OleDbDataAdapter("SELECT * FROM `Table1`",connection);
            adapter.Fill(localdatatable);
            if (localdatatable.Rows.Count > 0)
            {
                row_positon = localdatatable.Rows.Count;
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            connect_to_database();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
                button5.Enabled = true;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            storedata(converimagetobytes(pictureBox2.Image));
        }

        private byte[] converimagetobytes(Image input)
        {
            Bitmap bm = new Bitmap(input);
            MemoryStream mystream = new MemoryStream();
            bm.Save(mystream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageasbytes = mystream.ToArray();
            return imageasbytes;
        }
        private void storedata(byte[] image)
        {
            if (connection.State.Equals(ConnectionState.Closed))
                connection.Open();
            try
            {

                MessageBox.Show("saving at row :" + row_positon.ToString());
                OleDbCommand oledbinsert = new OleDbCommand("Insert INTO Table1 (Img) Values('@myimg')", connection);
                OleDbParameter imagepram = oledbinsert.Parameters.AddWithValue("@myimg", SqlDbType.Binary);
                imagepram.Value = image;
                imagepram.Size = image.Length;
                int afrow = oledbinsert.ExecuteNonQuery();
                MessageBox.Show("image added");
                row_positon++;

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
                MessageBox.Show(e.StackTrace.ToString());

            }
            finally
            {
                refreshcon();
            }
        }
        private void refreshcon()
        { 
            if(connection.State.Equals(ConnectionState.Open))
            {
            connection.Close();
             localdatatable.Clear();
                connect_to_database();

            }

        }
        private Image readimage()
        {
            Image fetched;
            if (row_number >= 0)
            {
                byte[] fetchedimgbytes = (byte[])localdatatable.Rows[row_number]["Img"];
                MemoryStream stream = new MemoryStream(fetchedimgbytes);
                fetched = Image.FromStream(stream,true,true);//here is where i get the error
                return fetched;

            }
            else
            {
                MessageBox.Show("error");
                return null;
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            refreshcon();
            row_number = 0;
            pictureBox2.Image = readimage();
            button2.Enabled = true;
            button3.Enabled = true;

        }

    }
}

1 个答案:

答案 0 :(得分:1)

参数名称周围的引号搞乱了插入查询:

更改此

new OleDbCommand("Insert INTO Table1 (Img) Values ('@myimg')", connection);

new OleDbCommand("Insert INTO Table1 (Img) Values (@myimg)", connection);