C#WPF从SQL Server数据库检索图像,字节到图像 [英] C# WPF Retrieve Image from SQL Server database, byte to image

查看:66
本文介绍了C#WPF从SQL Server数据库检索图像,字节到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这些代码可以获取用户信息.

So I have these codes that will get me the user information.

注意:注释行是我已经尝试过的内容.

NOTE: the commented lines are the stuff that I have already tried.

代码:

    public UserInfo GetUserCredentials(string usernameParameter, string passwordParameter)
    {
        UserInfo getInfo = new UserInfo();

        try
        {
            accessToDatabase.OpeningDatabase();
            String query = "SELECT * FROM Users where Username=@Username AND Password=@Password";
            SqlCommand sqlCmd = accessToDatabase.Command(query);

            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.Parameters.AddWithValue("@Username", usernameParameter);
            sqlCmd.Parameters.AddWithValue("@Password", passwordParameter);
            SqlDataReader oReader = sqlCmd.ExecuteReader();

            byte[] pictureIdByte;
            Image pictureIdImg = null;

            while (oReader.Read())
            {
                getInfo.Username = oReader["Username"].ToString();
                getInfo.Password = oReader["Password"].ToString();
                getInfo.Role = oReader["Role"].ToString();
                getInfo.Firstname = oReader["Firstname"].ToString();
                getInfo.Lastname = oReader["Lastname"].ToString();
                getInfo.Status = Convert.ToInt32(oReader["Status"]);
                getInfo.Gender = oReader["Gender"].ToString();
                getInfo.Birthday = oReader["Birthday"].ToString();
                getInfo.Email = oReader["Email"].ToString();
                getInfo.Address = oReader["Address"].ToString();
                getInfo.City = oReader["City"].ToString();
                getInfo.Country = oReader["Country"].ToString();
                getInfo.Mobilenumber = oReader["Mobile"].ToString();

                pictureIdByte = (byte[])(oReader["PictureID"]);

                if (pictureIdByte == null)
                {
                    getInfo.PictureID = pictureIdImg;
                }
                else
                {
                    //MemoryStream stream = new MemoryStream(pictureIdByte);
                    //getInfo.PictureID.Source = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

                    //var pictureIdVar = new BitmapImage();
                    //using (var memStream = new MemoryStream(pictureIdByte))
                    //{
                    //    memStream.Position = 0;
                    //    pictureIdVar.BeginInit();
                    //    pictureIdVar.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                    //    pictureIdVar.CacheOption = BitmapCacheOption.OnLoad;
                    //    pictureIdVar.UriSource = null;
                    //    pictureIdVar.StreamSource = memStream;
                    //    pictureIdVar.EndInit();
                    //}
                    //pictureIdVar.Freeze();
                    //getInfo.PictureID.Source = pictureIdVar;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            accessToDatabase.ClosingDataBase();
        }
        return getInfo;
    }

然后我在这个主窗口中有一个图像,该图像是我从xaml设计中拖放下来的.并使用后面的代码,我试图像这样设置源,但是每当我尝试登录时,它都会给我一个错误:

AND I have this main window where it has an image which I dropped and drag from the xaml design. And using the code behind, I tried setting the source like this but whenever I am trying to log-in it is giving me an error of:

System.NullReferenceException:'对象引用未设置为对象的实例.'

System.NullReferenceException: 'Object reference not set to an instance of an object.'

MyThesisProject.Model.UserInfo.PictureID.get 返回null.

以下是xaml主窗口中的代码:

Here is the code behind of the main window xaml:

public partial class MainWindow : Window
{
    public MainWindow(UserInfo userLoginDetails)
    {
        InitializeComponent();
        imgTest.Source = userLoginDetails.PictureID.Source;
    }
}

我不知道哪里出了问题,我尝试使用带注释的行,但仍然出现null错误.

I do not know where did I go wrong, I tried using the commented lines but still the same error of null.

推荐答案

您的UserInfo类中不应包含 Image 元素.

You should not have an Image element in your UserInfo class.

PictureID 属性的类型更改为ImageSource

Change the type of the PictureID property to ImageSource

public class UserInfo
{
    ...
    public ImageSource PictureID { get; set; } 
    ...
}

并按如下所示设置其值:

and set its value like this:

if (pictureIdByte != null)
{
    using (var stream = new MemoryStream(pictureIdByte))
    {
        getInfo.PictureID = BitmapFrame.Create(stream,
                                BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}

在您的MainWindow构造函数中,如下设置图像的源:

In your MainWindow constructor, set the Image's Source like this:

imgTest.Source = userLoginDetails.PictureID;

这篇关于C#WPF从SQL Server数据库检索图像,字节到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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