如何使用c#在postgres数据库中显示我的图片?
我可以在我的postgres数据库中保存我的图片,它在picturebox中。 但是在使用c#在postres数据库中显示我的图片之后,我该怎么做呢如何使用c#在postgres数据库中显示我的图片?,c#,postgresql,C#,Postgresql,我可以在我的postgres数据库中保存我的图片,它在picturebox中。 但是在使用c#在postres数据库中显示我的图片之后,我该怎么做呢 无法显示“来自数据库”的图像 在应用程序中,您需要从数据库中读取图像,然后显示它。您不能“从数据库”显示图像 在应用程序中,您需要从数据库中读取图像,然后显示它。假设您的表如下所示: create table reismDatabase ( resimismi text, -- or some variant resimkendi
无法显示“来自数据库”的图像
在应用程序中,您需要从数据库中读取图像,然后显示它。您不能“从数据库”显示图像
在应用程序中,您需要从数据库中读取图像,然后显示它。假设您的表如下所示:
create table reismDatabase (
resimismi text, -- or some variant
resimkendi bytea
);
这是将表格内容读入图片对象的代码:
NpgSqlCommand cmd = new NpgsqlCommand("select resimismi, resimkendi from reismDatabase",
conn);
NpgsqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string photoName = reader.GetString(0);
Byte[] photoContents = (Byte[])Reader.GetValue(1);
Image photo;
if (photoContents != null)
{
using (Stream st = new System.IO.MemoryStream(photoContents))
photo = Image.FromStream(st);
}
}
reader.Close();
从那里,您可以将
photo
分配给pictureBox.Image
,假设您的表如下所示:
create table reismDatabase (
resimismi text, -- or some variant
resimkendi bytea
);
这是将表格内容读入图片对象的代码:
NpgSqlCommand cmd = new NpgsqlCommand("select resimismi, resimkendi from reismDatabase",
conn);
NpgsqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string photoName = reader.GetString(0);
Byte[] photoContents = (Byte[])Reader.GetValue(1);
Image photo;
if (photoContents != null)
{
using (Stream st = new System.IO.MemoryStream(photoContents))
photo = Image.FromStream(st);
}
}
reader.Close();
从那里,您可以将photo
分配给您的pictureBox.Image
我找到了解决方案。
我使用此查询将图像保存到数据库中
pictureBox1.Image.Dispose();
Postgisconn.Open();
str= "SELECT lo_export(testimage.image, 'C://outputimage.jpg') FROM testimage WHERE id =1;";
NpgsqlCommand cmmnd= new NpgsqlCommand(str, Postgisconn);
cmmnd.ExecuteNonQuery();
Postgisconn.Close();.
pictureBox1.Image = Image.FromFile("C://outputimage.jpg");
通过此查询,我将数据库图像显示到picturebox
感谢所有回答我问题的人。我找到了解决方案。
我使用此查询将图像保存到数据库中
pictureBox1.Image.Dispose();
Postgisconn.Open();
str= "SELECT lo_export(testimage.image, 'C://outputimage.jpg') FROM testimage WHERE id =1;";
NpgsqlCommand cmmnd= new NpgsqlCommand(str, Postgisconn);
cmmnd.ExecuteNonQuery();
Postgisconn.Close();.
pictureBox1.Image = Image.FromFile("C://outputimage.jpg");
通过此查询,我将数据库图像显示到picturebox
谢谢所有回答我问题的人