public static void ApplyMatrixMultiplier(this Image sourceImage, double[,] matrixMultiplier) { unsafe { DLLImports.gdImageStruct *pStruct = (DLLImports.gdImageStruct *)sourceImage.gdImageStructPtr; for (int y = 0; y < sourceImage.HeightInPixels; y++) { for (int x = 0; x < sourceImage.WidthInPixels; x++) { int currentColor = pStruct->tpixels[y][x]; pStruct->tpixels[y][x] = ApplyFilter(currentColor, matrixMultiplier); } } } }
//Creating an avatar of the image public static Image CircleCrop(this Image sourceImage) { int radius, diameter; if (sourceImage.HeightInPixels < sourceImage.WidthInPixels) { radius = sourceImage.HeightInPixels / 2; } else { radius = sourceImage.WidthInPixels / 2; } diameter = 2 * radius; Image destinationImage = Image.Create(diameter, diameter); unsafe { DLLImports.gdImageStruct *pStruct = (DLLImports.gdImageStruct *)sourceImage.gdImageStructPtr; DLLImports.gdImageStruct *fStruct = (DLLImports.gdImageStruct *)destinationImage.gdImageStructPtr; for (int y = 0; y < diameter; y++) { for (int x = 0; x < diameter; x++) { int currentColor = pStruct->tpixels[y][x]; if ((x - radius) * (x - radius) + (y - radius) * (y - radius) > (radius * radius)) { //mask to just get the alpha value (7 bits) double currentAlpha = (currentColor >> 24) & 0xff; currentAlpha = 127; //make a new color with the new alpha to set the pixel currentColor = (currentColor & 0x00ffffff | ((int)currentAlpha << 24)); } fStruct->tpixels[y][x] = currentColor; } } } return(destinationImage); }
public static Image Load(Stream stream) { if (stream != null) { unsafe { IntPtr pNativeImage = IntPtr.Zero; var wrapper = new gdStreamWrapper(stream); pNativeImage = DLLImports.gdImageCreateFromPngCtx(ref wrapper.IOCallbacks); DLLImports.gdImageStruct *pStruct = (DLLImports.gdImageStruct *)pNativeImage; Image toRet = Image.Create(pStruct->sx, pStruct->sx); DLLImports.gdImageDestroy(toRet.gdImageStructPtr); toRet.gdImageStructPtr = pNativeImage; return(toRet); } } else { throw new InvalidOperationException(SR.NullStreamReferenced); } }
//Transparency public static void SetAlphaPercentage(this Image sourceImage, double opacityMultiplier) { if (opacityMultiplier > 1 || opacityMultiplier < 0) { throw new InvalidOperationException(SR.Format(SR.InvalidTransparencyPercent, opacityMultiplier)); } double alphaAdjustment = 1 - opacityMultiplier; unsafe { DLLImports.gdImageStruct *pStruct = (DLLImports.gdImageStruct *)sourceImage.gdImageStructPtr; for (int y = 0; y < sourceImage.HeightInPixels; y++) { for (int x = 0; x < sourceImage.WidthInPixels; x++) { int currentColor = pStruct->tpixels[y][x]; //mask to just get the alpha value (7 bits) double currentAlpha = (currentColor >> 24) & 0xff; //if the current alpha is transparent //dont bother/ skip over if (currentAlpha == 127) { continue; } //calculate the new alpha value given the adjustment currentAlpha += (127 - currentAlpha) * alphaAdjustment; //make a new color with the new alpha to set the pixel currentColor = (currentColor & 0x00ffffff | ((int)currentAlpha << 24)); pStruct->tpixels[y][x] = currentColor; } } } }
//Stamping an Image onto another public static void Draw(this Image destinationImage, Image sourceImage, int xOffset, int yOffset) { //turn alpha blending on for drawing DLLImports.gdImageAlphaBlending(destinationImage.gdImageStructPtr, 1); unsafe { DLLImports.gdImageStruct *pStructSource = (DLLImports.gdImageStruct *)sourceImage.gdImageStructPtr; DLLImports.gdImageStruct *pStructDest = (DLLImports.gdImageStruct *)destinationImage.gdImageStructPtr; //loop through the source image for (int y = 0; y < sourceImage.HeightInPixels; y++) { for (int x = 0; x < sourceImage.WidthInPixels; x++) { //ignores what falls outside the bounds of dsetination image if ((y + yOffset) >= destinationImage.HeightInPixels || (x + xOffset) >= destinationImage.WidthInPixels) { continue; } int sourceColor = pStructSource->tpixels[y][x]; int alpha = (sourceColor >> 24) & 0xff; //should not have 127 as magic if (alpha == 127) { continue; } int destColor = pStructDest->tpixels[y + yOffset][x + xOffset]; int blendedColor = DLLImports.gdAlphaBlend(destColor, sourceColor); pStructDest->tpixels[y + yOffset][x + xOffset] = blendedColor; } } } }