如何从表中加载图像。我用过图像控制。 [英] How To Load Image From A Table. I Have Used Image Control.

查看:51
本文介绍了如何从表中加载图像。我用过图像控制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的c#代码..我想在image1图像控件上加载图片

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (Convert.ToInt16(Session [ CompanyID] .ToString())> 0
{
con.Open( );
SqlCommand cmd = new SqlCommand( select *来自CompanyID = + Session [ CompanyID],con)的公司;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, 公司);

if (ds.Tables [ 0 ]。Rows.Count > 0
{
Label1.Text = ds.Tables [ 0 ]。行[ 0 ] [ EMAILID]的ToString();
Image1 = ???

}

解决方案

如果您将图像作为字节存储在数据库中,这些链接将帮助您 -

存储和检索来自的图像SQL Server使用Strored Procedures和C#.net [ ^ ]

在C#中发送/接收PictureBox图像到/从Microsoft SQL SERVER [ ^ ]

从SQL Server数据插入和检索图像基础使用C# [ ^ ]

流式传输图片 [ ^ ]


您没有提供与您的问题相关的足够信息,例如存储图像的位置,存储方式(字节,base64string等等),以及您的控件是什么尝试将图像附加到。



但是根据你所提供的内容,这可能会对你有所帮助。

例如:

  protected   void  Page_Load(对象发​​件人,EventArgs e){
if (Convert.ToInt16(Session [ CompanyID]。ToString())> 0 ){
con.Open();
SqlCommand cmd = new SqlCommand(
从CompanyID = +
的公司中选择*会话[ CompanyID< /跨度>],CON);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, 公司);

if (ds.Tables [ 0 ]。Rows.Count > 0 ){
Label1.Text = ds.Tables [ 0 ]。行[ 0 ] [ EMAILID]的ToString();

// 假设您将图像存储为字节:
// ======================= ====================
// 从数据库字段中获取字节
// 其中MyImage是字段表格中的名称
byte [] img = ds.Tables [ 0 ] .Rows [ 0 ] [ MyImage];
Image image1 = ConvertImage(img);

if (image1!= null // 检查图像
// 将图片添加到控件中(如果存在)
}
}
}

private static 图片ConvertImage( byte [] img){
尝试 {
if (img == null || img.Length == 0
throw new 异常( Byte [] empty);

// create stream&将字节数组转换为图像
MemoryStream ms = new MemoryStream(img); // 将字节添加到Steam
Image i = Image.FromStream(ms); // 从流创建图像

ms.Dispose(); // 清理,发布流资源

return i; // image / null返回值
}
catch (例外e){ // 错误:
// 做某事
return null ; // genaric return value
}
}


Below is my c# code.. i want to load image on image1 image control

 protected void Page_Load(object sender, EventArgs e)
        {
 if (Convert.ToInt16( Session["CompanyID"].ToString()) > 0)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Companies where CompanyID="+Session["CompanyID"],con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds,"Companies");

                if (ds.Tables[0].Rows.Count > 0)
                {
                    Label1.Text = ds.Tables[0].Rows[0]["EmailID"].ToString();
Image1= ???

}

解决方案

if you are storing your images as bytes in the database, these links will help you -
Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]
Sending/Receiving PictureBox Image in C# To/From Microsoft SQL SERVER[^]
Inserting and Retrieving images from SQL Server Database using C#[^]
Streaming the Images[^]


You don't provide enough info relating to your question, such as where is the image stored, how is it stored (bytes, base64string, ect...), and what control you are trying to attach the image to.

However base on what you have provided this may help you.
exmple:

protected void Page_Load(object sender, EventArgs e) {
    if (Convert.ToInt16(Session["CompanyID"].ToString()) > 0) {
        con.Open();
        SqlCommand cmd = new SqlCommand(
                "select * from Companies where CompanyID="+
                Session["CompanyID"],con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds,"Companies");
 
        if (ds.Tables[0].Rows.Count > 0) {
            Label1.Text = ds.Tables[0].Rows[0]["EmailID"].ToString();

            //Assuming you are storing the image as bytes:
            //===========================================
            //Get bytes from database field
            //Where "MyImage" is the Field name in your table
            byte[] img = ds.Tables[0].Rows[0]["MyImage"];
            Image image1 = ConvertImage(img);

            if(image1 != null) //check for image
                //Add to your image to the control, if exist
        }
    }
}

private static Image ConvertImage(byte[] img) {
    try {
        if (img == null || img.Length == 0)
            throw new Exception("Byte[] empty");

        //create stream & convert byte array to image
        MemoryStream ms = new MemoryStream(img); //add bytes to steam
        Image i = Image.FromStream(ms); //create image from stream

        ms.Dispose(); //clean-up, release stream resource

        return i; //image/null return value
    }
    catch(exception e) { //Error:
        //Do somthing
        return null; //genaric return value
    }
}


这篇关于如何从表中加载图像。我用过图像控制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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