简体   繁体   English

无法将System.string强制转换为System.Byte []

[英]Unable to cast System.string to System.Byte[]

For some reason, i have been struggling with this since yesterday to generate an image from a Base64 string, it just wouldnt and it shows ok from a test box but does not create the image file , be it PNG or JPG hence i decided to paste here maybe i am wrong somehow. 由于某种原因,我从昨天开始就一直在努力从Base64字符串生成图像,它只是不愿意,而且它在测试盒中显示还可以,但是没有创建图像文件,无论是PNG还是JPG,因此我决定粘贴在这里,也许我错了。

Code looks like this 代码看起来像这样

public void generateImageFromBase64(int idnumber)
{
    string connectionString = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=ImageControl;Integrated Security=True";
    SqlConnection con = new SqlConnection(connectionString);
    con.Open();

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

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

    while (rd.Read())
    {
        //byte[] imgData = (byte[])rd[0];
        **byte[] fileData = (byte[])rd.GetValue(0);
        string img = Convert.ToString(fileData);**
        var bytes = Convert.FromBase64String(img);
        using (var imageFile = new FileStream(@"C:\Users\*****\Desktop\output\test.png", FileMode.Create))
        {
            imageFile.Write(bytes, 0, bytes.Length);
            imageFile.Flush();
        }
    }
}

Now the winform i am using to call it to generate the image file, looks like this 现在我正在使用它来生成图像文件的winform看起来像这样

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 WindowsFormsApp8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ReadAndConverttoJPG.ReadFileDB drd = new ReadAndConverttoJPG.ReadFileDB();
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                drd.generateImageFromBase64(2);
                MessageBox.Show("OK!");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.ToString());
            }
        }
    }
}

What could i be missing here ? 我在这里想念什么?

Edit 编辑

Now here is what, Klaus, it does not seem to output the PNG image to my desktop folder unlike the PDF which did earlier 现在,克劳斯,这似乎是什么,它不像以前的PDF那样将PNG图像输出到我的桌面文件夹中

Code now looks like this 代码现在看起来像这样

public void generateImageFromBase64(int idnumber)
{
    string connectionString = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=ImageControl;Integrated Security=True";
    SqlConnection con = new SqlConnection(connectionString);
    con.Open();

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

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

    while (rd.Read())
    {
        string imgBase64 = rd.GetString(0);
        var bytes = Convert.FromBase64String(imgBase64);
        FileStream fs = new FileStream(@"C:\Users\*****\Desktop\output\test.png", FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();
    }
}

Since the database field contains the base64 string, simply get it as string and convert to binary: 由于数据库字段包含base64字符串,因此只需将其作为字符串获取并转换为二进制:

string imgBase64 = rd.GetString(0);
var bytes = Convert.FromBase64String(imgBase64);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 错误在C#调用存储过程中从'System.String'到'System.Byte []'的无效转换 - Error Invalid cast from 'System.String' to 'System.Byte[]' in C# calling stored procedure 反序列化对象时,无法将类型为System.String的对象转换为类型为System.Byte [] - Cannot convert object of type System.String to type System.Byte[] when deserializing object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM