在c#中检索图像到图片框时,我得到的错误参数无效 [英] i got error parameter is not valid while retrieving image to picture box in c#
问题描述
我已编写此代码用于从数据库检索图像到图片框。我在这一行'pictureBox1.Image = Image.FromStream(stm)中得到了这些错误'参数无效';
I have written this code for retrieving image from database to picturebox. I got these error 'Parameter is not valid' at this line 'pictureBox1.Image = Image.FromStream(stm);
Byte[] imagebyte = new Byte[0];
imagebyte = (Byte[])(dr["Pic_Image"]);
MemoryStream stm = new MemoryStream(imagebyte);
pictureBox1.SizeMode = PictureBoxSizeMode。 StretchImage;
pictureBox1.Image = Image.FromStream(stm);
它将该图像存储为数据库中的0x53797374656D2E44726177696E672E4269746D6170
你会解决这个错误吗。
MemoryStream stm = new MemoryStream(imagebyte);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromStream(stm);
It is storing that image as 0x53797374656D2E44726177696E672E4269746D6170 in database
would u pls solve this error.
推荐答案
所以你的步骤会是这样的
1.此代码从< sometable>中检索行。将数据库中的表放入DataSet中,将最近添加的图像复制到Byte数组中,然后复制到MemoryStream对象中,然后将MemoryStream加载到PictureBox控件的Image属性中。
2.将数据导入数据集后,计算编号。检索到数据表的图像。
So your steps would be like this
1.This code retrieves the rows from the <sometable> table in the database into a DataSet, copies the most recently added image into a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Image property of the PictureBox control.
2. After getting data into Dataset, count the no. of images retrieved into datatable.
if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
}
C=从数据表中检索到的行数
"C" = count of rows retrieved from data table
您好,
从Sqlserver检索图像时,通过这种方式将byte []转换为原始格式。
Hello ,
While retrieving the image from Sqlserver convert the byte[] into original format by this way.
public Image GetDataToImage(byte[] pData)
{
try
{
ImageConverter imgConverter = new ImageConverter();
return imgConverter.ConvertFrom(pData) as Image;
}
catch (Exception ex)
{
MsgBox.Show(ex.Message, "Error", MsgBox.Buttons.OK, MsgBox.Icon.Error);
return null;
}
}
并最终通过调用GetDataToImage这个函数设置图片框图像,然后在图片框中加载图片
and ultimately set the picturebox image by call "GetDataToImage" this function before load the image in picturebox
pictureBox1.Image = GetDataToImage((byte[])byteimage);
谢谢
thanks
在数据库中它存储如下0x53797374656D2E44726177696E672E4269746D6170
可以解决错误
In database it stored like this 0x53797374656D2E44726177696E672E4269746D6170
Can u pls solve the error
这篇关于在c#中检索图像到图片框时,我得到的错误参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!