zoukankan      html  css  js  c++  java
  • asp.net(c#)上传图片生成缩略图

        根据网上代码自己该动了些
    #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 ConStr ="UID=sa,PWD=sa,Server=local,Database=mydb";
       SqlConnection Conn = new SqlConnection(ConStr);
        Conn.Open();
            SqlCommand  myCommand =new SqlCommand();
         myCommand.Connection=Conn;
            myCommand.CommandText="insert into [UpImage] (imageName,image,imgType,SmallImage) values (@ImgName,@FileByte,@imgType,@SmallImage)";
         myCommand.Parameters.Add("@ImgName",ImgName);
         myCommand.Parameters.Add("@FileByte",FileByte);
         myCommand.Parameters.Add("@imgType",imgType);
         myCommand.Parameters.Add("@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]);
       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

    ////图片显示页的代码
     myCommand.CommandText="select smallimage from [UpImage] ";
       SqlDataReader dr =db.myCommand.ExecuteReader();
       this.Response.ContentType="image/gif";
       while(dr.Read())
       {
        Response.BinaryWrite((byte[])dr["smallimage"]);
          }

  • 相关阅读:
    2018年Q1增长最快的20个技能
    微信备份方法
    [转] 浅谈 OpenResty
    Angular默认路由策略-PathLocationStrategy策略页面刷新报404错误解决方案
    [转] ABP框架Web API跨域问题的解决方案
    [转] Win10插入U盘后双击无法打开,无法访问,显示设备未就绪;驱动哥帮你解决
    java jdk 8u191 官网下载地址
    IDEA 2019 注册码
    [JAVA] maven 阿里云节点 settings.xml
    .net webapi跨域 web.config配置
  • 原文地址:https://www.cnblogs.com/wangergo/p/284473.html
Copyright © 2011-2022 走看看