ASP.NET使用数据库存储、读取并修改图片

在SQL SERVER中建立这样结构的一个表:

列名类型目的
IDInteger主键ID
IMGTITLEVarchar(50)图片的标题
IMGTYPEVarchar(50)图片类型. ASP.NET要以辨认的类型
IMGDATAImage用于存储二进制数据

 

==============================================================================================     using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Data.SqlClient;
using System.IO;

public partial class ChangeImageSize : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

        private int width;
        private int height;
        private Bitmap newpic, savepic;
        private float k1, k2;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ChangeImageSize()
        {
        }
              

        /// <summary>
        /// 传入一张图片,并设定它的大小
        /// </summary>
        /// <param name="TheImage">传入的图片对象</param>
        /// <param name="TheWidth">图片宽的范围</param>
        /// <param name="TheHeight">图片长的范围</param>
        /// <param name="path">保存的文件名和路径</param>
        public Bitmap ChangePicMethod(object TheImage, int TheWidth, int TheHeight)
        {
            newpic = new Bitmap((System.Drawing.Image)TheImage);
            this.width = newpic.Width;
            this.height = newpic.Height;

            k1 = (float)width / (float)TheWidth;
            k2 = (float)height / (float)TheHeight;
            if (k1 > k2)
            {
                this.width = (int)(width / k1);
                this.height = (int)(height / k1);
            }
            else
            {
                this.width = (int)(width / k2);
                this.height = (int)(height / k2);
            }
            savepic = new Bitmap(newpic, width, height);
            return savepic;
            //savepic.Save(@path);
        }

    /// <summary>
    /// 保存图片到数据库
    /// </summary>
    /// <returns></returns>
        private bool StoreImage()
        {
            bool rs = false;
            Stream imgdatastream = this.FileUpload1.PostedFile.InputStream;
            int imgdatalen = this.FileUpload1.PostedFile.ContentLength;
            string imgtype = this.FileUpload1.PostedFile.ContentType;
            string imgtitle =FileUpload1.PostedFile.FileName;
            // byte[] imgdata = new byte[imgdatalen];
            //int n = imgdatastream.Read(imgdata, 0, imgdatalen);
            //Bitmap bp = ChangePicToSaveMethod(imgdatastream, 200, 200);
           
            MemoryStream ms = new MemoryStream();
            Bitmap bp = new Bitmap(imgdatastream);
            bp = ChangePicMethod(bp, 200, 200);
            //bp=bp.GetThumbnailImage(200,200,new System.Drawing.Image.GetThumbnailImageAbort(aa()) ,System.IntPtr.Zero);
            bp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Flush();
            byte[] imgdata = ms.GetBuffer();
            ms.Close();  

            SqlConnection connection = Connection.getConnection();
            SqlCommand command = new SqlCommand("INSERT INTO ImageStore(ImageTitle,ImageType,ImageData)VALUES ( @imgtitle, @imgtype,@imgdata )", connection);

            SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar, 50);

            paramTitle.Value = imgtitle;
            command.Parameters.Add(paramTitle);

            SqlParameter paramData = new SqlParameter("@imgdata", SqlDbType.Image);
            paramData.Value = imgdata;
            command.Parameters.Add(paramData);

            SqlParameter paramType = new SqlParameter("@imgtype", SqlDbType.VarChar, 50);
            paramType.Value = imgtype;
            command.Parameters.Add(paramType);

            connection.Open();
            int numRowsAffected = command.ExecuteNonQuery();
            connection.Close();
            rs = true;
            return rs;
        }
    /// <summary>
    /// 从数据库取出图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
        private void GetImage(string imgid)
        {
            string sql = "SELECT ImageData, ImageType FROM ImageStore WHERE ID = " + imgid;
            SqlConnection connection = Connection.getConnection();
            SqlCommand command = new SqlCommand(sql, connection);
            connection.Open();
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                Response.ContentType = dr["ImageType"].ToString();
                Response.BinaryWrite((byte[])dr["ImageData"]);
            }
            connection.Close();

        }

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值