英文:
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'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)
{
}
}
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论