首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

实现这样的效果如何做呀

2013-09-25 
实现这样的效果怎么做呀namespace WindowsFormsApplication1{public partial class Form1 : Form{Graphics

实现这样的效果怎么做呀

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        GraphicsPath gp1 = null;
        GraphicsPath gp2 = null;
        GraphicsPath gp3 = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            Bitmap bit = new Bitmap(panel3.Width, panel3.Height);
            Graphics g = Graphics.FromImage(bit);
            g.Clear(Color.Transparent);
            g.DrawImage(Properties.Resources._1, 0, 0, panel3.Height, panel3.Width);
            gp1 = GetTransparentBounds(bit);

            g.Clear(Color.Transparent);
            g.DrawImage(Properties.Resources._2, 0, 0, panel3.Height, panel3.Width);
            gp2 = GetTransparentBounds(bit);

            g.Clear(Color.Transparent);
            g.DrawImage(Properties.Resources._3, 0, 0, panel3.Height, panel3.Width);
            gp3 = GetTransparentBounds(bit);

            g.DrawImage(Properties.Resources._1, 0, 0, panel3.Height, panel3.Width);
            g.DrawImage(Properties.Resources._2, 0, 0, panel3.Height, panel3.Width);


            g.DrawImage(Properties.Resources._3, 0, 0, panel3.Height, panel3.Width);
            panel3.BackgroundImage = bit;
            
            
        }
        public GraphicsPath GetTransparentBounds(Bitmap source)
        {
            GraphicsPath gp = new GraphicsPath();
            for (int i = 0; i < source.Width; i++)
            {
                for (int j = 0; j < source.Height; j++)
                {
                    Color cl;
                    cl = source.GetPixel(i, j);
                    if (cl.A != 0)
                    {
                        gp.AddRectangle(new Rectangle(i, j, 1, 1));
                    }
                }
            }

            return gp;
        }

        private void panel3_MouseMove(object sender, MouseEventArgs e)
        {
                       
            if (gp1.PathPoints.Contains(e.Location) ){ MessageBox.Show("gp1"); };


            if (gp2.PathPoints.Contains(e.Location)) { MessageBox.Show("gp2"); };
            if (gp3.PathPoints.Contains(e.Location)) { MessageBox.Show("gp3"); };
        }
    }
}


[解决办法]
如果你能保证没有颜色的地方没有图形,还不如判断像素点的颜色。
[解决办法]
应该是source.GetPixel(i,j)太慢了,用下面这个,应该能提速1000倍

BitmapData bitmapData=source.LockBits(new Rectangle(0,0,source.Width,source.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, source.PixelFormat);

public unsafe Color GetPixel(int x,int y)
        {
            if (bitmapData.PixelFormat == PixelFormat.Format32bppRgb 
[解决办法]
 bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
            {
                byte* numPtr = (byte*)((int)bitmapData.Scan0 + y * bitmapData.Stride + x * 4);
                return Color.FromArgb(numPtr[3],numPtr[2], numPtr[1],numPtr[0]);
            }
            if (bitmapData.PixelFormat == PixelFormat.Format24bppRgb)
            {
                byte* numPtr2 = (byte*)((int)bitmapData.Scan0 + y * bitmapData.Stride + x * 3);
                return Color.FromArgb(numPtr2[2], numPtr2[1], numPtr2[0]);


            }
            return Color.Empty;
        }


[解决办法]
if (Properties.Resources._1.GetPixel(e.X, e.Y).A != 0) MessageBox.Show("gp1");
if (Properties.Resources._2.GetPixel(e.X, e.Y).A != 0) MessageBox.Show("gp2");
if (Properties.Resources._3.GetPixel(e.X, e.Y).A != 0) MessageBox.Show("gp3");

热点排行