我用了FileUpload1来做上传,我现在想在上传图片的时候我能够把图片变小以后在上传,该如何实现,麻烦告知谢谢!我用的是C#语言,麻烦给个完整的例子谢谢!

解决方案 »

  1.   

    use the f*cking google!
      

  2.   

    我这里有个创建缩略图的例子,楼主看下能用不
    http://blog.csdn.net/durongjian/archive/2009/05/14/4183109.aspx
      

  3.   

    上传后再改变大小,
    //  保存图片,必要时还要缩小
                if (this.File1.PostedFile.ContentType.IndexOf("image") == -1)
                {                CCClient.AlertMessage("只能上传图片文件!");
                    return;
                }
                System.Drawing.Image img = System.Drawing.Image.FromStream(this.File1.PostedFile.InputStream);
                int iMaxWidth = 240;
                int iMaxHeight = 320;            int iNewWidth = 0;
                int iNewHeight = 0;
                if (img.Width > iMaxWidth || img.Height > iMaxHeight)
                {
                    iNewWidth = iMaxHeight * img.Width / img.Height;
                    iNewHeight = iMaxWidth * img.Height / img.Width;
                    if (iNewWidth > iMaxWidth) 还是宽了,以宽为标准计算
                    {
                        iNewWidth = iMaxWidth;                }
                    else if (iNewHeight > iMaxHeight)
                    {
                        iNewHeight = iMaxHeight;
                    }                img  = img.GetThumbnailImage(iNewWidth, iNewHeight, null, IntPtr.Zero);
                    System.Drawing.Imaging.ImageCodecInfo e = null;
                    System.Drawing.Imaging.ImageCodecInfo[] es = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                   // 扩展名;
                   string ext = this.File1.PostedFile.ContentType.ToLower();
                   if (ext.IndexOf("gif") > -1)
                   {
                       ext = "gif";
                   }
                   else
                   {
                       ext = "jpeg";
                   }
                    foreach (System.Drawing.Imaging.ImageCodecInfo ef in es)
                    {
                      
                        if (ef.MimeType.ToLower().IndexOf(ext) > -1)
                        {
                            e = ef;
                        }
                    }
                    System.Drawing.Imaging.EncoderParameters ps = new System.Drawing.Imaging.EncoderParameters();
                    ps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);                img.Save(NewPath, e, ps);
                }
                else
                {
                    this.File1.PostedFile.SaveAs(NewPath);
                }
     
      

  4.   

    http://blog.csdn.net/slimboy123/archive/2007/08/11/1738217.aspx这里有源码
      

  5.   

    GetThumbnailImage的效果并不是很好,所以要改成这种方法。    System.Drawing.Bitmap objNewBitMap = new Bitmap(iNewWidth, iNewHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                          
                            Graphics objGraphics = Graphics.FromImage(objNewBitMap);
                            
                            objGraphics.Clear(Color.Transparent);
                           
                            objGraphics.DrawImage(img, new Rectangle(0, 0, iNewWidth, iNewHeight));
                            objNewBitMap.Save(NewPath, e, ps);