Writing to Image using Lock Bits in C# -


i'm working on optimizing program i'm working on, reads byte data using lock bits, writes pixel data using setpixel. how modify pixel data i'm reading in? if try setting pp, cp, or np, method won't work (since loops , needs pp, cp, , np represent pixel data), i'm confused. need write pixel data byte[] , manipulate it, or what?

here's code sample:

            bitmapdata data = img.lockbits(new rectangle(0, 0, img.width, img.height),             imagelockmode.readwrite, pixelformat.format24bpprgb);              int scaledpercent = (int)(math.round(percentageint * 255)) - 47;             debug.writeline("percent " + scaledpercent);             unsafe             {                 debug.writeline("woah there, unsafe stuff");                 byte* prevline = (byte*)data.scan0;                 byte* currline = prevline + data.stride;                 byte* nextline = currline + data.stride;                  (int y = 1; y < img.height - 1; y++)                 {                      byte* pp = prevline + 3;                     byte* cp = currline + 3;                     byte* np = nextline + 3;                     (int x = 1; x < img.width - 1; x++)                     {                         if (isedgeoptimized(pp, cp, np, scaledpercent))                         {                             //debug.writeline("x " + x + "y " + y);                              img2.setpixel(x, y, color.black);                          }                         else                         {                             img2.setpixel(x, y, color.white);                         }                         pp += 3; cp += 3; np += 3;                     }                     prevline = currline;                     currline = nextline;                     nextline += data.stride;                 }             }             img.unlockbits(data);             picturebox2.image = img2;         }     } } 

setpixel slow use compared getting raw bits array. looks you're doing kind of edge-detection (?). example lockbits on msdn (http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx) shows how raw array out , work that, saving result original image.

the interesting bits of example bytes copied of pointer using marshal.copy:

        // address of first line.         intptr ptr = bmpdata.scan0;          // declare array hold bytes of bitmap.          int bytes  = math.abs(bmpdata.stride) * bmp.height;        byte[] rgbvalues = new byte[bytes];          // copy rgb values array.         system.runtime.interopservices.marshal.copy(ptr, rgbvalues, 0, bytes); 

now have values need in rgbvalues array , can start manipulating these


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -