Sub crop_with_calque() 'fonction de coupage
Dim percentLeft#, percentTop#, percentRight#, percentBottom#, origWidth#, origHeight#, pict As Picture, Calque As Shape
Dim temp
Set pict = ActiveSheet.Pictures("origin"): Set Calque = ActiveSheet.Shapes("calque")
'******************************************
'correctif depassement du calque si un de ses bord est en dhors de l'image
If Calque.Left < pict.Left Then temp = pict.Left - Calque.Left: Calque.Left = pict.Left: Calque.Width = Calque.Width - temp
If Calque.Top < pict.Top Then temp = pict.Top - Calque.Top: Calque.Top = pict.Top: Calque.Height = Calque.Height - temp
If Calque.Left + Calque.Width > (pict.Left + pict.Width) Then
Calque.Width = Calque.Width - ((Calque.Left + Calque.Width) - (pict.Left + pict.Width))
End If
If Calque.Top + Calque.Height > (pict.Top + pict.Height) Then
Calque.Height = Calque.Height - ((Calque.Top + Calque.Height) - (pict.Top + pict.Height))
End If
'***********************************************
'correctif "divizion by zero" coome ca
If Calque.Left - pict.Left > 0 Then percentLeft = 100 / (pict.Width / (Calque.Left - pict.Left)) Else percentLeft = 0
If Calque.Top - pict.Top > 0 Then percentTop = 100 / (pict.Height / (Calque.Top - pict.Top)) Else percentTop = 0
'ou comme ca
percentRight = 100 - (100 / (pict.Width / (Application.Max((1 / 100000000000#), Calque.Left - pict.Left) + Calque.Width)))
percentBottom = 100 - (100 / (pict.Height / (Application.Max((1 / 100000000000#), Calque.Top - pict.Top) + Calque.Height)))
With pict.ShapeRange '(1)
'on fait une copie temporaire pour récupérer les dimention originales!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
With .Duplicate: .ScaleWidth 1, True: origWidth = .Width: origHeight = .Height: .Delete: End With
With .PictureFormat
'Crop-->(Left,Top,Right,Bottom) = original Size(width ou height) * pourcentage / 100
.CropLeft = origWidth * (percentLeft / 100)
.CropRight = origWidth * (percentRight / 100)
.CropTop = origHeight * (percentTop / 100)
.CropBottom = origHeight * (percentBottom / 100)
End With
.Left = (([d3:F11].Width - .Width) / 2) + [d3:F11].Left
.Top = (([d3:F11].Height - .Height) / 2) + [d3:F11].Top
End With
With Feuil1.Shapes("calque"): .ZOrder msoBringToFront: .Top = [c2].Top: .Left = [c2].Left: .Width = 1: .Height = 1: End With
Feuil1.CommandButton2.Enabled = False: Feuil1.CommandButton3.Enabled = False: Feuil1.CommandButton4.Enabled = False
Feuil1.CommandButton5.Enabled = True
[d3:F11].Select
End Sub