怎么样把一个bmp图片中指定的颜色设置为透明?我有两张图片想把他们做成层叠起来的效果,但按照下面的代码执行完之后不能达到效果,边上没有都是白色的,而且顶层图片的白色会当住下面的图片,怎么办啊?救命。为什么不是透明的阿?
        private void Form1_Load(object sender, System.EventArgs e)
        {
            int width = image.Width;
            int height= image.Height;
            Color tmp = image.GetPixel(1,1);
            for(int i=0;i<width;i++)
            {
                for(int j=0;j<height;j++)
                {
                    if(image.GetPixel(i,j) == tmp)
                    {
                        image.SetPixel(i,j,Color.Transparent);
                    }
                }
            }           
        }        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics dc = e.Graphics;
            dc.DrawImage(image,10,10,100,100);
            dc.DrawImage(image,50,50,100,100);
        }

解决方案 »

  1.   

    应该用MakeTransparent这个方法吧
    bmp.MakeTrasparent(MaskColor)
      

  2.   

    完成了窗体布局,我们接着开始编写代码。首先,我们添加窗体的Paint消息响应函数。读者可以发现,我们在进行窗体布局的时候并没有在窗体上添加pictureBox控件,那么我们怎么显示图像并画出各种图形呢?这就要用到窗体的Paint消息响应函数了,函数实现如下: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    DrawPerson();
    //画一幅图像 
    Image curImage = Image.FromFile(@"e:\photo.jpg");
    g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height);
    g.Dispose();
    }
     
    (注:其中的e:\photo.jpg为笔者电脑上图像文件的路径,读者可以将它改为自己电脑上图像文件的路径,下同。该文件可在源代码文件夹中找到。) 在上面的函数中,我们先建立了一个Graphics对象,该对象就是专门用来画图形和图像的。我们还调用了本类的一个私有成员函数DrawPerson(),该函数实现的功能就是在窗体上画一些图形,这些图形包括了椭圆、曲线、三角形,而这些图形合在一起恰好构成了一个人的头部轮廓,所以我姑且将该函数命名为了DrawPerson()。在调用完毕后,就在窗体上画出一幅图像(用一个图像文件完成)。最后就是Graphics对象的Dispose工作。其中,DrawPerson()函数的具体实现如下: private void DrawPerson()
    {
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    //画人头轮廓
    Rectangle rect1 = new Rectangle(75, 0, 150, 200); 
    g.FillEllipse(new SolidBrush(Color.FromArgb(225, 155, 150, 25)), rect1);
    //定义三支画笔,其中第一支为不透明,第二支为半透明,第三支为强透明
    Pen opqPen = new Pen(Color.FromArgb(155, 120, 205, 190), 15);
    Pen transPen = new Pen(Color.FromArgb(128, 25, 150, 25), 12);
    Pen totTransPen = new Pen(Color.FromArgb(140, 120, 18, 10), 10);
    //画人头的眼睛
    Rectangle rect2 = new Rectangle(70, 0, 160, 80);
    g.DrawArc( opqPen, rect2, 30, 40 );
    g.DrawArc( opqPen, rect2, 110, 40 );
    //画人头的鼻子
    Point[] threePoints=new Point[]{new Point(140, 130), new Point(160, 130), new 
    Point(150, 80)};
    g.DrawPolygon(transPen,threePoints);
    //画人头的嘴巴
    Rectangle rect3 = new Rectangle(110, 150, 80, 20);
    g.DrawEllipse(totTransPen,rect3);
    g.Dispose();
    }
     
    请读者注意,在上面的函数中,我们定义画笔的时候定义了三支透明程度不同的画笔:不透明、半透明、强透明,如此一来就可以完成图形的透明效果。不过,图像的透明效果实现起来相对复杂,我将会在后面介绍。 这样,我们就完成了窗体的初始化工作。在运行程序后,窗体上就不是空白一片,而是有具体的图像了。由于我们的图像比较大,所以窗体上的图形在最初的时候是看不见的,只有当图像为透明时,其中的人头图形才会显现,这就正好符合我们程序的初衷了。 接下来,我们就着手实现图像的透明效果。在实现图像的透明效果过程中,我们需要用到了ColorMatrix和ImageAttributes等类,而这些类包含在System.Drawing.Imaging名字空间中,所以我们在源代码文件的开始处需添加:using System.Drawing.Imaging;来实现对这些类的调用。还有,ImageAttributes类是用来设置图像的一系列属性的,它被用作Graphics类对象的DrawImage方法的一个参数。而ImageAttributes类对象的方法SetColorMatrix则调用ColorMatrix来设置图像的颜色值。而图像的透明效果正是又ColorMatrix中部分值所决定的。下面就是具体的实现方法了。 添加三个按钮的消息响应函数如下: "半透明"按钮: private void button1_Click(object sender, System.EventArgs e)
    {
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    DrawPerson();
    Bitmap bitmap = new Bitmap(@"e:\photo.jpg");
    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}, //注意:此处为0.5f,图像为半透明
    new float[] {0, 0, 0, 0, 1}}; 
    ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
    ImageAttributes imgAttributes = new ImageAttributes();
    //设置图像的颜色属性
    imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, 
    ColorAdjustType.Bitmap);
    //画图像
    g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 
    0, 0, bitmap.Width, bitmap.Height,
    GraphicsUnit.Pixel, imgAttributes);
    g.Dispose();
    }
     
    "强透明"按钮: private void button2_Click(object sender, System.EventArgs e)
    {
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    DrawPerson();
    Bitmap bitmap = new Bitmap(@"e:\photo.jpg");
    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.1f, 0}, //注意:此处为0.1f,图像为强透明
    new float[] {0, 0, 0, 0, 1}}; 
    ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
    ImageAttributes imgAttributes = new ImageAttributes();
    //设置图像的颜色属性
    imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, 
    ColorAdjustType.Bitmap);
    //画图像
    g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 
    0, 0, bitmap.Width, bitmap.Height,
    GraphicsUnit.Pixel, imgAttributes);
    g.Dispose();
    }
     
    "不透明"按钮: private void button3_Click(object sender, System.EventArgs e)
    {
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    DrawPerson();
    //画一幅图像 
    Image curImage = Image.FromFile(@"e:\photo.jpg");
    g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height);
    g.Dispose();
    }
     
    这样,我们就完成了三个按钮的消息响应函数,整个程序也完毕了。
      

  3.   

    没必要这么复杂,你做一张PNG的图片即可。
    将图片设置成透明的,然后把图层在PS里设置一下透明度即可,你的程序没问题。