IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Position d'une zone dans une image


Sujet :

Windows Presentation Foundation

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut Position d'une zone dans une image
    Bonjour,


    Je récupére une image provenant d'une caméra, en faisant une capture d'écran.
    L'image provenant de la caméra correspondra toujours à la même zone d'observation, aux mêmes positions X,Y,Z.
    Dans cette image je veux sélectionner une zone, obtenir la position exacte de cette zone dans le but de déplacer automatiquement un microscope sur cette zone.
    Comment à partir de cette image obtenir la position(X,Y) de la zone? (en passant par matrice de pixel? si oui comment?,...)

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour

    Du code svp ...quel est ton souci au niveau du code....
    Moi j'ai un rader ,comment le pointer sur une position exacte dans une image,si tu peux repondre...

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    Bonjour,

    En fait pour l'instant je n'ai pas de soucis particulier niveau code. C'est juste que je ne sais pas comment me lancer et partir, pour l'instant (notamment à l'aide d'un bout de code que tu as fourni) je récupére un buffer des bytes correspondant à l'image. et J'aurai voulu savoir, si il existe un moyen de dessiner une zone et de récupérer les info correspondante comme la position, à la zone dessinée sur l'image

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    Private Sub bntCapture_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            imgCapture.Source = imgVideo.Source
            Dim frame As BitmapFrame
            ' = DirectCast(Me.imgCapture.Source, BitmapFrame)
            frame = BitmapFrame.Create(imgCapture.Source)
     
            Dim ms As New MemoryStream()
            Dim encoder As New BmpBitmapEncoder()
            encoder.Frames.Add(BitmapFrame.Create(frame))
            encoder.Save(ms)
     
     
     
            'AFFICHE DU MemoryStream
            bi = New BitmapImage()
     
            Dim buffer As Byte() = New Byte(ms.Length - 1) {}
            ms.Position = 0
            ms.Read(buffer, 0, CInt(ms.Length) - 1)
     
            'test  buffer modifié mis dans image
            Dim buffer2 As Byte() = New Byte(ms.Length - 1) {}
            buffer2 = buffer
     
            Dim a As Long = 76853
            Do While a < 154000
                buffer2(a) = 0
                a = a + 4
     
     
            Loop
            MsgBox(buffer.Length)
            ms.Position = 0
            ms.Write(buffer2, 0, CInt(ms.Length) - 1)
            bi.BeginInit()
            ms.Position = 0
            ' TRES TRES IMPORTANT CECI ...!!!
            bi.CacheOption = BitmapCacheOption.OnLoad
            'Mise en cached memoire
            bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat
            bi.StreamSource = ms
            bi.EndInit()
     
            Me.imgFromByte.Source = bi
     
            'Libere Ressources
            ms.Close()
            encoder = Nothing
            webcam.Stop()
        End Sub

  4. #4
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour SkaknYoshi

    C'est un peu plus clair cette fois ci..

    Pour dessiner Pixel par Pixel dans une zone sur un Bitmap vierge ou Existant :
    -WriteableBitmap est ton ami...
    Il dispose de l'arsenal necessaire :
    -WriteableBitmap.WritePixels(srcRec as Int32Rec,ArrayPixel as Byte,stride as Integer,offsetDest as Integer)
    C'est un peu le Bitmap.SetPixel(x,y) combine à Bitmap.LockBits mais plus costaud
    -il peut prendre carrement une zone rectangle srcRec..
    -utilise l'interop comme et donc tres rapide...

    1er exemple adapte par mes soins à vb.net qui dessine sur un bitmap vierge à la souris...
    (voir lien de l'original MSDN en c# ci-apres):
    code xaml du simple Form(le control image et son wbitmap sont crees par code):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    <Window x:Class="WinWriteableBitmapMsdn"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WinWriteableBitmapMsdn" Height="300" Width="300"
        Loaded="Window_Loaded"
        MouseWheel="Window_MouseWheel"    >
     
    </Window>
    code behind .vb du form:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
     
    Imports System.Runtime.InteropServices
     
    Public Class WinWriteableBitmapMsdn
        Shared writeableBitmap As WriteableBitmap
        Shared WithEvents i As Image
     
     
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            i = New Image()
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor)
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased)
     
     
     
            Me.Content = i
            AddHandler i.MouseMove, AddressOf i_MouseMove
            AddHandler i.MouseLeftButtonDown, AddressOf i_MouseLeftButtonDown
            AddHandler i.MouseRightButtonDown, AddressOf i_MouseRightButtonDown
     
     
            AddHandler Me.MouseWheel, AddressOf Window_MouseWheel
        End Sub
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
     
            writeableBitmap =
              New WriteableBitmap(
                      CInt(Me.ActualWidth),
                     CInt(Me.ActualHeight),
                      96,
                      96,
                      PixelFormats.Bgr32,
                      Nothing)
     
            i.Source = writeableBitmap
     
            i.Stretch = Stretch.None
            i.HorizontalAlignment = HorizontalAlignment.Left
            i.VerticalAlignment = VerticalAlignment.Top
        End Sub
     
     
        Private Sub i_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            DrawPixel(e)
        End Sub
        Private Sub i_MouseRightButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            ErasePixel(e)
        End Sub
        Private Sub i_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
            If e.LeftButton = MouseButtonState.Pressed Then
                DrawPixel(e)
            ElseIf e.RightButton = MouseButtonState.Pressed Then
                ErasePixel(e)
            End If
        End Sub
        Private Sub DrawPixel(ByVal e As MouseEventArgs)
            Dim column As Integer = CInt(e.GetPosition(i).X)
            Dim row As Integer = CInt(e.GetPosition(i).Y)
     
            '-----C'EST => DU BITMAP.LOCKBITS-----
            'Reserve the back buffer for updates.
            writeableBitmap.Lock()
     
            'Get a pointer to the back buffer.
            Dim pBackBuffer As Integer = writeableBitmap.BackBuffer.ToInt32
     
            'Find the address of the pixel to draw.
            pBackBuffer += row * writeableBitmap.BackBufferStride
            pBackBuffer += column * 4
     
     
            'Compute the pixel's color.
     
            Dim color_data As Integer = 255 << 16 ' R
            color_data = color_data Or 128 << 8   ' G
            color_data = color_data Or 255 << 0   ' B
     
            Marshal.WriteInt32(pBackBuffer, color_data)
     
            'Specify the area of the bitmap that changed.
            writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
     
            '-----C'EST => DU BITMAP.UNLOCKBITS-----
            'Release the back buffer and make it available for display.
            writeableBitmap.Unlock()
     
        End Sub
        Private Sub ErasePixel(ByVal e As MouseEventArgs)
            Dim ColorData() As Byte = {
                0, 0, 0, 0
                } ' B G R
     
            '-----EQUIVALENT = > SETPIXEL WINFORM-------- 
            Dim rect As Int32Rect = New Int32Rect(
                     CInt(e.GetPosition(i).X),
                     CInt(e.GetPosition(i).Y),
                      1,
                      1)
     
            writeableBitmap.WritePixels(rect, ColorData, 4, 0)
     
        End Sub
     
     
        Private Sub Window_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
            Dim m As Matrix = i.RenderTransform.Value
            If e.Delta > 0 Then
     
                m.ScaleAt(
                    1.5,
                    1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            Else
                m.ScaleAt(
                    1.0 / 1.5,
                    1.0 / 1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            End If
     
            i.RenderTransform = New MatrixTransform(m)
        End Sub
     
    End Class
    2e exemple dessine sur un bitmap existant recuper des resources du form...:
    code xaml du simple Form:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    <Window x:Class="Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300"
            Loaded="Window_Loaded"
        MouseWheel="Window_MouseWheel" >
        <Window.Resources>
            <Image 
                x:Key="img"
                Stretch="None"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Source="Resources/noel2007.jpg"
                >
            </Image>
        </Window.Resources>
        <Image 
            x:Name="i"
            MouseLeftButtonDown="i_MouseLeftButtonDown"
            MouseMove="i_MouseMove"
            MouseRightButtonDown="i_MouseRightButtonDown" 
               >
        </Image >
    </Window>
    code behind du Form:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    Imports System.Runtime.InteropServices
    Public Class Window2
        Private writeableBitmap As WriteableBitmap
     
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor)
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased)
     
        End Sub
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim img As Image = CType(Me.FindResource("img"), Image)
            Dim frame As BitmapFrame = img.Source
     
            'copie  image dans  WriteableBitmap
            writeableBitmap =
              New WriteableBitmap(frame)
     
            i.Source = writeableBitmap
     
            i.Stretch = Stretch.None
            i.HorizontalAlignment = HorizontalAlignment.Left
            i.VerticalAlignment = VerticalAlignment.Top
        End Sub
        Private Sub i_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            DrawPixel(e)
        End Sub
        Private Sub i_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            ErasePixel(e)
        End Sub
        Private Sub i_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
            If e.LeftButton = MouseButtonState.Pressed Then
                DrawPixel(e)
            ElseIf e.RightButton = MouseButtonState.Pressed Then
                ErasePixel(e)
            End If
        End Sub
        Private Sub DrawPixel(ByVal e As MouseEventArgs)
            Dim column As Integer = CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96)
            Dim row As Integer = CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96)
     
            '-----C'EST => DU BITMAP.LOCKBITS-----
            'Reserve the back buffer for updates.
            writeableBitmap.Lock()
     
            'Get a pointer to the back buffer.
            Dim pBackBuffer As Integer = writeableBitmap.BackBuffer.ToInt32
     
            'Find the address of the pixel to draw.
            pBackBuffer += row * writeableBitmap.BackBufferStride
            pBackBuffer += column * 4
     
     
            'Compute the pixel's color.
     
            Dim color_data As Integer = 255 << 16 ' R
            color_data = color_data Or 128 << 8   ' G
            color_data = color_data Or 255 << 0   ' B
     
            Marshal.WriteInt32(pBackBuffer, color_data)
     
            'Specify the area of the bitmap that changed.
            writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
     
            '-----C'EST => DU BITMAP.UNLOCKBITS-----
            'Release the back buffer and make it available for display.
            writeableBitmap.Unlock()
     
        End Sub
        Private Sub ErasePixel(ByVal e As MouseEventArgs)
            Dim ColorData() As Byte = {
                0, 0, 0, 0
                } ' B G R
     
            '-----EQUIVALENT = > SETPIXEL WINFORM-------- 
            Dim rect As Int32Rect = New Int32Rect(
                     CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96),
                     CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96),
                      1,
                      1)
     
            writeableBitmap.WritePixels(rect, ColorData, 4, 0)
     
        End Sub
     
        Private Sub Window_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
     
            Dim m As Matrix = i.RenderTransform.Value
            If e.Delta > 0 Then
     
                m.ScaleAt(
                    1.5,
                    1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            Else
                m.ScaleAt(
                    1.0 / 1.5,
                    1.0 / 1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            End If
     
            i.RenderTransform = New MatrixTransform(m)
        End Sub
     
     
    End Class
    NB: un point essential dans ce 2eme Exemple pour la gestion de la souris :
    -quand on utilise un bitmap existant il faut tenir compte de sa resolution ou DpiX et DpiY dans la gestion des coords x,y de souris obtenus avec la methode e.GetPosition(UIElement).X et e.GetPosition(UIElement).Y ...sinon les positions x,y du rectangle de mise à jour IntRec32 seront fausses....

    En bonus ,tu as le "zoom pixel" dans mouse_wheel...
    bon code....

  5. #5
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    Re

    Oups!!!

    le lien msdn que j'ai oublie(code c#):

    http://www.google.fr/url?q=http://ms...09L2MjbH6VP4QQ
    bon code...

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    Super merci pour ta réponse, je regarde ça dès que possible et je tiens tout le monde au courant, en attendant j'essaye de résoudre un souci de mémoire.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    J'ai testé la seconde version ailleurs que dans mon programme pour bien comprendre le fonctionnement, mais je ne comprends pas tout, en effet, je n'arrive pas à avoir l'image

    avec les codes correspondant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    '
    ' Created by SharpDevelop.
    ' User: Anthony
    ' Date: 17/02/2014
    ' Time: 14:44
    ' 
    ' To change this template use Tools | Options | Coding | Edit Standard Headers.
    '
    Imports System.Runtime.InteropServices
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Input
     
    Public Class MainWindow
        Private writeableBitmap As WriteableBitmap
     
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor)
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased)
     
        End Sub
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim img As Image = CType(Me.FindResource("img"), Image)
            Dim frame As BitmapFrame = img.Source
     
            'copie  image dans  WriteableBitmap
            writeableBitmap =
              New WriteableBitmap(frame)
     
            i.Source = writeableBitmap
     
            i.Stretch = Stretch.None
            i.HorizontalAlignment = HorizontalAlignment.Left
            i.VerticalAlignment = VerticalAlignment.Top
        End Sub
        Private Sub i_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            DrawPixel(e)
        End Sub
        Private Sub i_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            ErasePixel(e)
        End Sub
        Private Sub i_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
            If e.LeftButton = MouseButtonState.Pressed Then
                DrawPixel(e)
            ElseIf e.RightButton = MouseButtonState.Pressed Then
                ErasePixel(e)
            End If
        End Sub
        Private Sub DrawPixel(ByVal e As MouseEventArgs)
            Dim column As Integer = CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96)
            Dim row As Integer = CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96)
     
            '-----C'EST => DU BITMAP.LOCKBITS-----
            'Reserve the back buffer for updates.
            writeableBitmap.Lock()
     
            'Get a pointer to the back buffer.
            Dim pBackBuffer As Integer = writeableBitmap.BackBuffer.ToInt32
     
            'Find the address of the pixel to draw.
            pBackBuffer += row * writeableBitmap.BackBufferStride
            pBackBuffer += column * 4
     
     
            'Compute the pixel's color.
     
            Dim color_data As Integer = 255 << 16 ' R
            color_data = color_data Or 128 << 8   ' G
            color_data = color_data Or 255 << 0   ' B
     
            Marshal.WriteInt32(pBackBuffer, color_data)
     
            'Specify the area of the bitmap that changed.
            writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
     
            '-----C'EST => DU BITMAP.UNLOCKBITS-----
            'Release the back buffer and make it available for display.
            writeableBitmap.Unlock()
     
        End Sub
        Private Sub ErasePixel(ByVal e As MouseEventArgs)
            Dim ColorData() As Byte = {
                0, 0, 0, 0
            } ' B G R
     
            '-----EQUIVALENT = > SETPIXEL WINFORM-------- 
            Dim rect As Int32Rect = New Int32Rect(
                     CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96),
                     CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96),
                      1,
                      1)
     
            writeableBitmap.WritePixels(rect, ColorData, 4, 0)
     
        End Sub
     
        Private Sub Window_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
     
            Dim m As Matrix = i.RenderTransform.Value
            If e.Delta > 0 Then
     
                m.ScaleAt(
                    1.5,
                    1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            Else
                m.ScaleAt(
                    1.0 / 1.5,
                    1.0 / 1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            End If
     
            i.RenderTransform = New MatrixTransform(m)
        End Sub
     
     
    End Class
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?xml version="1.0" encoding="utf-8"?>
    <Window
    	x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	Title="MainWindow"
    	Height="300"
    	Width="300"
    	Loaded="Window_Loaded"
    	MouseWheel="Window_MouseWheel">
    	<Window.Resources>
    		<Image
    			x:Key="img"
    			Stretch="None"
    			HorizontalAlignment="Left"
    			VerticalAlignment="Top"
    			Source="Resources/Capture03.jpg"></Image>
    	</Window.Resources>
    	<Grid>
    	<Image
    		x:Name="i"
    		MouseLeftButtonDown="i_MouseLeftButtonDown"
    		MouseMove="i_MouseMove"
    		MouseRightButtonDown="i_MouseRightButtonDown"></Image></Grid>
    </Window>

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    En fait c'est moi qui faisait un mauvais appel a mon image ressource.

    Après avoir tester, je peux dessiner des pixels,
    je suis en train de l'insérer dans mon programme

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    Bonjour,

    Maintenant je peux dessiner sur mon image, mais toujours pas de zone sélection sous forme de rectangle.

    Pour l'instant pour mes tests, je fais un snap de l'image obtenue par la caméra et je "copie" cette nouvelle image dans une autre. C'est sur cette dernière que je souhaite dessiner ce qui est le cas or j'ai un souci, l'image ne prend que sur 640*480 alors que celle précédente fait 2592*1944. Et je ne comprends pas car height et width prenne les bonnes valeurs de départ.

    mon code vb.net :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    Imports System
    Imports System.IO
    Imports System.Linq
    Imports System.Collections.Generic
    Imports System.Collections.ObjectModel
    Imports System.Text
     
    Imports System.Windows.Threading
    Imports System.Windows.Media.Animation
    Imports System.Threading
    Imports System.Windows.Resources
    Imports System.Reflection
    Imports WebCam_Capture
     
     
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Runtime.InteropServices
     
    Imports System.Windows.Data
    Imports System.Windows.Documents
    Imports System.Windows.Input
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Shapes
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Net
    Imports System.Threading.Tasks
     
     
    Class Window1
        Private writeableBitmap As WriteableBitmap
        Private bi As BitmapImage
        Private webcam As WebCam
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor)
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased)
     
        End Sub
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            webcam = New WebCam()
            webcam.InitializeWebCam(imgVideo)
        End Sub
     
        Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            webcam.Start()
     
        End Sub
     
        Private Sub bntStop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            webcam.Stop()
        End Sub
        Private Sub bntContinue_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            webcam.Continue()
        End Sub
     
        Private Sub bntCapture_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            imgCapture.Source = imgVideo.Source
            Dim frame As BitmapFrame
            ' = DirectCast(Me.imgCapture.Source, BitmapFrame)
            frame = BitmapFrame.Create(imgCapture.Source)
     
            Dim ms As New MemoryStream()
            Dim encoder As New BmpBitmapEncoder()
            encoder.Frames.Add(BitmapFrame.Create(frame))
            encoder.Save(ms)
     
     
     
            'AFFICHE DU MemoryStream
            bi = New BitmapImage()
            'bi.BeginInit()
            'ms.Position = 0
            ' TRES TRES IMPORTANT CECI ...!!!
            'bi.CacheOption = BitmapCacheOption.OnLoad
            'Mise en cached memoire
            'bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat
            'bi.StreamSource = ms
            'bi.EndInit()
            '
     
     
            '  WHATEVER YOU WANT : storage dans un buffer pixels
            Dim buffer As Byte() = New Byte(ms.Length - 1) {}
            ms.Position = 0
            ms.Read(buffer, 0, CInt(ms.Length) - 1)
     
            'test  buffer modifié mis dans image
            Dim buffer2 As Byte() = New Byte(ms.Length - 1) {}
            buffer2 = buffer
     
            Dim a As Long = 76854
            Do While a < 254000
                buffer2(a) = 255
                a = a + 1
     
     
            Loop
            ' MsgBox(buffer.Length)
            ms.Position = 0
            ms.Write(buffer2, 0, CInt(ms.Length) - 1)
            bi.BeginInit()
            ms.Position = 0
            ' TRES TRES IMPORTANT CECI ...!!!
            bi.CacheOption = BitmapCacheOption.OnLoad
            'Mise en cached memoire
            bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat
            bi.StreamSource = ms
            bi.EndInit()
            ' Dim destRect1 As Rect(200, 10, 20, 16)
     
     
     
            ' Me.imgFromByte.Source = bi.SourceRect(0, 0, 100, 100)
            'copie  image dans  WriteableBitmap
            WriteableBitmap =
              New WriteableBitmap(frame)
     
            i.Source = WriteableBitmap
     
            i.Stretch = Stretch.None
            i.HorizontalAlignment = HorizontalAlignment.Left
            i.VerticalAlignment = VerticalAlignment.Top
            i.Height = frame.Height
     
     
            'i.Width = frame.Width
            MsgBox(i.Height)
            MsgBox(i.Width)
            MsgBox(writeableBitmap.DpiX)
     
            'Libere Ressources
            ms.Close()
            encoder = Nothing
            webcam.Stop()
        End Sub
     
        Private Sub bntSaveImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Helper.SaveImageCapture(DirectCast(imgCapture.Source, BitmapSource))
        End Sub
        Private Sub i_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            DrawPixel(e)
        End Sub
        Private Sub i_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            ErasePixel(e)
        End Sub
        Private Sub i_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
            If e.LeftButton = MouseButtonState.Pressed Then
                DrawPixel(e)
            ElseIf e.RightButton = MouseButtonState.Pressed Then
                ErasePixel(e)
            End If
        End Sub
        Private Sub DrawPixel(ByVal e As MouseEventArgs)
            Dim column As Integer = CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96)
            Dim row As Integer = CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96)
     
            '-----C'EST => DU BITMAP.LOCKBITS-----
            'Reserve the back buffer for updates.
            writeableBitmap.Lock()
     
            'Get a pointer to the back buffer.
            Dim pBackBuffer As Integer = writeableBitmap.BackBuffer.ToInt32
     
            'Find the address of the pixel to draw.
            pBackBuffer += row * writeableBitmap.BackBufferStride
            pBackBuffer += column * 4
     
     
            'Compute the pixel's color.
     
            Dim color_data As Integer = 255 << 16 ' R
            color_data = color_data Or 255 << 8   ' G
            color_data = color_data Or 0 << 0   ' B
     
            Marshal.WriteInt32(pBackBuffer, color_data)
     
            'Specify the area of the bitmap that changed.
            writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
     
            '-----C'EST => DU BITMAP.UNLOCKBITS-----
            'Release the back buffer and make it available for display.
            writeableBitmap.Unlock()
     
        End Sub
        Private Sub ErasePixel(ByVal e As MouseEventArgs)
            Dim ColorData() As Byte = {
                255, 255, 255, 255
            } ' B G R
     
            '-----EQUIVALENT = > SETPIXEL WINFORM-------- 
            Dim rect As Int32Rect = New Int32Rect(
                     CInt(e.GetPosition(i).X * writeableBitmap.DpiX / 96),
                     CInt(e.GetPosition(i).Y * writeableBitmap.DpiY / 96),
                     1,
                     1)
     
            writeableBitmap.WritePixels(rect, ColorData, 4, 0)
     
        End Sub
     
        Private Sub Window_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
     
            Dim m As Matrix = i.RenderTransform.Value
            If e.Delta > 0 Then
     
                m.ScaleAt(
                    1.5,
                    1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            Else
                m.ScaleAt(
                    1.0 / 1.5,
                    1.0 / 1.5,
                    e.GetPosition(Me).X,
                    e.GetPosition(Me).Y)
            End If
     
            i.RenderTransform = New MatrixTransform(m)
        End Sub
    xaml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
                <Image Width="640" Height="480"   x:Name="i"  Stretch="Fill" MouseLeftButtonDown="i_MouseLeftButtonDown"
    		MouseMove="i_MouseMove"
    		MouseRightButtonDown="i_MouseRightButtonDown" Margin="623,736,-138,-397">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform />
                            <SkewTransform/>
                            <RotateTransform Angle="-90"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
     
                <Image Width="640" Height="480" x:Name="imgCapture" Stretch="Fill" Margin="35,736,0,-397" OpacityMask="Black" HorizontalAlignment="Left">
     
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform Angle="-90"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 66
    Points : 42
    Points
    42
    Par défaut
    J'ai reussi en utilisant DrawingSelectionBackDemo que j'ai trouvé sur le net en c#, et adapté à mon code en vb.net en virant notamment l'appel a endpoint

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Afficher une checkbox dans une feuille si une checkbox d'une autre feuille est cochée
    Par JessieCoutas dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 18/08/2009, 13h35
  2. portée d'une variable dans une fonction dans une méthode
    Par laurentg2003 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 29/06/2009, 19h05
  3. [POO] dans une classe, appeler une fonction dans une méthode
    Par arnaudperfect dans le forum Langage
    Réponses: 3
    Dernier message: 26/08/2007, 23h04
  4. Envoyer une formulaire dans une page dans une Frame
    Par zooffy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 29/06/2007, 10h13
  5. Recherche une valeur d'une cellule dans une colonne d'une autre feuille
    Par kourria dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 21/06/2007, 13h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo