本文介绍了在 C# 中使用 image.fromstream 时参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从互联网上的教程中获得了这段代码,我按照教程一步一步地移动,但我的代码无法正常工作并给我这个错误:System.Drawing.dll 中发生类型为System.ArgumentException"的未处理异常

I have this code from a tutorial on the internet i moved step by step with the tutorial but my code dosent work and gives me this error:An unhandled exception of type 'System.ArgumentException' occurred in 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;

        }

    }
}

推荐答案

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

The quotes around the parameter name are messing up the insert query:

改变这个

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

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

这篇关于在 C# 中使用 image.fromstream 时参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:54