图片缩放

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Cry18
{
 /// <summary>
 /// WebForm3 的摘要说明。
 /// </summary>
 public class WebForm3 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Image Image1;
  protected System.Web.UI.WebControls.Image Image2;
  protected System.Web.UI.HtmlControls.HtmlInputFile File1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
    this.Button1.Attributes["onclick"]="return OkUp();";
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   if(this.File1.PostedFile.ContentLength>0)
   {
    string ctype=this.File1.PostedFile.ContentType;
    if(!(ctype=="image/pjpeg"||ctype=="image/gif"||ctype=="image/bmp"))
    {
     Response.Write("<script>alert('文件格式不对!');</script>");
     Response.End ();
    }
   }
      
    string[] files=System.IO.Directory.GetFiles(Server.MapPath("Img"));
   int len=files.Length ;
   System.Random ra=new Random(len);
   string path="",bpath="";
   string nt="";
   bool ok=false;
   do
   {
    len++;
    nt=ra.Next(len,len+1).ToString ();
    path=Server.MapPath("Img/"+nt+".jpg");
    bpath=Server.MapPath("Img/"+nt+"b.jpg");
    ok=System.IO.File.Exists(path);
   }while(ok==true);
   Cry18.Config.Cry18Config  cfg=Cry18.Config.Cry18Config.Instance ();
   this.SaveImg(cfg.ProductImgWidth,cfg.ProductImgHight,this.File1.PostedFile.InputStream,path);
   this.SaveImg(cfg.BigImgWidth,cfg.BigImgHight,this.File1.PostedFile.InputStream,bpath);

   this.Image1.ImageUrl="Img/"+nt+".jpg";
   this.Image2.ImageUrl="Img/"+nt+"b.jpg";
   

  }
  private void SaveImg(int width,int hi,System.IO.Stream imgStream,string toFile)
  {

   System.Drawing.Bitmap myImage=System.Drawing.Image.FromStream(imgStream) as System.Drawing.Bitmap;
   int nWidth=width;
   int nHeight=hi;
   System.Drawing.Bitmap newbmp=new System.Drawing.Bitmap(nWidth,nHeight,System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
   int iX,iY,yMax,xMax;
   for(iX=0;iX<nWidth;iX++)
   {
    for(iY=0;iY<nHeight;iY++)
    {
     newbmp.SetPixel(iX,iY,System.Drawing.Color.White);
    }
   }
   if(nWidth<myImage.Width || nHeight<myImage.Height )
   {
    float f1=myImage.Width ,f2=myImage.Height ,n1=nWidth,n2=nHeight;
    float dx=f1/f2-n1/n2;
    if(myImage.Width/myImage.Height >nWidth/nHeight)
    {
     xMax=nWidth;
     yMax=(int)(f2*nWidth /f1);
    }
    else
    {
     yMax=nHeight;
     xMax=(int)(f1*n2/f2);
    }
   }
    
   else
   {
    xMax=myImage.Width;
    yMax=myImage.Height ;
   }
  
   System.Drawing.Bitmap tmpbmp=new System.Drawing.Bitmap(myImage,xMax,yMax);
   xMax=(newbmp.Width -tmpbmp.Width )/2;
   yMax=(newbmp.Height-tmpbmp.Height )/2;
   for(iX=0;iX<tmpbmp.Width ;iX++)
   {
    for(iY=0;iY<tmpbmp.Height;iY++)
    {
     newbmp.SetPixel(iX+xMax,iY+yMax,tmpbmp.GetPixel(iX,iY));
    }
   }
   
   myImage.Dispose ();
   tmpbmp.Dispose ();
   newbmp.Save(toFile,System.Drawing.Imaging.ImageFormat.Jpeg );
   newbmp.Dispose ();
  }
 }

}

posted on 2005-10-17 16:39  老代哥哥  阅读(135)  评论(0编辑  收藏  举报

导航