Reputation: 7518
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 clear 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?
Upvotes: 1
Views: 18448
Reputation: 1
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
Upvotes: 0
Reputation: 59
//using System.Drawing.Drawing2D;
g.FillRectangle(Brushes.White,x,y,width,height);
g.CompositingMode = CompositingMode.SourceCopy;
g.FillRectangle(Brushes.Transparent,x,y,width,height);
Upvotes: 5
Reputation: 1885
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.
Upvotes: 2
Reputation: 887195
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.
Upvotes: 2