将图像从数据库加载到WPF中的图像控件 [英] load image from database to image control in wpf

查看:64
本文介绍了将图像从数据库加载到WPF中的图像控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
{
    SqlConnection conn = new SqlConnection("Data Source=UDAY;Initial Catalog=Sample;Integrated Security=True");
    conn.Open();
    SqlCommand cmd = new SqlCommand("select id from testimage", conn);
    cmd.ExecuteNonQuery();
    if (image1.DataContext != null)
    {
                      return;
    }
    //using filestream object write the column as bytes and store it as an image
    foreach (DataRow dataRow in dataTable.Rows)
    {
        if (dataRow.ItemArray[0].ToString() == comboBox1.SelectedItem.ToString())
        {
            FileStream FS1 = new FileStream("c:\\image.jpg", FileMode.Create);
            byte[] blob = (byte[])dataRow.ItemArray[1];
            FS1.Write(blob, 0, blob.Length);
            FS1.Close();

            ImageSourceConverter imgs = new ImageSourceConverter();
                        
            image2.Stretch = Stretch.Fill;
            image2.DataContext = null;

        }
    }
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.StackTrace);
}



我没有收到任何错误,但图像未加载.您能帮我吗?



I didn''t get any errors, but the image is not loading. Can you help me?

推荐答案

您也没有将image source属性设置为希望返回的图像.

我不知道您是否需要它,但我使用此方法将byte []数组转换为BitmapImage对象:

You''re also not setting the image source property to what you''re hoping is the returned image.

I don''t know if you need it or not, but I use this method to convert a byte[] array to an BitmapImage object:

//--------------------------------------------------------------------------------
public static BitmapImage BitmapImageFromBytes(byte[] bytes)
{
   BitmapImage  image  = null;
   MemoryStream stream = null;
   try
   {
       stream = new MemoryStream(bytes);
       stream.Seek(0, SeekOrigin.Begin);
       System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
       image = new BitmapImage();
       image.BeginInit();
       MemoryStream ms = new MemoryStream();
       img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
       ms.Seek(0, SeekOrigin.Begin);
       image.StreamSource = ms;
       image.StreamSource.Seek(0, SeekOrigin.Begin);
       image.EndInit();
    }
    catch (Exception)
    {
       throw;
    }
    finally
    {
       stream.Close();
       stream.Dispose();
    }
    return image;
}


您正在使用ExecuteNonQuery.这不会返回任何行,但是将使用数据填充指定的任何输出参数.您未指定任何参数,查询为选择ID".

阅读有关ExecuteNonQuery的规范以查看此内容; http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx [ ^ ]

您应该正在执行ExecuteReader,并且查询应包括包含blob的必要列.

目前,blob尚无任何内容可写入文件.
You are using ExecuteNonQuery. This does not return any rows, but any output parameters specified will be populated with data. You are not specifying any paramaters and your query is Select an ID.

Read the spec on ExecuteNonQuery to see this; http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx[^]

You should be executing a ExecuteReader and the query should include the necessary column containing the blob.

At present the blob won''t have anything to write to the file.


这篇关于将图像从数据库加载到WPF中的图像控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆