0
votes

I need to generate a new image based on merge from a solid color (background) and a semi transparent image. All the image will be semi transparent, so it will all blend with a solid color.

I tried:

 private Image BlendImageWithWindowBackgoundColor(Image pImage, float pColorOpacity)
    {
        Image mResult = null;
        if (pImage != null)
        {
            ColorMatrix matrix = new ColorMatrix(new float[][]{
            new float[] {1F, 0, 0, 0, 0},
            new float[] {0, 1F, 0, 0, 0},
            new float[] {0, 0, 1F, 0, 0},
            new float[] {0, 0, 0, pColorOpacity, 0}, //opacity in rage [0 1]
            new float[] {0, 0, 0, 0, 1F}});

            ImageAttributes imageAttributes = new ImageAttributes();
            imageAttributes.SetColorMatrix(matrix);
            imageAttributes.SetWrapMode(WrapMode.TileFlipXY);

            mResult = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(mResult);

            g.Clear(Color.Red); //<--This is the color i want merged as background!
            g.CompositingMode = CompositingMode.SourceCopy;
            g.CompositingQuality = CompositingQuality.HighQuality;

            g.DrawImage(pImage, new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), 0, 0, pImage.Width, pImage.Height, GraphicsUnit.Pixel, imageAttributes);
        }

        return mResult;

    }

If i try the pOpacity 0.5 cannot see RED, when pOpacity is 0 it returns black.

Even if i try with g.CompositingMode = CompositingMode.SourceOver it only generates a semitransparent image, ignores the red background.

2
If you are just merging then why do you use SetWrapMode?γηράσκω δ' αεί πολλά διδασκόμε

2 Answers

1
votes

If you want to tile an image you need to use a TextureBrush and Graphics.FillRectangle not Drawimage. Your code should be:

private void BlendImageWithWindowBackgoundColor( Image pImage, float pColorOpacity ) {
    Image mResult = null;
    Image tempImage = null; //we will set the opacity of pImage to pColorOpacity and copy
                            //it to tempImage 
    if( pImage != null ) {
        Graphics g;

        ColorMatrix matrix = new ColorMatrix( new float[][]{
                new float[] {1F, 0, 0, 0, 0},
                new float[] {0, 1F, 0, 0, 0},
                new float[] {0, 0, 1F, 0, 0},
                new float[] {0, 0, 0, pColorOpacity, 0}, //opacity in rage [0 1]
                new float[] {0, 0, 0, 0, 1F}} );

        ImageAttributes imageAttributes = new ImageAttributes();
        imageAttributes.SetColorMatrix( matrix );

        tempImage = new Bitmap( pImage.Width, pImage.Height, PixelFormat.Format32bppArgb );

        g = Graphics.FromImage( tempImage );
        g.Clear( Color.Transparent );
        //setting pColorOpacity to pImage and drawing to tempImage 
        g.DrawImage( pImage, new Rectangle( 0, 0, tempImage.Width, tempImage.Height ), 
                     0, 0, pImage.Width, pImage.Height, GraphicsUnit.Pixel, imageAttributes );

        g.Dispose();
        g = null;

        //now we will tile the tempImage
        TextureBrush texture = new TextureBrush( tempImage );
        texture.WrapMode = WrapMode.TileFlipXY;

        mResult = new Bitmap( Screen.PrimaryScreen.Bounds.Width,
                              Screen.PrimaryScreen.Bounds.Height,
                              PixelFormat.Format32bppArgb );

        g = Graphics.FromImage( mResult );
        g.Clear( Color.Red ); //<--This is the color i want merged as background!

        g.FillRectangle( texture, new Rectangle( 0, 0, mResult.Width, mResult.Height ) );

        g.Dispose();
        g = null;

        tempImage.Dispose();
        tempImage = null;

    }

    return mResult;

}
0
votes

Meanwhile I found a solution:

 private Image BlendImageWithWindowBackgoundColorToSize(Image pImage, float pColorOpacity)
    {
        Image mResult = null;
        if (pImage != null)
        {
            ColorMatrix matrix = new ColorMatrix(new float[][]{
            new float[] {1F, 0, 0, 0, 0},
            new float[] {0, 1F, 0, 0, 0},
            new float[] {0, 0, 1F, 0, 0},
            new float[] {0, 0, 0, pColorOpacity, 0}, //opacity in rage [0 1]
            new float[] {0, 0, 0, 0, 1F}});

            ImageAttributes imageAttributes = new ImageAttributes();
            imageAttributes.SetColorMatrix(matrix);
            imageAttributes.SetWrapMode(WrapMode.TileFlipXY);

            mResult = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            Image mImgSemiTransparent = (Image)mResult.Clone();
            Graphics g = Graphics.FromImage(mImgSemiTransparent);
            g.CompositingMode = CompositingMode.SourceOver; 
            g.CompositingQuality = CompositingQuality.HighQuality;           
            g.DrawImage(pImage, new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), 0, 0, pImage.Width, pImage.Height, GraphicsUnit.Pixel, imageAttributes);

            Graphics gF = Graphics.FromImage(mResult);
            gF.Clear(Color.Red);
            gF.DrawImageUnscaled(mImgSemiTransparent, 0, 0, mImgSemiTransparent.Width, mImgSemiTransparent.Height);
        }

        return mResult;
    }

But the one answered by γηράσκω δ' αεί πολλά διδασκόμε with a few teaks (it was not exactly what I wanted) is four times faster than mine! :)

        private Image BlendImageWithWindowBackgoundColorToSize2(Image pImage, float pColorOpacity)
    {
        Image mResult = null;
        Image tempImage = null; //we will set the opacity of pImage to pColorOpacity and copy
                                //it to tempImage 
        if (pImage != null)
        {
            Graphics g;

            ColorMatrix matrix = new ColorMatrix(new float[][]{
            new float[] {1F, 0, 0, 0, 0},
            new float[] {0, 1F, 0, 0, 0},
            new float[] {0, 0, 1F, 0, 0},
            new float[] {0, 0, 0, pColorOpacity, 0}, //opacity in rage [0 1]
            new float[] {0, 0, 0, 0, 1F}});

            ImageAttributes imageAttributes = new ImageAttributes();
            imageAttributes.SetColorMatrix(matrix);
            imageAttributes.SetWrapMode(WrapMode.TileFlipXY);
            tempImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            g = Graphics.FromImage(tempImage);
            //g.Clear(Color.Transparent); //No need!
            //setting pColorOpacity to pImage and drawing to tempImage 
            g.DrawImage(pImage, new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), 0, 0, pImage.Width, pImage.Height, GraphicsUnit.Pixel, imageAttributes);

            g.Dispose();
            g = null;

            //now we will tile the tempImage
            TextureBrush texture = new TextureBrush(tempImage);

            mResult = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                  Screen.PrimaryScreen.Bounds.Height,
                                  PixelFormat.Format32bppArgb);

            g = Graphics.FromImage(mResult);
            g.Clear(myColor);

            g.FillRectangle(texture, new Rectangle(0, 0, mResult.Width, mResult.Height));

            g.Dispose();
            g = null;

            tempImage.Dispose();
            tempImage = null;

        }

        return mResult;

    }