请教: 一张图有一矩形区域是透明的,要用另一张图填充到这个区域里,用C#实现我的想法是: 先得到透明区域的起始(x1,y1)和终点(x2,y2),再用g.DrawImage(img,x1,y1,x2,y2);
但怎么得到一张图片透明区域的起始(x1,y1)和终点(x2,y2)呢?好心人给指点下吧,谢谢!

解决方案 »

  1.   

    static void Test()
    {
        Bitmap bmp = new Bitmap(200,200);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.Yellow);
            g.FillRectangle(Brushes.Blue, new Rectangle(10, 10, 30, 50));
        }
        bmp.MakeTransparent(Color.Blue);    Rectangle rect = GetTransparentRect(bmp);   // rect = {10,10,30,50)
    }static Rectangle GetTransparentRect(Bitmap bmp)
    {
        Rectangle rect = new Rectangle(0, 0, 0, 0);    for (int y = 0; y < bmp.Height; y++)
        {
            for (int x = 0; x < bmp.Width; x++)
            {            Color c = bmp.GetPixel(x, y);
                if (bmp.GetPixel(x, y).A == 0)                 // find the topLeft pixle whose Alpha == 0
                {
                    rect.Location = new Point(x, y);
                    for (int w = x + 1; w < bmp.Width && bmp.GetPixel(w, y).A == 0; w++)
                    {
                        rect.Width = w - x + 1;
                    }
                    for (int w = y + 1; w < bmp.Height && bmp.GetPixel(x, w).A == 0; w++)
                    {
                        rect.Height = w - y + 1;
                    }
                    return rect;
                }
            }
        }
        return rect;
    }
      

  2.   

    一楼的代码写的很清楚,rect.Location 的X和Y就是透明区域的起始点(x1,y1),(rect.Location.X+rect.Width,rect.Location.Y+rect.Height)就是透明区域的终点(x2,y2)了.