| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1304 人关注过本帖
标题:显示数据库中的图片,却显示System.byte[]?
只看楼主 加入收藏
lxd824
Rank: 1
等 级:新手上路
帖 子:135
专家分:0
注 册:2007-4-21
收藏
 问题点数:0 回复次数:1 
显示数据库中的图片,却显示System.byte[]?
这是我从网上搜的代码,存储部分都搞定了,但是当显示的时候只显示了System.byte[],不知道是什么问题?
#region 上传图片到数据库
  private void UpIMGButton_Click(object sender, System.EventArgs e)
  {   
   string exName=UpFile.Value.Substring(UpFile.Value.LastIndexOf(".")+1).ToUpper();//找出图片的后缀名
      string ImgName=DateTime.Now.ToString("yyyyMMddhhmmssfff")+"."+exName;
   if (UpFile.PostedFile.ContentLength==0)
   {
    Response.Write("<script> alert('你上传的图片不能为空!');</script>");
   }
   else
   {
    try
    {
     Byte[] FileByte = new byte[UpFile.PostedFile.ContentLength];
     Stream  ObjectStream = UpFile.PostedFile.InputStream;
     ObjectStream.BeginRead(FileByte,0,UpFile.PostedFile.ContentLength,null,null);
     string imgType=UpFile.PostedFile.ContentType;
                    Byte[] SmallFileByte = new byte[UpFile.PostedFile.ContentLength];
                     SmallFileByte=CreateThumnail(ObjectStream,100,100);
    string C;
   SqlConnection Conn = new SqlConnection(ConStr);
    Conn.Open();
        SqlCommand  myCommand =new SqlCommand();
     myCommand.Connection=Conn;
         into [UpImage] (imageName,image,imgType,SmallImage) values (@ImgName,@FileByte,@imgType,@SmallImage)";
     myCommand.Parameters.AddWithvalue("@ImgName",ImgName);
     myCommand.Parameters.AddWithvalue("@FileByte",FileByte);
     myCommand.Parameters.AddWithvalue("@imgType",imgType);
     myCommand.Parameters.AddWithvalue("@SmallImage",SmallFileByte);
     myCommand.ExecuteNonQuery();
     Response.Write("<script> alert('图片保存到数据库成功!');</script>");
    }
    catch(Exception ex)
    {
     Response.Write (ex.Message);
       }
   
   }
  }
  #endregion
  #region 生成缩略图
  private Byte[]  CreateThumnail(Stream ImageStream,int tWidth, int tHeight)
    {
   System.Drawing.Image g  =  System.Drawing.Image.FromStream(ImageStream);
   int[] thumbSize = new int[]{1,1};
   thumbSize = NewthumbSize(g.Width, g.Height, tWidth, tHeight);
   Bitmap imgOutput = new Bitmap(g, thumbSize[0], thumbSize[0]);//这里提示为无效参数,所以我把后两个参数直接用100,100代替
   MemoryStream imgStream = new MemoryStream();
   System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
   imgOutput.Save(imgStream, thisFormat);
   Byte[] imgbin =new byte[imgStream.Length];
   imgStream.Position = 0;
   Int32 n = imgStream.Read(imgbin,0,imgbin.Length);
   g.Dispose();
   imgOutput.Dispose();
   return imgbin;
  }
  #endregion
  #region 根据上传图片调整缩略图的尺寸
   
  protected int[] NewthumbSize(int currentwidth,int currentheight,int newWidth ,int newHeight)
  {
   int tempMultiplier;
   if(currentheight > currentwidth)
   {  
    tempMultiplier = newHeight / currentheight;
   }
      else
   {
     tempMultiplier = newWidth / currentwidth;
   }
   int[] NewSize = new int[]{(currentwidth * tempMultiplier),(currentheight * tempMultiplier)};
     return NewSize;
  }
  #endregion
////图片显示页的代码
  smallimage from [UpImage] ";
   SqlDataReader dr =db.myCommand.ExecuteReader();
   this.Response.C;
   while(dr.Read())
   {
    Response.BinaryWrite((byte[])dr["smallimage"]);
      }
搜索更多相关主题的帖子: byte 数据库 System 
2007-11-23 18:36
FenteLi
Rank: 1
来 自:上海
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-11-24
收藏
得分:0 
上传图片
<%@ Page Language="C#"%>
<% @ Import Namespace=" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<script Language="C#" runat="server">

    protected void btnUpdateImage_Click(object sender, EventArgs e)
    {
        if (File1.Value!=null)
        {
            SqlTransaction tran=null;
            string fileName = File1.Value.Substring(File1.Value.LastIndexOf(".")+1);
            try
            {
                HttpPostedFile UploadFile = File1.PostedFile;
                int iFileLength = UploadFile.ContentLength;

                Byte[] FileByteArray = new Byte[iFileLength];
                Stream StreamObject = UploadFile.InputStream;
                StreamObject.Read(FileByteArray, 0, iFileLength);

                SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["imagesConnString"]);
                conn.Open();

                tran = conn.BeginTransaction();
                SqlCommand cmd = conn.CreateCommand();
                cmd.Transaction = tran;
                = CommandType.StoredProcedure;
                = "add_new_img";

                cmd.Parameters.Add("@bytedata", SqlDbType.VarBinary);
                cmd.Parameters["@bytedata"].Value = FileByteArray;
                cmd.Parameters.Add("@type", SqlDbType.NVarChar);
                cmd.Parameters["@type"].Value = fileName;

                string strID=cmd.ExecuteScalar().ToString();
                hdPicPath.Value =strID;
                UploadStatusLabel.Text = "图片" + fileName + "已经上传成功!";
                ();
            }
            catch(Exception ep)
            {
                tran.Rollback();
            }
            finally
            {
               conn.Close();   
            }
        }
        else
        {
            UploadStatusLabel.Text = "你没有选择图片文件,请选择";
        }
    }
</script>
add_new_img是一个存储过程,如下:
ALTER PROCEDURE [dbo].[add_new_img]
    @type NVarchar(50),
    @bytedata VarBinary(max)
AS
BEGIN
    INSERT INTO image(bytedata,type) VALUES(@bytedata,@type)
    SELECT @@IDENTITY
END

显示图片部分
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<script Language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    string strID = Request.QueryString["ID"].ToString();//这个ID是刚才存储过程中返回的ID
    string query = "SELECT bytedata FROM image WHERE id=" + int.Parse(strID);
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["imagesConnString"]);
    try
    {
        conn.Open();

        SqlCommand cmd = new SqlCommand(query, conn);
        byte[] GetImagebyte = (byte[])cmd.ExecuteScalar();
        Response.BinaryWrite(GetImagebyte);
    }
    catch(Exception ep)
    {
        //throw new DataException(ep.Message);
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
    }         
}
</script>
该文件转载自[url]http://www.[/url]
2007-11-24 13:55
快速回复:显示数据库中的图片,却显示System.byte[]?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089869 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved