[英]How can I display my picture in postgres database with c#?
我可以将我的图片保存在我的postgres数据库中。 但是之后如何使用c#在postres数据库中显示我的图片?
FileStream fs = new FileStream(resimPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] resim = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
PostgisBaglan.Open();
strEksenCizgisiUpdate = "INSERT INTO reismDatabase(resimismi,resimkendi) values('resim3','"+resim+"');";
NpgsqlCommand EksenCizgisiUpdateCommand = new NpgsqlCommand(strEksenCizgisiUpdate, PostgisBaglan);
EksenCizgisiUpdateCommand.ExecuteNonQuery();
PostgisBaglan.Close();
openFileDialog1.Filter = "Jpeg Dosyası (*.jpg)|*.jpg|Gif Dosyası (*.gif)|*.gif|Png Dosyası (*.png)|*.png|Tif Dosyası (*.tif)|*.tif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
resimPath = openFileDialog1.FileName.ToString();
}
您无法“从数据库”显示图像。
在您的应用程序中,您需要从数据库中读取图像,然后显示它。
假设您的表格如下所示:
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
。
Postgisconn.Open();
str= "INSERT INTO testimage (id, image) VALUES (1, lo_import('C://test.jpg'));";
NpgsqlCommand cmmnd= new NpgsqlCommand(str, Postgisconn);
cmmnd.ExecuteNonQuery();
Postgisconn.Close();
我用这个查询将图像保存在数据库中。
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。
感谢所有回答我问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.