2

In this case that i want to invert colors using this method, how would i do so? I know i need to subtract 255 (but the place i'm doing it is obviously wrong. it just keeps giving me a grayscale which i don't want)

        for (int x = 0; x < bmp.Width; x++)
        {
            //set the new image's pixel to the invert version

            nRow[x * pixelSize] = (byte)(255 - nRow[x + 0]); //B
            nRow[x * pixelSize + 1] = (byte)(255 - nRow[x + 1]); //G
            nRow[x * pixelSize + 2] = (byte)(255 - nRow[x + 2]); //R
        }
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • Just from looking at it (without understanding your code): is `nRow[0]` meant to be `nRow[x]`? – annonymously Jan 23 '12 at 07:57
  • Woops, yup, i was playing around with it and forgot to change it back.. i'll fix that.. – BigBug Jan 23 '12 at 07:57
  • What happens when you input a red pixel? (`FF0000`) – annonymously Jan 23 '12 at 08:04
  • Sorry, i don't think i quite understand. Where did you want me to try inputting a red pixel? – BigBug Jan 23 '12 at 08:05
  • 1
    Duplicate: http://stackoverflow.com/questions/1165107/how-do-i-invert-a-colour-color-c-net – Gert Arnold Jan 23 '12 at 08:10
  • @GertArnold, not quite, there are different ways of going about inverting an image... the way i'm playing with an image, is different from the link u posted.. – BigBug Jan 23 '12 at 08:12
  • Shot in the dark, but nRow[x * pixelSize] produces always zero in the first step? 0*3 = 0 Don't you need there the pixelSize instead of 0? – basti Jan 23 '12 at 08:46
  • @chiffre ... hmm, i don't think it would always be 0 though, right? x is always incrementing.. did u want me to try changing that though? – BigBug Jan 23 '12 at 08:50
  • @BlueMonster ...it's always zero in the first iteration of the loop. I don't think that this is right? – basti Jan 23 '12 at 09:00

2 Answers2

2

Try this:

        nRow[x * pixelSize] = (byte)(255 - oRow[x * pixelSize + 0]); //B
        nRow[x * pixelSize + 1] = (byte)(255 - oRow[x * pixelSize + 1]); //G
        nRow[x * pixelSize + 2] = (byte)(255 - oRow[x * pixelSize + 2]); //R
annonymously
  • 4,708
  • 6
  • 33
  • 47
  • hmm... it does almost the same thing as the last image i posted. the only difference is that the blue part has now turned all red... you can still see that part of the image though... other part of the screen is black still – BigBug Jan 23 '12 at 09:03
  • ahah, you got it. Thanks for taking the time and helping. +1 and selected answer. – BigBug Jan 23 '12 at 09:08
1

You have to change your subtraction to this

             for (int x = 0; x < bmp.Width; x++)
            {
                //set the new image's pixel to the invert version

                nRow[x * pixelSize] = (byte)(255 - oRow[x]); //changed from nRow to oRow
                //you subtracted every line three times?
            }

As far as I can see, you subtracted the new Picture from the new Picture ;)

basti
  • 2,649
  • 3
  • 31
  • 46
  • Hmm, it doesn't work... it still gives me a grayscale image... also, it's not a regular grayscale image... there are vertical lines running through it now, for some reason... – BigBug Jan 23 '12 at 08:09
  • What, if you turn it around? (byte)(oRow[x+0]-255) AND: Change it for test-reasons to my updated code. – basti Jan 23 '12 at 08:11
  • hmm, okay, i changed it so it's oRow and i copy-pasted (changed x + 1 and x+2 were appropriate as well)... now it gives a light gray version of the image.. but there are so many vertical lines that...i can't make out the image anymore... no color is present at all =/ – BigBug Jan 23 '12 at 08:15
  • 1
    @chiffre actually he is setting the 3 *bytes* at a time, not 3 pixels. Notice the `pixelSize`. – annonymously Jan 23 '12 at 08:19
  • Are you sure that the pixels are only 3 bytes? is it possible it's using an alpha channel? – annonymously Jan 23 '12 at 08:22
  • @annonymously, you mean "she" ;) ... this code works fine when you need to have a grayscale, so i'm assuming most of it is right.. the only thing extra it had was an array called "grayscale".. i initially got this code from this site: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale .. i've been playing around with it for awhile now.. not sure about an alpha channel.. can you give an example? – BigBug Jan 23 '12 at 08:26
  • maybe you could post a screenshot of the image? That way we'd have a better idea of what's wrong – annonymously Jan 23 '12 at 08:29
  • You have split the channels, if you zoom in to the image it goes `Blue, Red, Green`. Change this: `nRow[x * pixelSize]` to `nRow[x]` and so on – annonymously Jan 23 '12 at 08:49
  • @annonymously if i change all of them to nRow[x], some color does appear... but it's still not an inverse.. i'll post another image so you can see.. – BigBug Jan 23 '12 at 08:56