GDI+中对图片的裁剪


ContractedBlock.gif ExpandedBlockStart.gif CutImage
 1void CutImage(HttpPostedFile post,string ppuid,out string imagename) 
 2ExpandedBlockStart.gifContractedBlock.gif    
 3        System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream); 
 4        if (SourceImg.Height > ConfigHelper.UserFaceMaxHeight) 
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        
 6            this._lbl_upload_msg.Text = "最大高度不得大于 " + ConfigHelper.UserFaceMaxHeight; 
 7            return
 8        }
 
 9        if (SourceImg.Width > ConfigHelper.UserFaceMaxWidth) 
10ExpandedSubBlockStart.gifContractedSubBlock.gif        
11            this._lbl_upload_msg.Text = "最大宽度不得大于 " + ConfigHelper.UserFaceMaxWidth; 
12            return
13        }
 
14        ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName)); 
15        string filename =  ppuid+"."+format.ToString(); 
16        imagename = filename; 
17
18        if (!UserFaceDir.EndsWith("\\"))  
19            UserFaceDir = UserFaceDir+"\\"
20        filename = UserFaceDir + filename; 
21        int SourceImgWidth = SourceImg.Width; 
22        int SourceImgHeight = SourceImg.Height; 
23        if ((SourceImgWidth != ConfigHelper.UserFaceWidth) && (SourceImgHeight != ConfigHelper.UserFaceHeight)) 
24ExpandedSubBlockStart.gifContractedSubBlock.gif        
25            //如果宽高比例为1:1,则直接构成缩略图 
26            if (((Double)SourceImgWidth / SourceImgHeight) == 1
27ExpandedSubBlockStart.gifContractedSubBlock.gif            
28                System.Drawing.Image thumbimg = SourceImg.GetThumbnailImage(ConfigHelper.UserFaceWidth, ConfigHelper.UserFaceHeight, null, IntPtr.Zero); 
29                thumbimg.Save(filename, format); 
30                thumbimg.Dispose(); 
31                SourceImg.Dispose(); 
32                return
33            }
 
34            Bitmap bit = new Bitmap(SourceImg); 
35            Rectangle rec = new Rectangle();   //构造一个Rectangle类,一个矩形 
36            rec.Width = ConfigHelper.UserFaceWidth;   
37            rec.Height = ConfigHelper.UserFaceHeight; 
38            if (SourceImgWidth > rec.Width) 
39                rec.X = (SourceImgWidth - rec.Width) / 2
40            else 
41ExpandedSubBlockStart.gifContractedSubBlock.gif            
42                rec.X = 0
43                rec.Width = SourceImg.Width; 
44            }
 
45            if (SourceImgHeight > rec.Height) 
46                rec.Y = (SourceImgHeight - rec.Height) / 2
47            else 
48ExpandedSubBlockStart.gifContractedSubBlock.gif            
49                rec.Y = 0
50                rec.Height = SourceImg.Height; 
51            }
 
52
53            try 
54ExpandedSubBlockStart.gifContractedSubBlock.gif            
55                //这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪 
56                bit.Clone(rec, PixelFormat.DontCare).Save(filename, format); 
57            }
 
58            catch (Exception ex) 
59ExpandedSubBlockStart.gifContractedSubBlock.gif            
60                this._lbl_upload_msg.Text = ex.Message; 
61                return
62            }
 
63            finally 
64ExpandedSubBlockStart.gifContractedSubBlock.gif            
65                bit.Dispose(); 
66                SourceImg.Dispose(); 
67            }
 
68        }
 
69    }
 
70

转载于:https://www.cnblogs.com/0000/archive/2009/11/26/1611248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值