[英]View a contact photo in a 3-layer architecture in a picturebox C#
I want to write a phonebook in C # with a three-layer architecture, but I do not have enough of this architecture. 我想用C#编写具有三层体系结构的电话簿,但是我对这种体系结构还不够。 We have a datagridview that displays the image of each person in the picturebox when we click on each of the rows.
我们有一个datagridview,当我们单击每一行时,它将在图片框中显示每个人的图像。 But it does not work.
但这行不通。 I do not know where the problem is.
我不知道问题出在哪里。 Please guide me.
请指导我。 I recently got acquainted with this architecture.
我最近熟悉这种体系结构。
DAL code: DAL代码:
public void GetBinaryImage(int Id)
{
context.People.Select(a => new { a.Id, a.PersonImage }).Where(a => a.Id == Id) ;
}
BLL code BLL代码
public void GrtBinaryImage(int person_Id)
{
Myperson.GetBinaryImage(person_Id);
}
PL code PL代码
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
int Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
showpictureBox.Image = bytearytoimage(person.GrtBinaryImage(Id));
}
}
private Image bytearytoimage(object v)
{
MemoryStream stream = new MemoryStream(v);
return Image.FromStream(stream);
}
I get the error: 我得到错误:
"Argument 1: cannot convert from 'void' to object".
“参数1:不能从'void'转换为object“。
I know I wrote a mistake, please guide me. 我知道我写错了,请指导我。
You seem to a have a lack of understanding with returning values from functions. 您似乎对从函数返回值缺乏理解。
First of all your DLL function doesn't return anything, I can only assume you are trying to return PersonImage
which is hopefully a byte[]
. 首先,您的DLL函数不返回任何内容,我只能假设您正在尝试返回
PersonImage
,希望它是一个byte[]
。 In addition, you need to change the code to select only the value you want to return: 此外,您需要更改代码以仅选择要返回的值:
DLL 动态链接库
public byte[] GetBinaryImage(int Id)
{
byte[] result = context.People.Where(a => a.Id == Id).Select(a => a.PersonImage).SingleOrDefault();
return result;
}
Next up, you have the same problem with return values in your BLL: 接下来,您在BLL中的返回值也遇到相同的问题:
BLL BLL
public byte[] GrtBinaryImage(int person_Id)
{
return Myperson.GetBinaryImage(person_Id);
}
That should be enough to get what you want, although I would also recommend changing the parameter type in your bytearytoimage
function specifically to byte[]
: 尽管我还建议您将
bytearytoimage
函数中的参数类型专门更改为byte[]
,但这应该足以满足您的要求:
PL PL
private Image bytearytoimage(byte[] v)
{
MemoryStream stream = new MemoryStream(v);
return Image.FromStream(stream);
}
Note that this all hinges on the fact that PersonImage
is a byte[]
. 请注意,所有这些都取决于
PersonImage
是byte[]
的事实。 If it isn't then you should update your question to be more specific about what data you have to work with. 如果不是,那么您应该更新您的问题,以更具体地说明必须使用哪些数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.