[英]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.