栀子花开

追求完美

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 Dim SourceImg As System.Drawing.Image = System.Drawing.Image.FromStream(Me.FileUpload1.PostedFile.InputStream)
        
Dim SourceImgWidth As Integer = SourceImg.Width '图片的原始Width 
        Dim SourceImgHeight As Integer = SourceImg.Height '图片的原始Height 

        
Dim bit As New Bitmap(SourceImg)


        
Dim rec As New Rectangle() '构造一个Rectangle类,一个矩形 
        rec.Width = 100 'ConfigHelper.UserFaceWidth
        rec.Height = 100 'ConfigHelper.UserFaceHeight

        
If SourceImgWidth > rec.Width Then
            rec.X 
= 20 '(SourceImgWidth - rec.Width) / 2
        Else
            rec.X 
= 0
            rec.Width 
= SourceImg.Width
        
End If

        
If SourceImgHeight > rec.Height Then
            rec.Y 
= 20 '(SourceImgHeight - rec.Height) / 2
        Else
            rec.Y 
= 0
            rec.Height 
= SourceImg.Height
        
End If

        
Try
            
'这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪 
            bit.Clone(rec, PixelFormat.DontCare).Save(Response.OutputStream, ImageFormat.Jpeg)

        
Catch ex As Exception
            Context.Response.Write(ex.Message)
            
Exit Sub
        
Finally
            bit.Dispose()
            SourceImg.Dispose()
        
End Try
posted on 2007-06-28 17:23  杨林  阅读(387)  评论(0编辑  收藏  举报