public static void Write(Image bmp, Stream stream) { DLLImports.gdImageSaveAlpha(bmp.gdImageStructPtr, 1); //MARSHALLING? DLLImports.gdImageStruct gdImageStruct = Marshal.PtrToStructure <DLLImports.gdImageStruct>(bmp.gdImageStructPtr); var wrapper = new gdStreamWrapper(stream); DLLImports.gdImagePngCtx(ref gdImageStruct, ref wrapper.IOCallbacks); }
/* constructors */ private Image(int width, int height) { if (width > 0 && height > 0) { gdImageStructPtr = DLLImports.gdImageCreateTrueColor(width, height); } else { string rsc = string.Format(Strings.CreateInvalidParameters, width, height); throw new InvalidOperationException(rsc); } }
//add png specific method later public static void Write(Image img, string filePath) { DLLImports.gdImageSaveAlpha(img.gdImageStructPtr, 1); if (!DLLImports.gdSupportsFileType(filePath, true)) { throw new InvalidOperationException(SR.Format(SR.FileTypeNotSupported, filePath)); } else { if (!DLLImports.gdImageFile(img.gdImageStructPtr, filePath)) { throw new FileLoadException(SR.Format(SR.WriteToFileFailed, filePath)); } } }
////test... //public static readonly double[,] AlphaMatrix040 = { {0.4, 0, 0, 0}, // {0, 1, 0, 0 }, // {0, 0, 1, 0}, // {0, 0, 0, 1} // }; //Resizing public static Image Resize(this Image sourceImage, int width, int height) { if (width > 0 && height > 0) { Image destinationImage = Image.Create(width, height); //turn off alpha blending to overwrite it right DLLImports.gdImageAlphaBlending(destinationImage.gdImageStructPtr, 0); DLLImports.gdImageCopyResized(destinationImage.gdImageStructPtr, sourceImage.gdImageStructPtr, 0, 0, 0, 0, destinationImage.WidthInPixels, destinationImage.HeightInPixels, sourceImage.WidthInPixels, sourceImage.HeightInPixels); return(destinationImage); } else { throw new InvalidOperationException(SR.Format(SR.ResizeInvalidParameters, width, height)); } }
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); } }
//add jpg specific method later public static Image Load(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException(string.Format(Strings.MalformedFilePath, filePath)); } else if (DLLImports.gdSupportsFileType(filePath, false)) { Image img = new Image(DLLImports.gdImageCreateFromFile(filePath)); if (!img.TrueColor) { DLLImports.gdImagePaletteToTrueColor(img.gdImageStructPtr); } return(img); } else { throw new FileLoadException(string.Format(Strings.FileTypeNotSupported, filePath)); } }
//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; } } } }
/* Release */ public void ReleaseStruct() { DLLImports.gdImageDestroy(gdImageStructPtr); }