Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将半透明图像与背景色合并到新图像中_C#_Image Processing_Gdi+ - Fatal编程技术网

c#将半透明图像与背景色合并到新图像中

c#将半透明图像与背景色合并到新图像中,c#,image-processing,gdi+,C#,Image Processing,Gdi+,我需要根据从纯色(背景)和半透明图像合并生成一个新图像。 所有图像都是半透明的,因此都将与纯色混合 我试过: private Image BlendImageWithWindowBackgoundColor(Image pImage, float pColorOpacity) { Image mResult = null; if (pImage != null) { ColorMatrix matrix = new

我需要根据从纯色(背景)和半透明图像合并生成一个新图像。 所有图像都是半透明的,因此都将与纯色混合

我试过:

 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;

    }
私有图像BlendImageWithWindowBackgoundColor(图像pImage、浮点PCology)
{
图像mResult=null;
如果(pImage!=null)
{
ColorMatrix矩阵=新的ColorMatrix(新浮点[][]{
新浮点[]{1F,0,0,0,0},
新浮点[]{0,1F,0,0,0},
新浮点[]{0,0,1F,0,0},
新浮点[]{0,0,0,pColorOpacity,0},//rage中的不透明度[0 1]
新浮点[]{0,0,0,0,1F});
ImageAttributes ImageAttributes=新的ImageAttributes();
设置颜色矩阵(矩阵);
SetWrapMode(WrapMode.TileFlipXY);
mResult=新位图(Screen.PrimaryScreen.Bounds.Width、Screen.PrimaryScreen.Bounds.Height、PixelFormat.Format32bppArgb);
Graphics g=Graphics.FromImage(mResult);

g、 清除(Color.Red);//如果要平铺图像,需要使用
TextureBrush
Graphics.FillRectangle
而不是
Drawimage
。您的代码应该是:

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;

}
private void BlendImageWithWindowBackgoundColor(图像pImage、float pColorOpacity){
图像mResult=null;
Image tempImage=null;//我们将pImage的不透明度设置为pColorOpacity并复制
//它是临时图像
如果(pImage!=null){
图形g;
ColorMatrix矩阵=新的ColorMatrix(新浮点[][]{
新浮点[]{1F,0,0,0,0},
新浮点[]{0,1F,0,0,0},
新浮点[]{0,0,1F,0,0},
新浮点[]{0,0,0,pColorOpacity,0},//rage中的不透明度[0 1]
新浮点[]{0,0,0,0,1F});
ImageAttributes ImageAttributes=新的ImageAttributes();
设置颜色矩阵(矩阵);
tempImage=新位图(pImage.Width、pImage.Height、PixelFormat.Format32bppArgb);
g=图形。FromImage(tempImage);
g、 透明(彩色、透明);
//将pColorOpacity设置为pImage并将图形设置为tempImage
g、 DrawImage(pImage,新矩形(0,0,tempImage.Width,tempImage.Height),
0,0,pImage.Width,pImage.Height,GraphicsUnit.Pixel,imageAttributes);
g、 处置();
g=零;
//现在我们将平铺临时图像
TextureBrush纹理=新的TextureBrush(tempImage);
texture.WrapMode=WrapMode.TileFlipXY;
mResult=新位图(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
g=图形。来自图像(mResult);

g、 清除(颜色为红色);//同时我找到了一个解决方案:

 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;
    }
但γηράσκω′αεπολλδδδασκμε和几块柚木(这不完全是我想要的)的回答比我的快四倍:)


如果您只是在合并,那么为什么要使用
SetWrapMode
?使用
texture.WrapMode=WrapMode.Clamp;
进行简单合并而不重复图像。谢谢!
        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;

    }