开发者

C# graphics draw transparent rectangle over area

I have two images layered ontop of each other and want to be able to clear a section of the top image. Normally if I wanted to cl开发者_StackOverflow中文版ear a section of an image I would just paint it the background color by doing

g.FillRectangle(Brushes.White,x,y,width,height);

but if I do that on the top image that area of the bottom image gets covered by the white rectangle. I tried doing

g.FillRectangle(Brushes.Transparent,x,y,width,height);

but that does not seem to clear the region of all of it's previous contents. Is there any way I can make the pixels in that region transparent?


//using System.Drawing.Drawing2D;

g.FillRectangle(Brushes.White,x,y,width,height);
g.CompositingMode = CompositingMode.SourceCopy;    
g.FillRectangle(Brushes.Transparent,x,y,width,height);


This is not possible.

GDI+ and the Graphics class do not support layered drawing; once you overwrite the previous image, those pixels are gone.

You should re-draw the portion of the previous image that you want to appear by calling a DrawImage overload that takes two rectangles.

If the lower image contains transparent portions, you should first clear that area to white (or whatever your original background is) by calling FillRectangle so that the transparency is overlaid correctly.


Another option is not to paint the images directly. Use:

System.Windows.Forms.PictureBox

and it's property

Region

to change the visibility/transaparency of the image. Region need not to be rectangle. It may be defined from any set of lines.

PS: Brushes.Transparent doesn't really mean transparent, but BackColor of the parent container.


float[][] ptsArray ={
            new float[] {1, 0, 0, 0, 0},
            new float[] {0, 1, 0, 0, 0},
            new float[] {0, 0, 1, 0, 0},
            new float[] {0, 0, 0, 0.5f, 0},
            new float[] {0, 0, 0, 0, 1}};
            ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
            ImageAttributes imgAttributes = new ImageAttributes();
            imgAttributes.SetColorMatrix(clrMatrix,
            ColorMatrixFlag.Default,
            ColorAdjustType.Bitmap);
            _ImageThumb.Height, imgAttributes);
            e.Graphics.DrawImage(_ImageThumb,new Rectangle(0, 0, _ImageThumb.Width,_ImageThumb.Height),0, 0, _ImageThumb.Width, _ImageThumb.Height,GraphicsUnit.Pixel, imgAttributes);

//use set clip & region to draw

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜