Traitement sur image : rotation 90°, effets 3d, conversion en niveau de gris, inverser des couleurs...

Description

Exemple de code VB pour faire une rotation d'image, un effets 3D, une conversion en niveau de gris, ou inverser simplement les couleurs... Le tous TRES RAPIDEMENT (passage par des API de base du GDI).

Source / Exemple :


VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Pivoter Bitmap"
   ClientHeight    =   7425
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7350
   LinkTopic       =   "Form1"
   ScaleHeight     =   7425
   ScaleWidth      =   7350
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Round 
      Caption         =   "effet 3D"
      Height          =   375
      Left            =   5160
      TabIndex        =   4
      Top             =   120
      Width           =   855
   End
   Begin VB.CommandButton Gray 
      Caption         =   "Gay"
      Height          =   375
      Left            =   3720
      TabIndex        =   3
      Top             =   120
      Width           =   1095
   End
   Begin VB.CommandButton Inverse 
      Caption         =   "Inverse"
      Height          =   375
      Left            =   2280
      TabIndex        =   2
      Top             =   120
      Width           =   1095
   End
   Begin VB.PictureBox Pic1 
      Appearance      =   0  'Flat
      AutoRedraw      =   -1  'True
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BorderStyle     =   0  'None
      ForeColor       =   &H80000008&
      Height          =   4860
      Left            =   480
      Picture         =   "Bitmap.frx":0000
      ScaleHeight     =   324
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   432
      TabIndex        =   1
      Top             =   840
      Width           =   6480
   End
   Begin VB.CommandButton CmdPivot 
      Caption         =   "Pivoter"
      Height          =   375
      Left            =   840
      TabIndex        =   0
      Top             =   120
      Width           =   1095
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'-------------------------------------------------------------------------------------------------------
' (C) Patrick MOIRE
' http://jeux.cartes.free.fr
'-------------------------------------------------------------------------------------------------------
' Le code Original de la rotation n'est pas de moi. Je l'ai corrigé (ne fonctionnait pas dans toutes les
' résolution, et apporté les autres effets...
'-------------------------------------------------------------------------------------------------------

Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Private Type BITMAPINFOHEADER
    biSize As Long
    biWidth As Long
    biHeight As Long
    biPlanes As Integer
    biBitCount As Integer
    biCompression As Long
    biSizeImage As Long
    biXPelsPerMeter As Long
    biYPelsPerMeter As Long
    biClrUsed As Long
    biClrImportant As Long
End Type

Private Type RGBQUAD
    rgbBlue As Byte
    rgbGreen As Byte
    rgbRed As Byte
    rgbReserved As Byte
End Type

Private Type BITMAPINFO
    bmiHeader As BITMAPINFOHEADER
    bmiColors As RGBQUAD
End Type

Dim BMPINFO As BITMAPINFO
Dim BMPINFOH As BITMAPINFOHEADER

 Dim Buffer() As RGBQUAD
 
 '-------------------------------------------------------------------------------------------------------
Private Sub CmdPivot_Click()

 '- - - Working
  Dim Buffer() As RGBQUAD
  Dim Result() As RGBQUAD
  Dim PicInfo As BITMAP
  Dim Width As Long
  Dim Height As Long
  Dim hdc As Long
  
 '- - - Capture un "Device Contexte" valide !
  hdc = GetDC(GetForegroundWindow())

 '- - - Copie de l'image courante dans le tampon
  GetObject Me.Pic1.Image, Len(PicInfo), PicInfo
  Width = PicInfo.bmWidth
  Height = PicInfo.bmHeight
 
  ReDim Buffer(0 To Width - 1, 0 To Height - 1)
  With BMPINFOH
    .biBitCount = 32
    .biHeight = Height
    .biWidth = Width
    .biPlanes = 1
    .biSize = Len(BMPINFOH)
  End With
  BMPINFO.bmiHeader = BMPINFOH
  GetDIBits hdc, Me.Pic1.Image.Handle, 0, Height, Buffer(0, 0), BMPINFO, 0
  
 '- - - Rotation...
  Dim X As Long
  Dim Y As Long
  Dim XX As Long
  Dim YY As Long
  X = UBound(Buffer, 1)
  Y = UBound(Buffer, 2)
  ReDim Result(0 To Y, 0 To X)
  For XX = 0 To X
    For YY = 0 To Y
      Result(Y - YY, XX) = Buffer(XX, YY) 'Result(Yy, X - Xx) = Buffer(Xx, Yy)
    Next YY
  Next XX

 '- - - modifiy l'aspect du controle Picture (cas ou il n'est pas carré !)
  Dim l As Single
  l = Me.Pic1.Width
  Me.Pic1.Width = Pic1.Height
  Me.Pic1.Height = l

  With BMPINFOH
    .biBitCount = 32
    .biHeight = Width
    .biWidth = Height
    .biPlanes = 1
    .biSize = Len(BMPINFOH)
  End With
  BMPINFO.bmiHeader = BMPINFOH
  
 '- - - Restaure l'image sur le controle
  Set Me.Pic1.Picture = Nothing
  SetDIBits hdc, Me.Pic1.Image.Handle, 0, Width, Result(0, 0), BMPINFO, 0
  
End Sub

'-------------------------------------------------------------------------------------------------------
' Merci à Jacky, Martin !
Private Sub Gray_Click()
  
 '- - - - Working
  Dim Cnt As Long
  Dim PicBits() As Byte
  Dim PicInfo As BITMAP
  Dim Gray As Byte
    
   '- - - Une Seul fois !
    Me.Gray.Enabled = False
   
   '- - - Lecture caracteristique de l'image
    GetObject Me.Pic1.Image, Len(PicInfo), PicInfo
    ReDim PicBits(1 To PicInfo.bmWidth * PicInfo.bmHeight * 4) As Byte
    GetBitmapBits Pic1.Image, UBound(PicBits), PicBits(1)
   
   '- - - Conversion des bits couleur selon le standard de l'International Telecommunication Union (ITU) : Gray scale=(222*Red+707*Green+71*Blue)/1000
   ' Voir http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html
    For Cnt = 1 To UBound(PicBits) Step 4
       Gray = (222 * CLng(PicBits(Cnt)) + 707 * CLng(PicBits(Cnt + 1)) + 71 * CLng(PicBits(Cnt + 2))) / 1000
       PicBits(Cnt) = Gray
       PicBits(Cnt + 1) = Gray
       PicBits(Cnt + 2) = Gray
    Next Cnt
    
   '- - - Restaure l'image sur le controle
    SetBitmapBits Me.Pic1.Image, UBound(PicBits), PicBits(1)
    Me.Pic1.Refresh
    
End Sub

'-------------------------------------------------------------------------------------------------------
Private Sub Inverse_Click()
 
 '- - - - Working
  Dim Cnt As Long, BytesPerLine As Long
  Dim PicBits() As Byte, PicInfo As BITMAP
  
   '- - - Lecture caracteristique de l'image
    GetObject Me.Pic1.Image, Len(PicInfo), PicInfo
    BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
    ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte
    GetBitmapBits Me.Pic1.Image.Handle, UBound(PicBits), PicBits(1)
    
   '- - - Inverse les bits...
    For Cnt = 1 To UBound(PicBits)
        PicBits(Cnt) = 255 - PicBits(Cnt)
    Next Cnt
    
   '- - - Restaure l'image sur le controle
    SetBitmapBits Me.Pic1.Image.Handle, UBound(PicBits), PicBits(1)
    Me.Pic1.Refresh
    
End Sub

'-------------------------------------------------------------------------------------------------------
Private Sub Round_Click()

 '- - - Working
  Dim PicInfo As BITMAP
'  Dim Buffer() As RGBQUAD
  Dim hdc As Long

 '- - - Une Seul fois !
  Me.Round.Enabled = False

 '- - - Copie de l'image courante dans le tampon
  GetObject Me.Pic1.Image, Len(PicInfo), PicInfo
  ReDim Buffer(0 To PicInfo.bmWidth - 1, 0 To PicInfo.bmHeight - 1)
  With BMPINFOH
    .biBitCount = 32
    .biHeight = PicInfo.bmHeight
    .biWidth = PicInfo.bmWidth
    .biPlanes = 1
    .biSize = Len(BMPINFOH)
  End With
  BMPINFO.bmiHeader = BMPINFOH

  hdc = GetDC(GetForegroundWindow())
  GetDIBits hdc, Me.Pic1.Image.Handle, 0, PicInfo.bmHeight, Buffer(0, 0), BMPINFO, 0
 
 '- - - Rotation...
  Dim X As Long
  Dim Y As Long
  Dim XX As Long
  Dim YY As Long
  X = UBound(Buffer, 1)
  Y = UBound(Buffer, 2)
  Dim Taux As Integer
  
  For XX = 0 To 5
    For YY = XX To Y - XX
      Taux = (120 - 20 * XX)
      Buffer(XX, YY).rgbBlue = Maxi(0, Buffer(XX, YY).rgbBlue - Taux)
      Buffer(XX, YY).rgbGreen = Maxi(0, Buffer(XX, YY).rgbGreen - Taux)
      Buffer(XX, YY).rgbRed = Maxi(0, Buffer(XX, YY).rgbRed - Taux)
      Buffer(X - XX, YY).rgbBlue = Mini(255, Buffer(X - XX, YY).rgbBlue + Taux)
      Buffer(X - XX, YY).rgbGreen = Mini(255, Buffer(X - XX, YY).rgbGreen + Taux)
      Buffer(X - XX, YY).rgbRed = Mini(255, Buffer(X - XX, YY).rgbRed + Taux)
    Next YY
  Next XX

  For YY = 0 To 5
    For XX = YY To X - YY
      Taux = (120 - 20 * YY)
      Buffer(XX, YY).rgbBlue = Maxi(0, Buffer(XX, YY).rgbBlue - Taux)
      Buffer(XX, YY).rgbGreen = Maxi(0, Buffer(XX, YY).rgbGreen - Taux)
      Buffer(XX, YY).rgbRed = Maxi(0, Buffer(XX, YY).rgbRed - Taux)
      Buffer(XX, Y - YY).rgbBlue = Mini(255, Buffer(XX, Y - YY).rgbBlue + Taux)
      Buffer(XX, Y - YY).rgbGreen = Mini(255, Buffer(XX, Y - YY).rgbGreen + Taux)
      Buffer(XX, Y - YY).rgbRed = Mini(255, Buffer(XX, Y - YY).rgbRed + Taux)
    Next XX
  Next YY

 '- - - Restaure l'image sur le controle
  Set Me.Pic1.Picture = Nothing
  SetDIBits hdc, Me.Pic1.Image.Handle, 0, PicInfo.bmHeight, Buffer(0, 0), BMPINFO, 0
 
End Sub

Private Function Mini(V1 As Integer, V2 As Integer) As Byte
  If V1 > V2 Then Mini = V2 Else Mini = V1
End Function

Private Function Maxi(V1 As Integer, V2 As Integer) As Byte
  If V1 > V2 Then Maxi = V1 Else Maxi = V2
End Function

Conclusion :


Le code Original de la rotation n'est pas de moi. Je l'ai corrigé (ne fonctionnait pas dans toutes les résolutions, et apporté les autres effets...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.