Bonsoir,
J'ai essayer de convertir la fonction suivante ecrite en C# grace à plusieurs convertisseurs en ligne :
Trouvé ici
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 private Rectangle searchBitmap(Bitmap smallBmp, Bitmap bigBmp, double tolerance) { BitmapData smallData = smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapData bigData = bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); int smallStride = smallData.Stride; int bigStride = bigData.Stride; int bigWidth = bigBmp.Width; int bigHeight = bigBmp.Height - smallBmp.Height + 1; int smallWidth = smallBmp.Width * 3; int smallHeight = smallBmp.Height; Rectangle location = Rectangle.Empty; int margin = Convert.ToInt32(255.0 * tolerance); unsafe { byte* pSmall = (byte*)(void*)smallData.Scan0; byte* pBig = (byte*)(void*)bigData.Scan0; int smallOffset = smallStride - smallBmp.Width * 3; int bigOffset = bigStride - bigBmp.Width * 3; bool matchFound = true; for (int y = 0; y < bigHeight; y++) { for (int x = 0; x < bigWidth; x++) { byte* pBigBackup = pBig; byte* pSmallBackup = pSmall; //Look for the small picture. for (int i = 0; i < smallHeight; i++) { int j = 0; matchFound = true; for (j = 0; j < smallWidth; j++) { //With tolerance: pSmall value should be between margins. int inf = pBig[0] - margin; int sup = pBig[0] + margin; if (sup < pSmall[0] || inf > pSmall[0]) { matchFound = false; break; } pBig++; pSmall++; } if (!matchFound) break; //We restore the pointers. pSmall = pSmallBackup; pBig = pBigBackup; //Next rows of the small and big pictures. pSmall += smallStride * (1 + i); pBig += bigStride * (1 + i); } //If match found, we return. if (matchFound) { location.X = x; location.Y = y; location.Width = smallBmp.Width; location.Height = smallBmp.Height; break; } //If no match found, we restore the pointers and continue. else { pBig = pBigBackup; pSmall = pSmallBackup; pBig += 3; } } if (matchFound) break; pBig += bigOffset; } } bigBmp.UnlockBits(bigData); smallBmp.UnlockBits(smallData); return location; }
Donne :
J'ai remplacé les déclarations "Pointer(Of Byte)" par des "IntPtr" comme conseillé.
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 Private Function searchBitmap(smallBmp As Bitmap, bigBmp As Bitmap, tolerance As Double) As Rectangle Dim smallData As BitmapData = smallBmp.LockBits(New Rectangle(0, 0, smallBmp.Width, smallBmp.Height), System.Drawing.Imaging.ImageLockMode.[ReadOnly], System.Drawing.Imaging.PixelFormat.Format24bppRgb) Dim bigData As BitmapData = bigBmp.LockBits(New Rectangle(0, 0, bigBmp.Width, bigBmp.Height), System.Drawing.Imaging.ImageLockMode.[ReadOnly], System.Drawing.Imaging.PixelFormat.Format24bppRgb) Dim smallStride As Integer = smallData.Stride Dim bigStride As Integer = bigData.Stride Dim bigWidth As Integer = bigBmp.Width Dim bigHeight As Integer = bigBmp.Height - smallBmp.Height + 1 Dim smallWidth As Integer = smallBmp.Width * 3 Dim smallHeight As Integer = smallBmp.Height Dim location As Rectangle = Rectangle.Empty Dim margin As Integer = Convert.ToInt32(255.0 * tolerance) Dim pSmall As Pointer(Of Byte) = CType(CType(smallData.Scan0, Pointer(Of System.Void)), Pointer(Of Byte)) Dim pBig As Pointer(Of Byte) = CType(CType(bigData.Scan0, Pointer(Of System.Void)), Pointer(Of Byte)) Dim smallOffset As Integer = smallStride - smallBmp.Width * 3 Dim bigOffset As Integer = bigStride - bigBmp.Width * 3 Dim matchFound As Boolean = True For y As Integer = 0 To bigHeight - 1 For x As Integer = 0 To bigWidth - 1 Dim pBigBackup As Pointer(Of Byte) = pBig Dim pSmallBackup As Pointer(Of Byte) = pSmall 'Look for the small picture. For i As Integer = 0 To smallHeight - 1 Dim j As Integer = 0 matchFound = True For j = 0 To smallWidth - 1 'With tolerance: pSmall value should be between margins. Dim inf As Integer = pBig(0) - margin Dim sup As Integer = pBig(0) + margin If sup < pSmall(0) OrElse inf > pSmall(0) Then matchFound = False Exit For End If pBig += 1 pSmall += 1 Next If Not matchFound Then Exit For End If 'We restore the pointers. pSmall = pSmallBackup pBig = pBigBackup 'Next rows of the small and big pictures. pSmall += smallStride * (1 + i) pBig += bigStride * (1 + i) Next 'If match found, we return. If matchFound Then location.X = x location.Y = y location.Width = smallBmp.Width location.Height = smallBmp.Height Exit For Else 'If no match found, we restore the pointers and continue. pBig = pBigBackup pSmall = pSmallBackup pBig += 3 End If Next If matchFound Then Exit For End If pBig += bigOffset Next bigBmp.UnlockBits(bigData) smallBmp.UnlockBits(smallData) Return location End Function
Le probléme j'ai le messagequi correspond aux lignes 43,44 et 45 avec les Pointer(0).Structure 'System.IntPtr' cannot be indexed because it has no default property
C'est bien les Pointer(0) qui ne lui plaisent pas, je pense les convertir avec la class "Marshal", mais je ne vois pas comment l'utiliser pour le faire.
Merci
Ren
Partager