1

I want to make program in wpf the can detect the red color(255) and in wpf I think is very hard to do that all I want to do is to convert the image.source to bitmap to use the method bitmap.getpixel(x,y).R=255 to check pixels one by one so I use method copypixel and then create bitmap but when I run Iget (System.ArgumentException: 'Parameter is not valid.') so please I want help to solve this problem

 Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)

   
    Dim image = New BitmapImage(New Uri("pixel3.bmp", UriKind.Relative))
    ___imagebox1_.Source = image
    Dim bitmapImage As BitmapSource = CType(___imagebox1_.Source, BitmapSource)
    Dim height As Integer = bitmapImage.PixelHeight
    Dim width As Integer = bitmapImage.PixelWidth
    Dim nStride As Integer = bitmapImage.PixelWidth * ((bitmapImage.Format.BitsPerPixel + 7) / 8)
    Dim memoryBlockPointer = Marshal.AllocHGlobal(height * nStride)
    bitmapImage.CopyPixels(New Int32Rect(0, 0, width, height), memoryBlockPointer, height * nStride, nStride)
    Dim pixelByteArray As Byte() = New Byte(bitmapImage.PixelHeight * nStride - 1) {}
    Dim heightaftercopy As Integer = bitmapImage.PixelHeight
    Dim widthaftercopy As Integer = bitmapImage.PixelWidth
    Dim bitmap = New Bitmap(widthaftercopy, heightaftercopy, nStride, PixelFormat.Format32bppPArgb, memoryBlockPointer)

End Sub

 
        
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Maybe you could just use something like this? https://stackoverflow.com/a/14878596/1136211. Of course call CopyPixels only once and keep the buffer. – Clemens Jun 22 '20 at 17:02
  • You can use a [CroppedBitmap](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.croppedbitmap.-ctor) class. It accepts a BitmapSource and an Int32Rect as input. The rect defines the position of the Pixel to read (e.g., `100, 100, 1, 1`). Then use the CopyPixels method (derived from BitmapSource), passing an array of bytes to read the Color bytes (you should work with bgr32 or Pbgra32 bitmaps) – Jimi Jun 22 '20 at 17:03

0 Answers0