如何从数据库中将图像显示到图片框中 [英] how to display image into picturebox from database

查看:90
本文介绍了如何从数据库中将图像显示到图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它的Windows应用程序。我使用以下编码从数据库中获取图像并显示到图片框中。我收到错误如

操作数冲突nvarchar与图像不兼容



如何解决这个问题

its windows application. i used below coding to fetch image from database and display into picturebox. i am getting error like
"operand clash nvarchar is incompatible with image"

how to resolve this

private void getemphoto(string empid)
        {
            try
            {
                gn.cnopen();
                SqlCommand cmdSelect = new SqlCommand("select emp_photo from EMP_PHOTO_DETAILS where emp_id=@ID", gn.cn());
                cmdSelect.Parameters.Add("@ID", SqlDbType.NVarChar, 250);
                cmdSelect.Parameters["@ID"].Value = empid.ToString();
                byte[] barrImg = (byte[])cmdSelect.ExecuteScalar();
                string strfn = Convert.ToString(DateTime.Now.ToFileTime());
                FileStream fs = new FileStream(strfn,
                                  FileMode.CreateNew, FileAccess.Write);
                fs.Write(barrImg, 0, barrImg.Length);
                fs.Flush();
                fs.Close();
                
                //picturebox   
                Empimgbox.Image = Image.FromFile(strfn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }

推荐答案

什么行给出了这个错误?您是否考虑过向我们提供ACTUAL错误消息?您可以使用FromStream从没有文件系统的数据创建图像。
What line gives this error ? Did you consider giving us the ACTUAL error message ? You can create an image from the data without the file system, using FromStream.


您好,



尝试以下方法,



1.创建一个名为EmployeeImage.aspx的页面



在Page_load中,编写以下代码



MemoryStream msStream = new MemoryStream();



尝试

{

if(!(String.IsNullOrEmpty(Request.QueryString [EmployeeId])))

{



gn.cnopen();

SqlCommand cmdSelect = new SqlCommand(从EMP_PHOTO_DETAILS中选择emp_photo,其中emp_id = @ ID,gn.cn());

cmdSelect.Parameters .Add(@ ID,SqlDbType.NVarChar,250);

cmdSelect.Parameters [@ ID]。Value = Request.QueryString [EmployeeId]。ToString();

byte [] Photo =(byte [])cmdSelect.ExecuteScalar();





msStream.Write(Photo,0,Photo.Length);

位图bmpBitmap =新位图(msStream);



Response.ContentType =image / jpg;

bmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);





}

}

终于

{

msStream.Close();

}



你的EmployeeImage.aspx如下所示,



<%@ Page Language =C#AutoEventWireup =trueCodeFile =EmployeeImage.aspx.csInherits =EmployeeImage%>



<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >



< html xmlns =http://www.w3.org/1999/xhtml>

< ; head runat =server>

< title>< / titl e>

< / head>

< body>

< form id =form1runat =server>

< div>



< / div>

< / form>

< / body>

< / html>



要显示图片的页面,请使用以下代码:



对于图像控制,

< asp:图像runat =服务器ID =imgEmpPhoto

ImageAlign =TopWidth =118Height =118/>



在显示图像的代码后面写下面的代码,

imgDvrPhoto.ImageUrl = String.Format(EmployeeImage.aspx?EmployeeId = {0},txtEmployeeId.Text);
Hi,

Try the following one,

1. Create a page called EmployeeImage.aspx

In Page_load, write the following code

MemoryStream msStream = new MemoryStream();

try
{
if (!(String.IsNullOrEmpty(Request.QueryString["EmployeeId"])))
{

gn.cnopen();
SqlCommand cmdSelect = new SqlCommand("select emp_photo from EMP_PHOTO_DETAILS where emp_id=@ID", gn.cn());
cmdSelect.Parameters.Add("@ID", SqlDbType.NVarChar, 250);
cmdSelect.Parameters["@ID"].Value = Request.QueryString["EmployeeId"].ToString();
byte[] Photo= (byte[])cmdSelect.ExecuteScalar();


msStream.Write(Photo, 0, Photo.Length);
Bitmap bmpBitmap = new Bitmap(msStream);

Response.ContentType = "image/jpg";
bmpBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);


}
}
finally
{
msStream.Close();
}

Your EmployeeImage.aspx will look like below,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeImage.aspx.cs" Inherits="EmployeeImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

The page you want to display the image, use the following code:

For Image control,
<asp:Image runat="server" ID="imgEmpPhoto"
ImageAlign="Top" Width="118" Height="118" />

In code behind to display the image write the following code,
imgDvrPhoto.ImageUrl = String.Format("EmployeeImage.aspx?EmployeeId={0}", txtEmployeeId.Text);


这篇关于如何从数据库中将图像显示到图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆