尝试从 SQL Server 数据库中检索图像。

huangapple go评论62阅读模式
英文:

trying to retrieve an image from sql server database

问题

我目前正在进行一个C#表单项目的工作,但遇到了以下问题:

> 错误 CS0117:'Image' 不包含 'Load' 的定义
> 错误 CS0308:非泛型类型 'Image' 不能与类型参数一起使用
> 错误 CS0246:类型或命名空间名称 'Rgba32' 无法找到(是否缺少 using 指令或程序集引用?)
> 错误 CS0246:类型或命名空间名称 'SixLabors' 无法找到(是否缺少 using 指令或程序集引用?)
> 错误 CS0246:类型或命名空间名称 'SixLabors' 无法找到(是否缺少 using 指令或程序集引用?)
> 错误 CS0246:类型或命名空间名称 'SixLabors' 无法找到(是否缺少 using 指令或程序集引用?)
> 错误 CS0246:类型或命名空间名称 'ImageSharp' 无法找到(是否缺少 using 指令或程序集引用?)

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;

            // 从数据库中检索人的名字和姓氏
            string matricule = "";
            byte[] imageData = null;
            using (SqlConnection connection = new SqlConnection("我隐藏了这部分代码!!"))
            {
                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();
            }

            // 设置标签的文本为人的名字和姓氏
            nameLabel.Text = firstName;
            lastNameLabel.Text = lastName;

            // 在图片框中显示个人资料图片
            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)
        {

        }
    }
}
英文:

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&#39;s name and last name from the database
string matricule = &quot;&quot;;
byte[] imageData = null;
using (SqlConnection connection = new SqlConnection(&quot;i hid this part of the code!!&quot;))
{
connection.Open();
SqlCommand command = new SqlCommand(&quot;SELECT Matricule, ProfilePicture FROM Login WHERE Username = @Username&quot;, connection);
command.Parameters.AddWithValue(&quot;@Username&quot;, username);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
matricule = reader.GetString(0);
if (!reader.IsDBNull(1))
{
imageData = (byte[])reader[&quot;ProfilePicture&quot;];
}
}
reader.Close();
command = new SqlCommand(&quot;SELECT Nom, Prenom FROM Salarie WHERE Matricule = @Matricule&quot;, connection);
command.Parameters.AddWithValue(&quot;@Matricule&quot;, matricule);
reader = command.ExecuteReader();
if (reader.Read())
{
firstName = reader.GetString(0);
lastName = reader.GetString(1);
}
reader.Close();
}
// Set the labels&#39; text to the person&#39;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&lt;Rgba32&gt; image = Image.Load&lt;Rgba32&gt;(ms);
image.Mutate(x =&gt; 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)
{
}
}
}

答案1

得分: 1

首先,您必须拥有.NET 6.0项目类型。
接下来,您必须打开 '工具' 菜单,'NuGet包管理器','包管理器控制台',并写入 'Install-Package SixLabors.ImageSharp' 以安装SixLabors包。
最后,添加 'using Image = SixLabors.ImageSharp.Image;' 指令。

英文:

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

huangapple
  • 本文由 发表于 2023年7月17日 17:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定