Exemple #1
0
        ////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));
            }
        }
Exemple #2
0
        //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;
                    }
                }
            }
        }