对图像透明化的处理
原始的picturebox
要实现这种模态效果
代码如下
private void button5_Click(object sender, EventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
Rectangle rect = new Rectangle(20,20,80,80);
Bitmap bitmap = new Bitmap(@"D:\test.PNG");
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);
g.FillRectangle(Brushes.White, rect);
g.DrawImage(bitmap,
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);
// Dispose
g.Dispose();
}
要实现下面的透明效果(即能看到窗体底层的颜色):
代码如下
方法一
private void button3_Click(object sender, EventArgs e)
{
//pictureBox1_Paint();
Bitmap myBitmap = new Bitmap(@"D:\test.PNG");
Pen blackPen = new Pen(Color.Transparent, 30);
int x1 = 50;
int y1 = 50;
int x2 = 100;
int y2 = 100;
using (var graphics = Graphics.FromImage(myBitmap))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.FillRectangle(Brushes.Transparent, x1, y1, x2, y2);
}
//myBitmap.MakeTransparent(Color.Red);
pictureBox1.Image = myBitmap;
}
方法二
private void button6_Click(object sender, EventArgs e)
{
Bitmap imageFileName = new Bitmap(@"D:\test.PNG");
Bitmap sourceImage = new Bitmap(imageFileName);
Bitmap image = new Bitmap(sourceImage.Width, sourceImage.Height);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Transparent);
g.DrawImage(sourceImage, 0, 0, sourceImage.Width, sourceImage.Height);
sourceImage.Dispose();
GraphicsPath path = new GraphicsPath();
Rectangle r = new Rectangle(20, 20, 80, 80);
path.AddRectangle(r);
g.SetClip(path);
g.Clear(Color.Transparent);
pictureBox1.Image = image;
}
实现如下选定区域之外的透明化:
private void button4_Click(object sender, EventArgs e)
{
this.pictureBox1.Image = new Bitmap(@"D:\test.PNG");
System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); //Gdi+
shape.AddEllipse(0,0,100,100); //绘制椭圆窗口
this.pictureBox1.Region = new System.Drawing.Region(shape);
}
其中有一种使用DllImport方法如下
private void button7_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"D:\test.PNG");
POINTAPI[] poin;
poin = new POINTAPI[5];
poin[0].x = 0;
poin[0].y = 0;
poin[1].x = 90;
poin[1].y = 100;
poin[2].x = 150;
poin[2].y = 150;
poin[3].x = 100;
poin[3].y = 100;
poin[4].x = 0;
poin[4].y = Width;
Boolean flag = true;
IntPtr hRgn = CreatePolygonRgn(ref poin[0], 8, 1);
SetWindowRgn(this.pictureBox1.Handle, hRgn, ref flag);
this.pictureBox1.BackColor = Color.BurlyWood;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINTAPI
{
internal int x;
internal int y;
}
[DllImport("gdi32.dll")]
private static extern IntPtr CreatePolygonRgn(ref POINTAPI lpPoint, int nCount, int nPolyFillMode);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, ref Boolean bRedraw);
}
效果图如下
参照链接:
#Remove a selected part of an image in C#
http://csharphelper.com/blog/2014/04/remove-a-selected-part-of-an-image-in-c/
#Drawing Transparent Images and Shapes using Alpha Blending
http://www.c-sharpcorner.com/UploadFile/mahesh/DrawTransparentImageUsingAB10102005010514AM/DrawTransparentImageUsingAB.aspx