c# - Can you change one colour to another in an Bitmap image? -
for bitmap
, there maketransparent
method, there 1 similar changing 1 color another?
// sets color.white transparent bitmap mybitmap = new bitmap(sr.stream); mybitmap.maketransparent(system.drawing.color.white);
is there can this?
bitmap mybitmap = new bitmap(sr.stream); mybitmap.changecolor(system.drawing.color.black, system.drawing.color.gray);
through curiosity yorye nathan's comment, extension created modifying http://msdn.microsoft.com/en-gb/library/ms229672(v=vs.90).aspx.
it can turn pixels in bitmap 1 colour another.
public static class bitmapext { public static void changecolour(this bitmap bmp, byte incolourr, byte incolourg, byte incolourb, byte outcolourr, byte outcolourg, byte outcolourb) { // specify pixel format. pixelformat pxf = pixelformat.format24bpprgb; // lock bitmap's bits. rectangle rect = new rectangle(0, 0, bmp.width, bmp.height); bitmapdata bmpdata = bmp.lockbits(rect, imagelockmode.readwrite, pxf); // address of first line. intptr ptr = bmpdata.scan0; // declare array hold bytes of bitmap. // int numbytes = bmp.width * bmp.height * 3; int numbytes = bmpdata.stride * bmp.height; byte[] rgbvalues = new byte[numbytes]; // copy rgb values array. marshal.copy(ptr, rgbvalues, 0, numbytes); // manipulate bitmap (int counter = 0; counter < rgbvalues.length; counter += 3) { if (rgbvalues[counter] == incolourr && rgbvalues[counter + 1] == incolourg && rgbvalues[counter + 2] == incolourb) { rgbvalues[counter] = outcolourr; rgbvalues[counter + 1] = outcolourg; rgbvalues[counter + 2] = outcolourb; } } // copy rgb values bitmap marshal.copy(rgbvalues, 0, ptr, numbytes); // unlock bits. bmp.unlockbits(bmpdata); } }
called bmp.changecolour(0,128,0,0,0,0);
Comments
Post a Comment