转换为PNG / JPG会导致错误(参数无效)

时间:2018-12-13 12:52:04

标签: c# bitmap

我有此代码,我正在尝试使用该代码从数据库表中的列转换字节数组。现在,它应该做的就是连接到数据库,直通读取,然后转换为Image文件。但这给了我一个非常愚蠢的

error : "Parameter is not valid" on the Method to Create Image From PDF CreateImageFromPDF

屏幕截图如下:

Error Image

现在我的代码看起来像这样

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

using TallComponents.PDF.Rasterizer;


namespace ReadAndConverttoJPG
{
    public class ReadFileDB
    {
        public bool Read(int idnumber)
        {
            //bool myData = false;
            string connectionString = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=ImageControl;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();

            string query = "select pdf_file from SavePDFTable where id =@id";
            SqlCommand cmd = new SqlCommand(query, con);

            cmd.Parameters.AddWithValue("@id", idnumber);
            SqlDataReader rd = cmd.ExecuteReader();

            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    byte[] fileData = (byte[])rd.GetValue(0);
                    FileStream fs = new FileStream(@"C:\Users\***\Desktop\output\test.pdf", FileMode.Create);
                    fs.Write(fileData, 0, fileData.Length);
                    fs.Close();
                }
            }return true;
        }

        public bool CreateImageFromPDF(int id)
        {
            string connectionString = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=ImageControl;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();


            string query = "select pdf_file from SavePDFTable where id =@id";
            SqlCommand cmd = new SqlCommand(query, conn);

            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader rd = cmd.ExecuteReader();

            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    byte[] fileData = (byte[])rd.GetValue(0);

                    Image img;
                    using (MemoryStream ms = new MemoryStream(fileData, 0, fileData.Length))
                    {
                        System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                        img = (Image)converter.ConvertFrom(fileData);

                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            connection.Open();
                            string sql = "insert into PDFImg (img) values (@img)";
                            using (SqlCommand cmd2 = new SqlCommand(sql, connection))
                            {
                                cmd2.Parameters.AddWithValue("@img", img);
                                cmd2.ExecuteNonQuery();
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
}

因此它是一个我想用作参考的类这是我的程序代码,应该从类库中调用main方法

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;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ReadAndConverttoJPG.ReadFileDB db = new ReadAndConverttoJPG.ReadFileDB();
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool i = db.CreateImageFromPDF(2);
                if (i == true)
                {
                    MessageBox.Show("I am OK!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: " + ex.ToString());
            }
        }
    }
}

0 个答案:

没有答案