画像の解像度に影響を及ぼさずに画像を等比例縮小する

1188 ワード

#region      
    /// 
    ///      
    /// 
    ///        
    private void SaveImage(string filePath)
    {
        string fileName = Path.GetExtension(File1.PostedFile.FileName).ToLower();
        string _filename = DateTime.Now.ToString("yyMMddHHmm");

        System.Drawing.Image image = null, aNewImage = null;

        //         
        int newwidth = 0, newheight = 0;
        image = System.Drawing.Image.FromFile(filePath);
        System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

        //       
        int phWidth = image.Width;
        int phHeight = image.Height;

        //          
        if (phWidth > 110)
        {
            //    
            newwidth = 110;
            //        
            newheight = phHeight * newwidth / phWidth;
        }
        aNewImage = image.GetThumbnailImage(newwidth, newheight, callb, new System.IntPtr());
        //           
        aNewImage.Save(Server.MapPath("~/upload/") + "Avaster_" + DateTime.Now.ToString("yyMMddHHmm") + fileName);
        image.Dispose();
    }

    #endregion