如何将图像加载到PictureBox根据数据库中存储的图像位置
本文关键字:图像 数据库 存储 位置 PictureBox 加载 | 更新日期: 2023-09-27 18:15:14
我有一个sql数据库,我有图像保存图像位置
,
D:'Local Pictures'Users'XXXX_XXXX_1.jpg
我正在尝试使用以下代码将其放置在图片框中
DataConnection myCon = new DataConnection(); // Contains Data Connection String
SqlConnection con1 = new SqlConnection(@"Data Source=.'SQLS2012;Initial Catalog=test;Integrated Security=True");
con1.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * From USER_TABLE WHERE USERID =" + userIdTextBox.Text,con1);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
nameTextBox.Text = (myReader["FIRST_NAME"].ToString());
lnameTextBox.Text = (myReader["LAST_NAME"].ToString());
posTextBox.Text = (myReader["POSITION"].ToString());
emailTextBox.Text = (myReader["E_MAIL"].ToString());
phoneTextBox.Text = (myReader["PHONE"].ToString());
usernameTextBox.Text = (myReader["USERNAME"].ToString());
userLevelTextBox.Text = (myReader["USER_LEVEL"].ToString());
string filename = (myReader["PROFILE_PICTURE"].ToString());
profilePicBox.ImageLocation = filename;
}
当我执行这段代码,我得到小x图片白色背景的图像。如何修复和加载图像
Loading:
MSDN不完全清楚这个问题- ImageLocation属性,但似乎设置ImageLocation
属性可能不会完全加载图像,所以你可以尝试使用PictureBox。加载方法代替:
...
string filename = (myReader["PROFILE_PICTURE"].ToString());
profilePicBox.Load(filename);
编辑
ImageLocation
应该工作得很好,只要确保你的路径是正确的,你不使用相对路径-我如何使用ImageLocation将图像放入我的图片框?
位置和大小:
并确保你的PictureBox属性相关的大小和位置可以处理不同的图像大小
在while循环中进行此更改
while (myReader.Read())
{
nameTextBox.Text = (myReader["FIRST_NAME"].ToString());
lnameTextBox.Text = (myReader["LAST_NAME"].ToString());
posTextBox.Text = (myReader["POSITION"].ToString());
emailTextBox.Text = (myReader["E_MAIL"].ToString());
phoneTextBox.Text = (myReader["PHONE"].ToString());
usernameTextBox.Text = (myReader["USERNAME"].ToString());
userLevelTextBox.Text = (myReader["USER_LEVEL"].ToString());
string filename = (myReader["PROFILE_PICTURE"].ToString());
profilePicBox.Image = Image.FromFile(filename )
}