图像未检索并显示错误输入不是有效的Base 64字符串

时间:2017-12-07 14:13:23

标签: c# sql

我正在尝试至少两个小时才能解决这个问题 我正在检索存储在数据库中的图像。

public Image Base64ToImage(string base64String) {
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    // Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = Image.FromStream(ms, true);
    return image;
}

使用数据库的代码为:

con.Open();
cmd = new SqlCommand("select [s_image] from [2017] where serialno="+serianotext.Text ,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {

    imagepicturebox.Image = Base64ToImage(dr[0].ToString());
}
con.Close();

错误显示在图片中 enter image description here

1 个答案:

答案 0 :(得分:0)

我不知道你的base64字符串的值,所以我写了这个例子。您可以使用它来制作自己的代码。

using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        private static string connectionString = "Data Source=localhost;Initial Catalog=my_test_db;Integrated Security=True;";

        public static string ImageToBase64(Image image, ImageFormat imageFormat)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, imageFormat);
                byte[] imageBytes = ms.ToArray();
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
        public static Image Base64ToImage(string base64String)
        {
            byte[] imageBytes = Convert.FromBase64String(base64String);
            using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
            {
                ms.Write(imageBytes, 0, imageBytes.Length);
                Image image = Image.FromStream(ms, true);
                return image;
            }
        }

        static void Main(string[] args)
        {
            string image1base64;

            using(Bitmap image1 = (Bitmap) Image.FromFile("C:\\test1.png"))
                image1base64 = ImageToBase64((Image)image1, ImageFormat.Png);

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand("create table #base64test(id int not null, base64image varchar(max));", sqlConnection))
                    sqlCommand.ExecuteNonQuery();

                using (SqlCommand sqlCommand = new SqlCommand("insert #base64test(id, base64image) values(@id, @base64image)", sqlConnection))
                {
                    sqlCommand.Parameters.Add("@id", SqlDbType.Int);
                    sqlCommand.Parameters["@id"].Value = 1;

                    sqlCommand.Parameters.Add("@base64image", SqlDbType.VarChar, -1);
                    sqlCommand.Parameters["@base64image"].Value = image1base64;

                    sqlCommand.ExecuteNonQuery();
                }

                using (SqlCommand sqlCommand = new SqlCommand("select base64image from #base64test where id = @id", sqlConnection))
                {
                    sqlCommand.Parameters.Add("@id", SqlDbType.Int);
                    sqlCommand.Parameters["@id"].Value = 1;

                    using (SqlDataReader rd = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        if (rd.Read() == false)
                            throw new ApplicationException("An error occurred ...");

                        object[] temp = new object[rd.FieldCount];//[1]

                        // read BLOB trick
                        if (rd.GetSqlValues(temp) != rd.FieldCount)
                            throw new ApplicationException("An error occurred ...");

                        Image image2 = Base64ToImage(((SqlString)temp[0]).ToString());
                        image2.Save("C:\\test2.png", ImageFormat.Png);
                    }
                }
            }
        }
    }
}