trying to retrieve an image from sql server database

fjnneemd  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(166)

I'm currently working on a c# form project, but came across the following issues
Error CS0117: 'Image' does not contain a definition for 'Load'
Error CS0308: The non-generic type 'Image' cannot be used with type arguments
Error CS0246: The type or namespace name 'Rgba32' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246: The type or namespace name 'SixLabors' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246: The type or namespace name 'SixLabors' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246: The type or namespace name 'SixLabors' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246: The type or namespace name 'ImageSharp' could not be found (are you missing a using directive or an assembly reference?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.PixelFormats;

namespace Database_Form
{
    public partial class UserInfo : Form
    {
        private string firstName;
        private string lastName;
        private string username;

        public UserInfo(string username)
        {
            InitializeComponent();
            this.username = username;

            // Retrieve the person's name and last name from the database
            string matricule = "";
            byte[] imageData = null;
            using (SqlConnection connection = new SqlConnection("i hid this part of the code!!"))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT Matricule, ProfilePicture FROM Login WHERE Username = @Username", connection);
                command.Parameters.AddWithValue("@Username", username);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    matricule = reader.GetString(0);
                    if (!reader.IsDBNull(1))
                    {
                        imageData = (byte[])reader["ProfilePicture"];
                    }
                }
                reader.Close();

                command = new SqlCommand("SELECT Nom, Prenom FROM Salarie WHERE Matricule = @Matricule", connection);
                command.Parameters.AddWithValue("@Matricule", matricule);
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    firstName = reader.GetString(0);
                    lastName = reader.GetString(1);
                }
                reader.Close();
            }

            // Set the labels' text to the person's name and last name
            nameLabel.Text = firstName;
            lastNameLabel.Text = lastName;

            // Display the profile picture in the picture box
            if (imageData != null)
            {
                using (MemoryStream ms = new MemoryStream(imageData))
                {
                    Image<Rgba32> image = Image.Load<Rgba32>(ms);
                    image.Mutate(x => x.Resize(new Size(pictureBox1.Width, pictureBox1.Height)));
                    pictureBox1.Image = image.ToBitmap();
                }
            }
        }

        private void backButton_Click_1(object sender, EventArgs e)
        {
            MainPage mainPageForm = new MainPage(username);
            mainPageForm.Show();
            this.Close();
        }
        private void UserInfo_Load(object sender, EventArgs e)
        {

        }

       

        

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
zpqajqem

zpqajqem1#

First, you must have .NET 6.0 project type. Next, you have to open 'Tools' menu, 'NuGet Packet Manager', 'Package Manager Console' and write 'Install-Package SixLabors.ImageSharp' to install SixLabor package. Last, add 'using Image = SixLabors.ImageSharp.Image;' directive

相关问题