无法在 Windows 表单应用程序中显示来自 SQL 服务器数据库的 PictureBox 控件中的图像
Unable To Show Image In Picture Box Control From SQL Server Database In Windows Form Application
我想从我的 C# Windows 表单应用程序将扫描图像(车辆文档)添加到 SQL 服务器数据库。在 SQL 服务器数据库中,将保存扫描文档的列的数据类型是图像,我想将图像存储为每个扫描文档的数组。这是我将数据保存到 SQL 服务器数据库中的代码。
浏览按钮代码:
private void btnBrowseFiles_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "image files|*.jpg;*.png;*.gif;*.icon;*.bmp;*.*";
DialogResult dResult = fileDialog.ShowDialog();
if (dResult == DialogResult.OK)
{
pBoxVehicleDocument.Image = Image.FromFile(fileDialog.FileName);
}
}
添加按钮代码:
private void btnAdd_Click(object sender, EventArgs e)
{
string documentName, vehicleId;
documentName = txtDocumentName.Text;
vehicleId = lblVehicleId.Text;
MemoryStream mStream = new MemoryStream();
pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
mStream.ToArray();
//The documentName, vehicleId and imageArray to be passed to AddVehicleDocuments Method within Vehicles Class.
//No I am getting invalid arguments exception on AddVehicleDocuments() method.
Vehicles.AddVehicleDocuments(documentName, vehicleId, imageArray);
}
最后是车辆中的 AddVehicleDocuments 方法代码 Class:
public static void AddVehicleDocuments(string documentName, int vehicleId, byte[] imageString)
{
string query = string.Format("insert into tblDocuments values ('{0}', {1}, {3})", documentName, vehicleId, imageString);
DataAccess.Execute(query);
}
现在,以下代码用于图像选择。我不确定如何将数组值从数据库转换回 Windows 表单上的图片框:
private void childDocumentDetails_Load(object sender, EventArgs e)
{
int documentId = Convert.ToInt32(lblDocumentId.Text);
DataRow dr = Vehicles.GetDocumentDetailsById(documentId);
txtDocumentName.Text = dr["DocumentName"].ToString();
txtVehicleNo.Text = dr["VehicleNo"].ToString();
//here the image array from the database will locate in dr["VehicleDocument"];
pBoxDocument.Image = //Need to write my code here
}
DataRow dr 包含给定文档的所有三个记录,即 DocumentName、VehicleNo/VehicleID 和字节字符串,但图片框控件仍然不显示目标图像。只是需要一些专家在这方面的建议。
任何帮助将不胜感激。
因为根据我的研究,我确信 SQL Server-Side 上的图像数据类型适合我存储车辆的扫描文档。不过,后来我修改了我的代码,使用了Verbinary(MAX)作为数据库存储图片的数据类型。
我修改了我的插入代码,现在看起来像这样:
private void btnAdd_Click(object sender, EventArgs e)
{
string documentName, vehicleId;
documentName = txtDocumentName.Text;
vehicleId = lblVehicleId.Text;
MemoryStream mStream = new MemoryStream();
pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagesBytes = mStream.ToArray();
Vehicles.AddVehicleDocuments(documentName, vehicleId, imagesBytes);
}
图像选择代码如下所示:
private void childDocumentDetails_Load(object sender, EventArgs e)
{
int documentId = Convert.ToInt32(lblDocumentId.Text);
DataRow dr = Vehicles.GetDocumentDetailsById(documentId);
txtDocumentName.Text = dr["DocumentName"].ToString();
txtVehicleNo.Text = dr["VehicleNo"].ToString();
byte[] imageString = (byte[]dr["VehicleDocument"]);
MemoryStream mStream = new MemoryStream(imageString);
pBoxDocument.Image = Image.FromStream(mStream);
}
感谢@Jimi 提出的代码修改建议。
我想从我的 C# Windows 表单应用程序将扫描图像(车辆文档)添加到 SQL 服务器数据库。在 SQL 服务器数据库中,将保存扫描文档的列的数据类型是图像,我想将图像存储为每个扫描文档的数组。这是我将数据保存到 SQL 服务器数据库中的代码。
浏览按钮代码:
private void btnBrowseFiles_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "image files|*.jpg;*.png;*.gif;*.icon;*.bmp;*.*";
DialogResult dResult = fileDialog.ShowDialog();
if (dResult == DialogResult.OK)
{
pBoxVehicleDocument.Image = Image.FromFile(fileDialog.FileName);
}
}
添加按钮代码:
private void btnAdd_Click(object sender, EventArgs e)
{
string documentName, vehicleId;
documentName = txtDocumentName.Text;
vehicleId = lblVehicleId.Text;
MemoryStream mStream = new MemoryStream();
pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
mStream.ToArray();
//The documentName, vehicleId and imageArray to be passed to AddVehicleDocuments Method within Vehicles Class.
//No I am getting invalid arguments exception on AddVehicleDocuments() method.
Vehicles.AddVehicleDocuments(documentName, vehicleId, imageArray);
}
最后是车辆中的 AddVehicleDocuments 方法代码 Class:
public static void AddVehicleDocuments(string documentName, int vehicleId, byte[] imageString)
{
string query = string.Format("insert into tblDocuments values ('{0}', {1}, {3})", documentName, vehicleId, imageString);
DataAccess.Execute(query);
}
现在,以下代码用于图像选择。我不确定如何将数组值从数据库转换回 Windows 表单上的图片框:
private void childDocumentDetails_Load(object sender, EventArgs e)
{
int documentId = Convert.ToInt32(lblDocumentId.Text);
DataRow dr = Vehicles.GetDocumentDetailsById(documentId);
txtDocumentName.Text = dr["DocumentName"].ToString();
txtVehicleNo.Text = dr["VehicleNo"].ToString();
//here the image array from the database will locate in dr["VehicleDocument"];
pBoxDocument.Image = //Need to write my code here
}
DataRow dr 包含给定文档的所有三个记录,即 DocumentName、VehicleNo/VehicleID 和字节字符串,但图片框控件仍然不显示目标图像。只是需要一些专家在这方面的建议。
任何帮助将不胜感激。
因为根据我的研究,我确信 SQL Server-Side 上的图像数据类型适合我存储车辆的扫描文档。不过,后来我修改了我的代码,使用了Verbinary(MAX)作为数据库存储图片的数据类型。
我修改了我的插入代码,现在看起来像这样:
private void btnAdd_Click(object sender, EventArgs e)
{
string documentName, vehicleId;
documentName = txtDocumentName.Text;
vehicleId = lblVehicleId.Text;
MemoryStream mStream = new MemoryStream();
pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagesBytes = mStream.ToArray();
Vehicles.AddVehicleDocuments(documentName, vehicleId, imagesBytes);
}
图像选择代码如下所示:
private void childDocumentDetails_Load(object sender, EventArgs e)
{
int documentId = Convert.ToInt32(lblDocumentId.Text);
DataRow dr = Vehicles.GetDocumentDetailsById(documentId);
txtDocumentName.Text = dr["DocumentName"].ToString();
txtVehicleNo.Text = dr["VehicleNo"].ToString();
byte[] imageString = (byte[]dr["VehicleDocument"]);
MemoryStream mStream = new MemoryStream(imageString);
pBoxDocument.Image = Image.FromStream(mStream);
}
感谢@Jimi 提出的代码修改建议。