asp.net(c#)上傳圖片產生縮圖

來源:互聯網
上載者:User

    根據網上代碼自己該動了些
#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"]);
      }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.