Results 1 to 2 of 2

Thread: WriteableBitmap - CopyPixels, Invert then WritePixels

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    WriteableBitmap - CopyPixels, Invert then WritePixels

    Hi I have a BitmapSource which is 150x150 pixels. I would like to invert the pixel colour from position (x=0, y=75) to position (x=75, y=75) so basically just a 1 pixel thick line from the center left to center center.

    The closest I have managed so far is by using:
    Code:
    Dim BMP As New WriteableBitmap(BitmapSource)
    Dim Stride As Integer = CInt(75 * (BMP.Format.BitsPerPixel + 7) / 8)
    Dim PixelData(Stride) As Integer
    BMP.CopyPixels(New Int32Rect(0, 75, 75, 1), PixelData, Stride, 0)
    For i As Integer = 0 To PixelData.Count - 1
      PixelData(i) = 255 - PixelData(i)
    Next i
    BMP.WritePixels(New Int32Rect(0, 74, 75, 1), PixelData, Stride, 0)
    The line appears to be getting drawn in the correct place. But my Bitmap changes as the mouse moves across the screen and the color of the line does not reflect what is at the mouse position. I am also not sure how to properly invert the color since every example I have found is different from every other so it is just confusing me.

    Thanks,
    Jay

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: WriteableBitmap - CopyPixels, Invert then WritePixels

    I seem to have stumbled into the desired result.. although I wonder if it can be improved upon:
    Code:
    Dim BMP As New WriteableBitmap(BitmapSource)
    Dim ByteCount As Integer = CInt(Math.Truncate((BMP.Format.BitsPerPixel + 7) / 8))
    Dim Stride As Integer = BMP.PixelWidth * ByteCount
    Dim PixelData(Stride - 1) As Byte
    
    BMP.CopyPixels(New Int32Rect(0, 74, 75, 1), PixelData, Stride, 0)
    For i As Integer = 0 To PixelData.Count - 1 Step ByteCount
      PixelData(i) = CByte(255 - PixelData(i))
      PixelData(i + 1) = CByte(255 - PixelData(i + 1))
      PixelData(i + 2) = CByte(255 - PixelData(i + 2))
    Next i
    BMP.WritePixels(New Int32Rect(0, 74, 75, 1), PixelData, Stride, 0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width