Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Errore: L'oggetto è correntemente utilizzato altrove.
Forum - C# / VB.NET - Errore: L'oggetto è correntemente utilizzato altrove. - Pagina 2

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 11:26
Venerdì, 12/02/2010
Quella funzione non ha parametri, e non vedo traccia di .Clone(), quindi dove hai usato questa funzione?

In alternativa, prova con la prima soluzione che ti avevo proposto.

PM Quote
Avatar
Federico1976 (Normal User)
Rookie


Messaggi: 42
Iscritto: 23/03/2009

Segnala al moderatore
Postato alle 13:08
Venerdì, 12/02/2010
L'immagine la clono  dal form principale quando istanzio la classe dove risiedono le due funzione Parser e area
gli assegno limmagine
Dim imgInfo As New ImmagineInfo 'classe in questione
        imgInfo.Immagine = DirectCast(pbxImage.Image.Clone, Bitmap)

comunque levato il delegato ho risolto il problema dell'errore
ho creato due variabili shared  e un timer sul form principale che le legge e aggiorna la scrolbar
in pratica
      If immagine Is Nothing Then 'questa è l'immagine originale clonata assegnata alla classe
            Exit Sub
        End If
immagineRed = DirectCast(Immagine.Clone, Bitmap) 'questa e l'immagine che modifico e assegno piu volte al controllo

PM Quote
Avatar
Federico1976 (Normal User)
Rookie


Messaggi: 42
Iscritto: 23/03/2009

Segnala al moderatore
Postato alle 13:25
Venerdì, 12/02/2010
funzione che istanzia la classe:
Codice sorgente - presumibilmente VB.NET

  1. Private Sub AvviaRicecaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AvviaRicecaToolStripMenuItem.Click
  2.         Dim imgInfo As New ImmagineInfo
  3.         imgInfo.Immagine = DirectCast(pbxImage.Image.Clone, Bitmap)
  4.         imgInfo.Control = pbxImage
  5.         imgInfo.CoefficenteSfondo = nudCoefficenteS.Value
  6.         imgInfo.CoefficenteOggetto = nudCoefficenteO.Value
  7.         imgInfo.PuntiPerPallino = PixelOgg
  8.  
  9.         If TrovaOggettiDiPiuColoriToolStripMenuItem.Checked = False Then
  10.             imgInfo.Funzione = ImmagineInfo.Funzioni.TrovaOggettiDiUnColore
  11.         Else
  12.             imgInfo.Funzione = ImmagineInfo.Funzioni.TrovaOggettiEscludendoSfondo
  13.         End If
  14.         If btnColorS.BackColor <> Color.White Then
  15.             imgInfo.ColoreSfondo = btnColorS.BackColor
  16.         End If
  17.         If btnColorO.BackColor <> Color.White Then
  18.             imgInfo.ColoreOggetto = btnColorO.BackColor
  19.         Else
  20.             MsgBox("Inpostare il colore Dello oggetto")
  21.         End If
  22.         Dim thrInfo As New Threading.Thread(AddressOf imgInfo.Parser)
  23.         thrInfo.Start()
  24.         aggiornamento.Start()
  25.         'imgInfo.Parser()
  26.  
  27.     End Sub




Classe:
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Drawing.Drawing2D
  2. Imports System.Drawing
  3. Imports System.Windows.Forms
  4. Imports System.Drawing.Imaging
  5.  
  6. Public Class ImmagineInfo
  7.     Enum Funzioni
  8.         TrovaOggettiEscludendoSfondo = 1
  9.         TrovaOggettiDiUnColore = 2
  10.     End Enum
  11. #Region "Variabili E Proprieta Globali"
  12.     Private _coloreSfondo As Color
  13.     Private _coloreOggetto As Color
  14.     Private Oggetto As OggettiImmagine
  15.     Private _Oggetti As New List(Of OggettiImmagine)
  16.     Public _immagine As Bitmap
  17.     Private _controllo As Object
  18.     Private _CoefficenteSfondo As Integer = 30
  19.     Private _CoefficenteOggetto As Integer = 30
  20.     Private PuntiInseriti As New List(Of Integer)
  21.     Private immagineRed As Bitmap
  22.     Private Funz As Funzioni = Funzioni.TrovaOggettiDiUnColore
  23.     Private _PuntiPerPallino As Integer
  24.     Public Shared value As Integer
  25.     Public Shared valueMax As Integer
  26.  
  27.     Public Property PuntiPerPallino() As Integer
  28.         Get
  29.             Return _PuntiPerPallino
  30.         End Get
  31.         Set(ByVal value As Integer)
  32.             _PuntiPerPallino = value
  33.         End Set
  34.     End Property
  35.     Public Property CoefficenteSfondo() As Integer
  36.         Get
  37.             Return _CoefficenteSfondo
  38.         End Get
  39.         Set(ByVal value As Integer)
  40.             _CoefficenteSfondo = value
  41.         End Set
  42.     End Property
  43.     Public Property Funzione() As Funzioni
  44.         Get
  45.             Return Funz
  46.         End Get
  47.         Set(ByVal value As Funzioni)
  48.             Funz = value
  49.         End Set
  50.     End Property
  51.     Public Property Red_Imagine() As Bitmap
  52.         Get
  53.             Return immagineRed
  54.         End Get
  55.         Set(ByVal value As Bitmap)
  56.             immagineRed = value
  57.         End Set
  58.     End Property
  59.     Public Property ColoreSfondo() As Color
  60.         Get
  61.             Return _coloreSfondo
  62.         End Get
  63.         Set(ByVal value As Color)
  64.             _coloreSfondo = value
  65.         End Set
  66.     End Property
  67.     Public Property ColoreOggetto() As Color
  68.         Get
  69.             Return _coloreOggetto
  70.         End Get
  71.         Set(ByVal value As Color)
  72.             _coloreOggetto = value
  73.         End Set
  74.     End Property
  75.     Public ReadOnly Property Oggetti() As List(Of OggettiImmagine)
  76.         Get
  77.             Return _Oggetti
  78.         End Get
  79.     End Property
  80.     Public Property Immagine() As Bitmap
  81.         Get
  82.             Return _immagine
  83.         End Get
  84.         Set(ByVal value As Bitmap)
  85.             _immagine = value
  86.         End Set
  87.     End Property
  88.     Public Property Control() As Object
  89.         Get
  90.             Return _controllo
  91.         End Get
  92.         Set(ByVal value As Object)
  93.             _controllo = value
  94.         End Set
  95.     End Property
  96.     Public Property CoefficenteOggetto() As Integer
  97.         Get
  98.             Return _CoefficenteOggetto
  99.         End Get
  100.         Set(ByVal value As Integer)
  101.             _CoefficenteOggetto = value
  102.         End Set
  103.     End Property
  104.  
  105. #End Region
  106.  
  107.  
  108.     Private Delegate Sub SetImageDelegate(ByVal imm As Bitmap, ByVal controllo As Object)
  109.     Private Sub SetImage(ByVal imm As Bitmap, ByVal controllo As Object)
  110.         If controllo.InvokeRequired Then
  111.             controllo.BeginInvoke(New SetImageDelegate(AddressOf SetImage), New Object() {imm, controllo})
  112.             Exit Sub
  113.         End If
  114.         'controllo.Invalidate()
  115.         controllo.image = imm
  116.     End Sub
  117.  
  118.     ''' <summary>
  119.     ''' La Classe permette di contere le vare aree in un immagine
  120.     ''' </summary>
  121.     ''' <param name="imm">Immagine da Parsare.</param>
  122.     ''' <param name="coefficenteOgg">Coefficente Degli oggetti Per il Confronto con immagini Sfocate.</param>
  123.     ''' <param name="CoefficenteSfondo">Coefficente Dello sfondo Per il Confronto con immagini Sfocate.</param>
  124.     ''' <remarks></remarks>
  125.     Sub New(ByVal imm As Bitmap, ByVal coefficenteOgg As Integer, ByVal CoefficenteSfondo As Integer, Optional ByVal Controllo As Object = Nothing)
  126.         immagine = imm
  127.         CoefficenteSfondo = CoefficenteSfondo
  128.         CoefficenteOggetto = coefficenteOgg
  129.         Control = Controllo
  130.         Parser()
  131.     End Sub
  132.     Sub New()
  133.  
  134.     End Sub
  135.     Sub Parser()
  136.         If immagine Is Nothing Then 'immagine originale da non toccare
  137.             Exit Sub
  138.         End If
  139.         Dim Larghezza As Integer = immagine.Width - 1 'Larghezza
  140.         Dim Altezza As Integer = immagine.Height - 1 'Altezza
  141.         Dim x As Integer
  142.         Dim y As Integer
  143.         Dim ColoreAttuale As Color
  144.  
  145.         ' GDI+ still lies to us - the return format is BGR, NOT RGB.
  146.         Dim bmData As BitmapData = Immagine.LockBits(New Rectangle(0, 0, Immagine.Width, Immagine.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  147.  
  148.         Dim stride As Integer = bmData.Stride
  149.         Dim Scan0 As System.IntPtr = bmData.Scan0
  150.         Dim bytes As Integer = Immagine.Width * Immagine.Height * 3
  151.         Dim p(bytes - 1) As Byte
  152.         ' Copy the RGB values into the array.
  153.         System.Runtime.InteropServices.Marshal.Copy(Scan0, p, 0, bytes)
  154.  
  155.         'finire da qui e lavorare su p variabile puntatore di area di memoria
  156.  
  157.         If Funzione = Funzioni.TrovaOggettiDiUnColore Then
  158.             If _coloreSfondo = Nothing Then
  159.                 MsgBox("Per La funzione di ricerca degli oggetti Di un Colore bisogna selezionare pripa il colore da catturare")
  160.                 Exit Sub
  161.             End If
  162.         Else 'Se Gli oggetti sono di piu colori iposto il colore di sfondo
  163.             ' e inposto la variabile boleana a false cosi che ogni colore diverso lo considera un oggettto
  164.             'Trova_Escludi = False
  165.             If _coloreSfondo = Nothing Then
  166.                 _coloreSfondo = Color.FromArgb(p(0), p(1), p(2)) '
  167.             End If
  168.         End If
  169.         immagineRed = DirectCast(Immagine.Clone, Bitmap)immagine che modifico e assegno 'al controllo tramite delegato
  170.  
  171.  
  172.         Dim c As Integer = 0
  173.         Dim nOffset As Integer = stride - bmData.Width * 3
  174.         valueMax = Altezza
  175.  
  176.         For y = 0 To Altezza
  177.             value = y
  178.             For x = 0 To Larghezza
  179.                 'Debug.Assert(Control.InvokeRequired = False)
  180.                 If y = 42 Then
  181.                     Threading.Thread.Sleep(10)
  182.                 End If
  183.                 'ColoreAttuale = Immagine.GetPixel(x, y)
  184.                 ColoreAttuale = Color.FromArgb(p(c + 2), p(c + 1), p(c))
  185.                 'Application.DoEvents()
  186.  
  187.                 If PuntiInseriti.IndexOf(c) = -1 Then
  188.                     If IsNearestColor(_coloreSfondo, ColoreAttuale, CoefficenteSfondo) = False Then 'Funzione Di Confronto Colore con coefficente
  189.                         Application.DoEvents()
  190.                         Oggetto = New OggettiImmagine
  191.                         Oggetto.coefficente = CoefficenteOggetto
  192.                         Oggetto.colore = ColoreAttuale
  193.                         Oggetto.point = Area(c, CoefficenteOggetto)
  194.                         If Oggetto.point.Count > PuntiPerPallino Then
  195.                             _Oggetti.Add(Oggetto)
  196.  
  197.                         End If
  198.                     End If
  199.                 End If
  200.                 c += 3
  201.             Next
  202.             c += nOffset
  203.         Next
  204.         If Control IsNot Nothing Then
  205.             SetImage(immagineRed, Control)
  206.         End If
  207.         Immagine.UnlockBits(bmData)
  208.         MsgBox("Ho Trovato N° Oggetti " & Oggetti.Count & " di Colore ")
  209.     End Sub
  210.  
  211.     Private Shared Function CercaUnPunto(ByVal p As Point, ByVal list As List(Of Point))
  212.         For Each punto As Point In list
  213.             If punto = p Then
  214.                 Return True
  215.             End If
  216.         Next
  217.         Return False
  218.     End Function
  219.  
  220.     'Strumento secchiello
  221.     Public Function Area(ByVal c As Integer, ByVal Coefficente As Integer) As List(Of Integer)
  222.         Dim Points As New List(Of Integer)
  223.         Dim NewPoints As New List(Of Integer)
  224.         Dim ColoreAttuale As Color = Nothing
  225.         Dim Col_Oggetto As Color
  226.         Dim ArrayPoint As New List(Of Integer)
  227.         '
  228.         Points.Add(c)
  229.         Dim bSrc As Bitmap = DirectCast(immagineRed.Clone(), Bitmap)
  230.  
  231.         ' GDI+ still lies to us - the return format is BGR, NOT RGB.
  232.         Dim bmData As BitmapData = immagineRed.LockBits(New Rectangle(0, 0, immagineRed.Width, immagineRed.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  233.         Dim bmSrc As BitmapData = bSrc.LockBits(New Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  234.  
  235.         Dim SrcScan0 As System.IntPtr = bmSrc.Scan0
  236.         Dim stride As Integer = bmData.Stride
  237.         Dim Scan0 As System.IntPtr = bmData.Scan0
  238.         Dim bytes As Integer = immagineRed.Width * immagineRed.Height * 3
  239.         Dim p2(bytes - 1) As Byte
  240.         Dim bytes1 As Integer = immagineRed.Width * immagineRed.Height * 3
  241.         Dim pSrc(bytes1 - 1) As Byte
  242.  
  243.         ' Copy the RGB values into the array.
  244.         System.Runtime.InteropServices.Marshal.Copy(Scan0, p2, 0, bytes)
  245.         System.Runtime.InteropServices.Marshal.Copy(SrcScan0, pSrc, 0, bytes1)
  246.  
  247.         Dim nOffset As Integer = stride - bmData.Width * 3
  248.         ' sicurezza che la variabile "Col_Oggetto" venga inpostata manualmente su un colore di
  249.         'un oggetto per evitare la ripetizione della ricerca dei punti inseriti
  250.  
  251.  
  252.         If Funzione = Funzioni.TrovaOggettiDiUnColore Then
  253.             If ColoreOggetto = Nothing Then
  254.                 Col_Oggetto = Color.FromArgb(pSrc(c + 2), pSrc(c + 1), pSrc(c))
  255.             Else
  256.                 Col_Oggetto = ColoreOggetto
  257.             End If
  258.         Else
  259.             Col_Oggetto = Color.FromArgb(pSrc(c + 2), pSrc(c + 1), pSrc(c))
  260.         End If
  261.         Do
  262.  
  263.             For Each K As Integer In Points
  264.                 'Application.DoEvents()
  265.                 Dim sin As Integer
  266.                 Dim dest As Integer
  267.                 Dim sop As Integer
  268.                 Dim Sot As Integer
  269.  
  270.                 'testa il punto a destra di questo
  271.                 If K > 3 Then
  272.                     sin = K - 3
  273.                 End If
  274.  
  275.                 ColoreAttuale = Color.FromArgb(pSrc(sin + 2), pSrc(sin + 1), pSrc(sin))
  276.                 If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then 'Funzione Di Confronto Colore con coefficente
  277.  
  278.                     If PuntiInseriti.IndexOf(sin) = -1 Then
  279.                         p2(sin + 2) = Color.Red.R
  280.                         p2(sin + 1) = Color.Red.G
  281.                         p2(sin) = Color.Red.B
  282.  
  283.                         NewPoints.Add(sin)
  284.  
  285.                         PuntiInseriti.Add(sin)
  286.                         ArrayPoint.Add(sin)
  287.  
  288.                         PuntiInseriti.Add(sin + 1)
  289.                         ArrayPoint.Add(sin + 1)
  290.  
  291.  
  292.                         PuntiInseriti.Add(sin + 2)
  293.                         ArrayPoint.Add(sin + 2)
  294.                     End If
  295.  
  296.                 End If
  297.                 If dest < (bytes - 3) Then
  298.                     dest = K + 3
  299.                 End If
  300.  
  301.                 ColoreAttuale = Color.FromArgb(pSrc(dest + 2), pSrc(dest + 1), pSrc(dest))
  302.                 If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  303.  
  304.                     If PuntiInseriti.IndexOf(dest) = -1 Then
  305.                         p2(dest + 2) = Color.Red.R
  306.                         p2(dest + 1) = Color.Red.G
  307.                         p2(dest) = Color.Red.B
  308.  
  309.                         NewPoints.Add(dest)
  310.  
  311.                         PuntiInseriti.Add(dest)
  312.                         ArrayPoint.Add(dest)
  313.  
  314.                         PuntiInseriti.Add(dest + 1)
  315.                         ArrayPoint.Add(dest + 1)
  316.  
  317.  
  318.                         PuntiInseriti.Add(dest + 2)
  319.                         ArrayPoint.Add(dest + 2)
  320.                     End If
  321.  
  322.                 End If
  323.                 If K > (stride) Then
  324.                     sop = K - stride
  325.                 End If
  326.  
  327.                 ColoreAttuale = Color.FromArgb(pSrc(sop + 2), pSrc(sop + 1), pSrc(sop))
  328.                 If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  329.  
  330.                     If PuntiInseriti.IndexOf(sop) = -1 Then
  331.                         p2(sop + 2) = Color.Red.R
  332.                         p2(sop + 1) = Color.Red.G
  333.                         p2(sop) = Color.Red.B
  334.  
  335.                         NewPoints.Add(sop)
  336.  
  337.                         PuntiInseriti.Add(sop)
  338.                         ArrayPoint.Add(sop)
  339.  
  340.                         PuntiInseriti.Add(sop + 1)
  341.                         ArrayPoint.Add(sop + 1)
  342.  
  343.                         PuntiInseriti.Add(sop + 2)
  344.                         ArrayPoint.Add(sop + 2)
  345.                     End If
  346.  
  347.                 End If
  348.  
  349.                 If Sot < (bytes - stride) Then
  350.                     Sot = K + stride
  351.                 End If
  352.  
  353.                 If Sot < bytes Then
  354.                     ColoreAttuale = Color.FromArgb(pSrc(Sot + 2), pSrc(Sot + 1), pSrc(Sot))
  355.                     If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  356.  
  357.                         If PuntiInseriti.IndexOf(Sot) = -1 Then
  358.                             p2(Sot + 2) = Color.Red.R
  359.                             p2(Sot + 1) = Color.Red.G
  360.                             p2(Sot) = Color.Red.B
  361.  
  362.                             NewPoints.Add(Sot)
  363.  
  364.                             PuntiInseriti.Add(Sot)
  365.                             ArrayPoint.Add(Sot)
  366.  
  367.                             PuntiInseriti.Add(Sot + 1)
  368.                             ArrayPoint.Add(Sot + 1)
  369.  
  370.  
  371.                             PuntiInseriti.Add(Sot + 2)
  372.                             ArrayPoint.Add(Sot + 2)
  373.                         End If
  374.  
  375.                     End If
  376.                 End If
  377.             Next
  378.             Points.Clear()
  379.             Points.AddRange(NewPoints)
  380.             NewPoints.Clear()
  381.  
  382.         Loop Until Points.Count = 0
  383.  
  384.         ' Copy the RGB values back to the bitmap
  385.         System.Runtime.InteropServices.Marshal.Copy(pSrc, 0, SrcScan0, bytes1)
  386.         System.Runtime.InteropServices.Marshal.Copy(p2, 0, Scan0, bytes)
  387.  
  388.         immagineRed.UnlockBits(bmData)
  389.         bSrc.UnlockBits(bmSrc)
  390.  
  391.         Return ArrayPoint
  392.     End Function
  393.  
  394.  
  395.  
  396.     ''' <summary>
  397.     ''' Funzione supplementare per determinare
  398.     ''' se il colore2 e nelle vicinanze del colore1
  399.     ''' </summary>
  400.     ''' <param name="color1">Colore del origine</param>
  401.     ''' <param name="Color2">Colore da controllare</param>
  402.     ''' <param name="coefficiente"> <code> integer </code> da 0 a 255(differenza tra i componenti ARDB dei colori </param>
  403.     ''' <returns>Restituisce  <code>true </code>, se la differenza tra i componenti ARGB del colore2
  404.     '''  e quelli del colore1, non supera il coefficiente, altrimenti <code>false</code></returns>
  405.     ''' <remarks></remarks>
  406.     Public Function IsNearestColor(ByVal color1 As Color, ByVal Color2 As Color, ByVal coefficiente As Integer) As Boolean
  407.         Dim ADif As Integer = Math.Abs(CInt(color1.A) - CInt(Color2.A))
  408.         Dim Rdif As Integer = Math.Abs(CInt(color1.R) - CInt(Color2.R))
  409.         Dim Gdif As Integer = Math.Abs(CInt(color1.G) - CInt(Color2.G))
  410.         Dim Bdif As Integer = Math.Abs(CInt(color1.B) - CInt(Color2.B))
  411.  
  412.         If ADif < coefficiente _
  413.         And Rdif < coefficiente _
  414.         And Gdif < coefficiente _
  415.         And Bdif < coefficiente _
  416.         Then
  417.             Return True
  418.         Else
  419.             Return False
  420.         End If
  421.     End Function
  422.  
  423.  
  424.  
  425. End Class
  426. Public Class OggettiImmagine
  427.     Public point As New List(Of Integer)
  428.     Public coefficente As Integer
  429.     Public colore As Color
  430. End Class




Ultima modifica effettuata da Federico1976 il 12/02/2010 alle 13:27
PM Quote
Avatar
Federico1976 (Normal User)
Rookie


Messaggi: 42
Iscritto: 23/03/2009

Segnala al moderatore
Postato alle 14:05
Venerdì, 12/02/2010

:om:

Ultima modifica effettuata da Federico1976 il 13/02/2010 alle 10:37
PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo