1

I have read through several articles which are alternatives to using setpixel/getpixel but I am not seeing how they relate to my issue.

I have a multicoloured image which (depending on dynamic values taken from DB) changes the color of each pixel color group to a new color.

Lets say I want all pixels with Argb value of -989556 changed to -16 & all pixels with Argb value of -1331 changed to -5383962. I am currently looking at every pixel in the Bitmap and checking their value, if they match then they are changed using setpixel.

I am sure there must be a better way but I am being too dim to see it!

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
T1gg3rB0unC3
  • 151
  • 1
  • 10
  • 2
    What is the problem you have doing it using GetPixel()/SetPixel()? Please tell us what UI lib you are coding against, WinForms, WPF, SilverLight... – Slugart Jun 25 '12 at 15:46
  • Set/getpixel just too slow over web (images are dynamically created). Just using it through a .net web app, delivering an image through a generic handler. – T1gg3rB0unC3 Jun 25 '12 at 15:52
  • I will check out color transformation matrix - thank you for suggestion.. – T1gg3rB0unC3 Jun 25 '12 at 15:53
  • @T1gg3rB0unC3: SetPixel is slow everywhere. You can get a pointer to the image data by calling `Bitmap.LockBits`, which is quite fast. if the matrix works for you go with that though. – Ed S. Jun 25 '12 at 15:57
  • 1
    @Slugart: I've (tentatively) added the *gdi+* tag, as that seems to be what everyone's talking about (*if* everyone's talking about the same API, that is). Without stating that, the question may be somewhat useless for future visitors. – O. R. Mapper Jun 25 '12 at 15:58
  • 1
    @T1gg3rB0unC3, For future question avoid adding "thank you notes", instead up-vote good answers and accept one. Also whenever you say "better way" try to specify in which way something should be "better". I.e. in this question you probably want "faster"... – Alexei Levenkov Jun 25 '12 at 15:59

3 Answers3

2

Take a look at ImageAttributes.SetRemapTable

Another example.

hometoast
  • 11,522
  • 5
  • 41
  • 58
0

If you are changing entire colors then you probably do not want to change them on a per pixel basis. There is a color transformation matrix that should allow you to change all pixles of a color in an image in a few lines of code, without having to loop through and manipulate each pixel of an image, which is horrendously slow.

See these links:

http://msdn.microsoft.com/en-us/library/6tf7sa87(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.colormatrix.aspx

Neil N
  • 24,862
  • 16
  • 85
  • 145
  • A transform is a great idea as long as the OP is ok with values overflowing and not being capped. I remember trying this years (4 or more) ago and that was a problem for me, so I ended up simply calling `LockBits` and doing the work directly in memory. – Ed S. Jun 25 '12 at 15:56
  • I've never actually used the color matrix class myself, but have used various forms of lockbits or pinned arrays with great success. – Neil N Jun 25 '12 at 15:59
0

Use bitmap, BitBlt functionality. I show an example in here.

Shahadat Hossain
  • 2,542
  • 3
  • 17
  • 13