對圖像透明化的處理

對圖像透明化的處理
原始的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

發佈了41 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章