zoukankan      html  css  js  c++  java
  • Gridview 读取数据库图片并 改变大小(图片处理)

    本例是model使用LINQ写的,数据库SQLserver,解决了数据库累心转换的麻烦问题。同时,通过函数的调用,使得数据库图片读取之后,可以虽数据值的改变,按着比例改变图片的大小。数据库的存储是,图片上传之后,使用二进制存储。感谢abe的指导和帮助。

    LINQ 的model

    复制代码
    Code
    private Binary _image;
    [Column(Storage ="_image", DbType ="varbinary")]
    public Binary image
    {
    get
    {
    returnthis._image;
    }
    set
    {
    this._image = value;
    }
    }
    复制代码

    DAL层数据的读取。通过产品的分类。读取相应的产品。

    复制代码
    Code
    ///<summary>
    /// Get all the products by classify
    ///</summary>
    ///<param name="classify"></param>
    ///<returns></returns>
    public List<Products> GetSomeCProducts(string classify )
    {
    var pspInfo = from u in db.Products
    where u.classification == classify
    orderby u.lastMTime
    select u;
    return pspInfo.ToList<Products>();
    }
    复制代码

    Gridview前台代码的设置。没什么好讲的。模板列的使用大家都很熟悉。

    复制代码
    Code
    <asp:GridView ID ="productsList" runat ="server" AllowPaging ="true" AutoGenerateColumns ="false"
    SkinID ="GridView" OnRowDataBound ="List_RowDataBound" OnPageIndexChanging ="List_PageIndexChanging">
    <Columns>
    <asp:ImageField DataImageUrlField="filename"
    HeaderText="图片">
    </asp:ImageField>
    <asp:TemplateField HeaderText="图片">
    <ItemTemplate>
    <asp:Image ID="Image1" runat="server"/>
    </ItemTemplate>

    </Columns>
    </asp:GridView>
    复制代码

    后台代码简要说明一下。由于数据库是存储的二进制,而Gridview的机制是使用ImageUrl才能读取,所以我们必须读取出来,存到一个缓存中,然后把缓存的URL给Gridview才能够显示图片。。。。CreateImage是把二进制的转换成Image格式,而下面的那个函数是,对图片自定义大小和背景颜色而后显示。因为图片大小不一定能满足你原有的比例,裁剪之后不一定合适,最后有背景颜色进行填充最好。。最后一个函数是Gridview的分页。

    复制代码
    Code
    privatevoid InitProductsList()
    {
    string prodInfo =string.Empty;
    //分类的名称。和数据库里面的classify对应。
    prodInfo ="psp";
    BLOProducts blp =new BLOProducts();
    productsList.DataSource = blp.GetProInfo(prodInfo);
    productsList.DataBind();
    }
    ///<summary>
    /// DataBound
    ///</summary>
    ///<param name="sender"></param>
    ///<param name="e"></param>
    protectedvoid List_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    string prodInfo =string.Empty;
    if (e.Row.RowIndex <0)
    return;

    string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "filename");

    System.Web.UI.WebControls.Image tmp_Image = (System.Web.UI.WebControls.Image)e.Row.Cells[2].FindControl("Image1");
    if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "image")))
    {

    byte[] photo = (DataBinder.Eval(e.Row.DataItem, "image") as Binary).ToArray();

    System.Drawing.Image img = CreateImage(photo);
    System.Drawing.Image aPhoto = CreateThumb(img, 60, 60, Color.Purple);


    string strPath ="~/images/"+ strPersonName.Trim() +".JPG";
    string strPhotoPath = Server.MapPath(strPath);
    //保存图片文件
    aPhoto.Save(strPhotoPath);
    tmp_Image.ImageUrl = strPath;
    }

    }
    }
    protected System.Drawing.Image CreateImage(Byte[] pBytes)
    {
    MemoryStream aStream =new MemoryStream(pBytes);
    System.Drawing.Image rImg = System.Drawing.Image.FromStream(aStream);
    aStream.Close();
    return rImg;
    }

    protected System.Drawing.Image CreateThumb(System.Drawing.Image pSource, int pWidth, int pHeight, Color pBkColor)
    {
    int aWidth = pSource.Width;
    int aHeight = pSource.Height;

    double xScale = (aWidth *1.0) / pWidth;
    double yScale = (aHeight *1.0) / pHeight;
    double mScale = xScale;
    bool fitX =true;
    if (yScale > xScale)
    {
    mScale = yScale;
    fitX =false;
    }
    int offset =0;
    if (fitX)
    offset = (int)(pHeight - aHeight / mScale) /2;
    else
    offset = (int)(pWidth - aWidth / mScale) /2;

    int rWidth = (int)(aWidth / mScale);
    int rHeight = (int)(aHeight / mScale);

    System.Drawing.Image rImg =new Bitmap(pWidth, pHeight);
    Graphics aGC = Graphics.FromImage(rImg);
    aGC.Clear(pBkColor);
    if (fitX)
    {
    int x =0;
    int y = offset;
    aGC.DrawImage(pSource, x, y, rWidth, rHeight);
    }
    else
    {
    int x = offset;
    int y =0;
    aGC.DrawImage(pSource, x, y, rWidth, rHeight);
    }
    return rImg;
    }
    ///<summary>
    /// PageChanging
    ///</summary>
    ///<param name="sender"></param>
    ///<param name="e"></param>
    protectedvoid List_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    productsList.PageIndex = e.NewPageIndex;
    InitProductsList();
    }
    复制代码

              

                作者:Alexliu(alex dotNet Learning)
          出处:http://alexliu.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,转载请注明。并且保留文章链接。否则保留追究法律责任的权利。

    分类: ASP.NET
     
  • 相关阅读:
    改造vant日期选择
    css3元素垂直居中
    npm综合
    (转)网页加水印方法
    Mac下IDEA自带MAVEN插件的全局环境配置
    隐藏注册控件窗口
    High performance optimization and acceleration for randomWalk, deepwalk, node2vec (Python)
    How to add conda env into jupyter notebook installed by pip
    The Power of WordNet and How to Use It in Python
    背单词app测评,2018年
  • 原文地址:https://www.cnblogs.com/woshare/p/2630685.html
Copyright © 2011-2022 走看看