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

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

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 指令或程序集引用?)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using System.IO;
  12. using SixLabors.ImageSharp;
  13. using SixLabors.ImageSharp.Processing;
  14. using SixLabors.ImageSharp.PixelFormats;
  15. namespace Database_Form
  16. {
  17. public partial class UserInfo : Form
  18. {
  19. private string firstName;
  20. private string lastName;
  21. private string username;
  22. public UserInfo(string username)
  23. {
  24. InitializeComponent();
  25. this.username = username;
  26. // 从数据库中检索人的名字和姓氏
  27. string matricule = "";
  28. byte[] imageData = null;
  29. using (SqlConnection connection = new SqlConnection("我隐藏了这部分代码!!"))
  30. {
  31. connection.Open();
  32. SqlCommand command = new SqlCommand("SELECT Matricule, ProfilePicture FROM Login WHERE Username = @Username", connection);
  33. command.Parameters.AddWithValue("@Username", username);
  34. SqlDataReader reader = command.ExecuteReader();
  35. if (reader.Read())
  36. {
  37. matricule = reader.GetString(0);
  38. if (!reader.IsDBNull(1))
  39. {
  40. imageData = (byte[])reader["ProfilePicture"];
  41. }
  42. }
  43. reader.Close();
  44. command = new SqlCommand("SELECT Nom, Prenom FROM Salarie WHERE Matricule = @Matricule", connection);
  45. command.Parameters.AddWithValue("@Matricule", matricule);
  46. reader = command.ExecuteReader();
  47. if (reader.Read())
  48. {
  49. firstName = reader.GetString(0);
  50. lastName = reader.GetString(1);
  51. }
  52. reader.Close();
  53. }
  54. // 设置标签的文本为人的名字和姓氏
  55. nameLabel.Text = firstName;
  56. lastNameLabel.Text = lastName;
  57. // 在图片框中显示个人资料图片
  58. if (imageData != null)
  59. {
  60. using (MemoryStream ms = new MemoryStream(imageData))
  61. {
  62. Image<Rgba32> image = Image.Load<Rgba32>(ms);
  63. image.Mutate(x => x.Resize(new Size(pictureBox1.Width, pictureBox1.Height)));
  64. pictureBox1.Image = image.ToBitmap();
  65. }
  66. }
  67. }
  68. private void backButton_Click_1(object sender, EventArgs e)
  69. {
  70. MainPage mainPageForm = new MainPage(username);
  71. mainPageForm.Show();
  72. this.Close();
  73. }
  74. private void UserInfo_Load(object sender, EventArgs e)
  75. {
  76. }
  77. private void label2_Click(object sender, EventArgs e)
  78. {
  79. }
  80. private void pictureBox1_Click(object sender, EventArgs e)
  81. {
  82. }
  83. }
  84. }
英文:

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?)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using System.IO;
  12. using SixLabors.ImageSharp;
  13. using SixLabors.ImageSharp.Processing;
  14. using SixLabors.ImageSharp.PixelFormats;
  15. namespace Database_Form
  16. {
  17. public partial class UserInfo : Form
  18. {
  19. private string firstName;
  20. private string lastName;
  21. private string username;
  22. public UserInfo(string username)
  23. {
  24. InitializeComponent();
  25. this.username = username;
  26. // Retrieve the person&#39;s name and last name from the database
  27. string matricule = &quot;&quot;;
  28. byte[] imageData = null;
  29. using (SqlConnection connection = new SqlConnection(&quot;i hid this part of the code!!&quot;))
  30. {
  31. connection.Open();
  32. SqlCommand command = new SqlCommand(&quot;SELECT Matricule, ProfilePicture FROM Login WHERE Username = @Username&quot;, connection);
  33. command.Parameters.AddWithValue(&quot;@Username&quot;, username);
  34. SqlDataReader reader = command.ExecuteReader();
  35. if (reader.Read())
  36. {
  37. matricule = reader.GetString(0);
  38. if (!reader.IsDBNull(1))
  39. {
  40. imageData = (byte[])reader[&quot;ProfilePicture&quot;];
  41. }
  42. }
  43. reader.Close();
  44. command = new SqlCommand(&quot;SELECT Nom, Prenom FROM Salarie WHERE Matricule = @Matricule&quot;, connection);
  45. command.Parameters.AddWithValue(&quot;@Matricule&quot;, matricule);
  46. reader = command.ExecuteReader();
  47. if (reader.Read())
  48. {
  49. firstName = reader.GetString(0);
  50. lastName = reader.GetString(1);
  51. }
  52. reader.Close();
  53. }
  54. // Set the labels&#39; text to the person&#39;s name and last name
  55. nameLabel.Text = firstName;
  56. lastNameLabel.Text = lastName;
  57. // Display the profile picture in the picture box
  58. if (imageData != null)
  59. {
  60. using (MemoryStream ms = new MemoryStream(imageData))
  61. {
  62. Image&lt;Rgba32&gt; image = Image.Load&lt;Rgba32&gt;(ms);
  63. image.Mutate(x =&gt; x.Resize(new Size(pictureBox1.Width, pictureBox1.Height)));
  64. pictureBox1.Image = image.ToBitmap();
  65. }
  66. }
  67. }
  68. private void backButton_Click_1(object sender, EventArgs e)
  69. {
  70. MainPage mainPageForm = new MainPage(username);
  71. mainPageForm.Show();
  72. this.Close();
  73. }
  74. private void UserInfo_Load(object sender, EventArgs e)
  75. {
  76. }
  77. private void label2_Click(object sender, EventArgs e)
  78. {
  79. }
  80. private void pictureBox1_Click(object sender, EventArgs e)
  81. {
  82. }
  83. }
  84. }

答案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:

确定