c# - Convert ARGB to PARGB -
i've been looking fast alternative method of setpixel() , have found link : c# - faster alternatives setpixel , getpixel bitmaps windows forms app
so problem i've image , want create copy directbitmap object first need convert argb pargb used code:
public static color premultiplyalpha(color pixel) { return color.fromargb( pixel.a, premultiplyalpha_component(pixel.r, pixel.a), premultiplyalpha_component(pixel.g, pixel.a), premultiplyalpha_component(pixel.b, pixel.a)); } private static byte premultiplyalpha_component(byte source, byte alpha) { return (byte)((float)source * (float)alpha / (float)byte.maxvalue + 0.5f); }
and here's copy code:
directbitmap dbmp = new directbitmap(img.width, img.height); myimage myimg = new myimage(img bitmap); (int = 0; < img.width; i++) { (int j = 0; j < img.height; j++) { color pargb = nativewin32.premultiplyalpha(color.fromargb(myimg.rgb[i, j].alpha, myimg.rgb[i, j].r, myimg.rgb[i, j].g, myimg.rgb[i, j].b)); byte[] bitmapdata = new byte[4]; bitmapdata[3] = (byte)pargb.a; bitmapdata[2] = (byte)pargb.r; bitmapdata[1] = (byte)pargb.g; bitmapdata[0] = (byte)pargb.b; dbmp.bits[(i * img.height) + j] = bitconverter.toint32(bitmapdata, 0); } }
myimage : class containing bitmap object along array of struct rgb storing colors of each pixel
however, code gives me messed image. doing wrong?
bitmap data organized horizontal line after horizontal line. therefore, last line should be:
dbmp.bits[j * img.width + i] = bitconverter.toint32(bitmapdata, 0);
Comments
Post a Comment