我正在尝试使用C#从SQL服务器检索图像,方法是单击Windows窗体中的SHOW按钮并将其显示在Windows窗体的PictureBox中(在Visual Studio 2010中)。下面的代码给出了“内存不足”的错误。在使用调试器时,可以看到barrImg获取了正确的值,但仍然没有显示图像。
namespace try1
{
public partial class Form1 : Form
{
SqlConnection conS;
SqlCommand cmdS;
SqlDataAdapter dataadapS;
string constr = "Data Source=______,_____;Network Library=__________;Initial Catalog=___________;User Id=___________;Password=__________";
public Form1()
{
InitializeComponent();
}
private void btn_show_Click(object sender, EventArgs e)
{
string sql = "select col1 from table1 where condition1";
conS = new SqlConnection(constr);
try
{
byte[] outbyte_LP1 = null;
DataSet gD = new DataSet();
conS.Open();
cmdS = new SqlCommand(sql, conS);
dataadapS = new SqlDataAdapter(cmdS);
dataadapS.Fill(gD, "gd");
if (gD.Tables["gd"].Rows.Count > 0)
{
//string strfn = Convert.ToString(DateTime.Now.ToFileTime());
//FileStream FS = new FileStream(strfn, FileMode.Create);
//byte[] blob = (byte[])gD.Tables["gd"].Rows[0]["col1"];
//FS.Write(blob, 0, blob.Length);
//FS.Close();
//FS = null;
//pctimg1.Image = Image.FromFile(strfn);
//byte[] barrImg = (byte[])cmdS.ExecuteScalar();
//string strfn = Convert.ToString(DateTime.Now.ToFileTime());
//FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
//fs.Write(barrImg, 0, barrImg.Length);
//fs.Flush();
//fs.Close();
//pctimg1.Image = Image.FromFile(strfn);
DateTime actDate = (DateTime)gD.Tables["gd"].Rows[0]["DateTime"];
outbyte_LP1 = (byte[])gD.Tables["gd"].Rows[0]["col1"];
MemoryStream ms_LP1 = new MemoryStream(outbyte_LP1, 0, outbyte_LP1.Length);
ms_LP1.Write(outbyte_LP1, 0, outbyte_LP1.Length);
pctimg1.Image = Image.FromStream(ms_LP1, true);
}}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}}
private void btn_close_Click(object sender, EventArgs e)
{
if (conS.State == ConnectionState.Open)
{
conS.Close();}
this.Close();}}}