在wpf应用程序中从数据库中检索图像

时间:2015-05-08 21:42:36

标签: c# .net sql-server wpf xaml

我有一个包含Login窗口的简单应用程序和另一个名为Principal的窗口。在我的SQL Server数据库中,有一个tbl_user,其中包含以下列:

  • pmk_int_user
  • name_str_user
  • user_str_user
  • pass_str_user
  • photo_img_user
    输入图片。

在我的校长WPF Xaml中:

<Grid>
        <Image HorizontalAlignment="Left" Height="71" Margin="10,10,0,0" VerticalAlignment="Top" Width="87" Name="Photo"/>
</Grid>

我的登录WPF XAML:

<TextBox Grid.Column="2"
     Grid.Row="1"
     Name="txtUser"
     Margin="4"/>

<PasswordBox Grid.Column="2"
     Grid.Row="2"
     Name="txtPass"
     Margin="4"/>

<Button Name="btnLogin"
    Content="Login"
    Width="60"
    Height="30"
    Margin="4"
    IsDefault="True"
    ToolTipService.ToolTip="Login"
    Click="btn_login">
</Button>

我的登录代码背后:

private void btn_login(object sender, RoutedEventArgs e)
{
    if (txtUser.Text != "") 
    {
        if (txtPass.Password != "")
        {

            _con = new SqlConnection(_strConexao);

            _con.Open();
            string user_str_user = txtUser.Text;
            string user_str_user = txtPass.Password;
            string query = "select * from tbl_user where user_str_user='" + this.txtUser.Text + "' and pass_str_user='" + this.txtPass.Password + "'";
            _cmd = new SqlCommand(query, _con);
            _dr = _cmd.ExecuteReader();


            string _name;
            string _data;
            int _id;
            byte[] _photo;

            if (_dr.HasRows)
            {
                while (_dr.Read())
                {
                    try
                    {
                        _id = _dr.GetInt32(0);
                        _name = _dr.GetString(1);
                        _data = DateTime.Now.ToString();
                        _photo = (byte[])_dr[3];
                        Principal p = new Principal();
                        p.ID.Text = _id.ToString();
                        p.Nome.Text = _name;
                        p.Data.Text = _data;

                        MemoryStream strm = new MemoryStream(_Photo);
                        System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        MemoryStream ms = new MemoryStream();
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        ms.Seek(0, SeekOrigin.Begin);
                        bi.StreamSource = ms;
                        bi.EndInit();
                        p.Photo.Source = bi;
                        p.Show();
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                         MessageBox.Show(ex.Message);
                    }
                }

但是当我点击并使用有效的用户和密码登录时,它会显示an invalid parameter。当我评论这段代码时:

MemoryStream strm = new MemoryStream(_photo);
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
p.Photo.Source = bi;

此代码只需打开File Dialoge即可选择Image:

private void btnAddFoto(object sender, RoutedEventArgs e)
        {
            OpenFileDialog _fd = new OpenFileDialog();
            byte[] dadosImagem = null;
            if (_fd.ShowDialog() == true)
            {
                try
                {
                    _fs = new FileStream(_fd.FileName, FileMode.Open, FileAccess.Read);
                    dadosImagem = new byte[_fs.Length];
                    _fs.Read(dadosImagem, 0, System.Convert.ToInt32(_fs.Length));
                    _fs.Close();

                    ImageSourceConverter imgs = new ImageSourceConverter();
                    imgPessoa.SetValue(Image.SourceProperty, imgs.ConvertFromString(_fd.FileName.ToString()));


                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro :: " + ex.Message);
                }
}

这是转换:

private byte[] BitmapSourceToByteArray(BitmapSource image)
        {
            using (var stream = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder(); 
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(stream);
                return stream.ToArray();
            }
        }

这是从Image Control获取图像并保存在数据库中:

var foto_img_pessoa = BitmapSourceToByteArray((BitmapSource)imgPessoa.Source);

虽然有效,但我的Principal窗口中没有显示我的图像。这段代码有什么问题? 我的错误消息例外是:{System.ArgumentException: Parâmetro inválido. em System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) em System.Drawing.Image.FromStream(Stream stream)

0 个答案:

没有答案