lijgame的专栏

Footprints on the sands of time

原创 以半透明方式混合2张图,例子程序一个收藏

新一篇: 多线程学习笔记 | 旧一篇: 如何用c#来实现截屏

对图像文件进行处理的类主要是Bitmap类和ImageAttributes类:
    1、Bitmap类封装了GDI+位图,我们可以用这个类来打开、创建、编辑、保存一个图像。
    2、ImageAttributes类有许多可用于在图像处理过程中修改图像的属性,下面那段代码我们就是先创建了一个  ImageAttributes对象,然后把该对象以参数的形式传给Graphics.DrawImage()函数来控制如何绘图。

Graphics类是GDI+的核心类,我们通过它来进行各种操作。
创建graphics对象有3种方式:
    1、一般Paint事件的参数PaintEventArgs中含有一个Graphics对象的引用,当你写某个控件的绘图代码时可以这样获取Graphics对象。
    2、调用一个控件或form的CreateGraphics方法,这样就可以取得和这个控件(form)界面相关联的Graphics对象。当你想要在已有控件(form)上绘图时可以采用这种方法。
    3、从一个继承自Image类的对象中创建Graphics对象,利用Graphics类的静态函数FromImage()即可。当你想要编辑某张图时可以用这种方法。 

下面那个例子用的第三种方式来取得Graphics对象的。

这个程序打开了2张c盘目录下的图,然后,把其中一张当背景图,另一张以半透明的方式画在这张图上,然后把混合后的新图保存为mix.jpg文件。

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace MixPic
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
//打开一张图,作为背景图像
            Bitmap background = new Bitmap(@"c:\1.jpg");
            
//打开一张图,这是画在背景图像上的图像,要做半透明处理
            Bitmap frontImage = new Bitmap(@"c:\2.jpg");
            
//新建一个bitmap,存放混合后的图像
            int iwidth = background.Width > frontImage.Width ? background.Width : frontImage.Width;
            
int iheight = background.Height> frontImage.Height ? background.Height : frontImage.Height;
            Bitmap mixImage 
= new Bitmap(iwidth, iheight);

            
//创建mixImage的Graphics对象
            Graphics g = Graphics.FromImage(mixImage);

            
//为了将frontImage以半透明的方式画上去,我们要建一个ImageAttributes对象

            
//这个矩阵就像一个算子,在表示像素rgb值的前三行的对角线上都为1表明不改变这个图像的颜色
            
//第四行表示这个像素的alpha值,也就是透明度,这里我们设置为0.6,这样就有透明效果了
            
//这样一个矩阵与代表图像的矩阵相乘后,图像的rgb值都不会变
            
//仅仅alpha值会变成原来的0.6倍
            float[][] colormatrix = 
                                        
new float[]{1,0,0,0,0},//代表了R
                                        new float[]{0,1,0,0,0},//代表了G
                                        new float[]{0,0,1,0,0},//代表了B
                                        new float[]{0,0,0,0.6f,0},//代表了A
                                        new float[]{0,0,0,0,1} 
            }
;
            ColorMatrix cm 
= new ColorMatrix(colormatrix);
            ImageAttributes imageAtt 
= new ImageAttributes();
            imageAtt.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            
//先画背景图
            g.DrawImage(background, new Point(00));

            
//然后把frontImage已半透明的方式再画上去
            
//只要将imageatt这个参数传给DrawImage函数就可以了
            g.DrawImage(frontImage, new Rectangle(00, frontImage.Width, frontImage.Height), 00, frontImage.Width, frontImage.Height, GraphicsUnit.Pixel, imageAtt);

            
//保存文件
            mixImage.Save(@"c:\mix.jpg");

        }

    }

}

 

发表于 @ 2006年12月19日 16:46:00|评论(loading...)|编辑

新一篇: 多线程学习笔记 | 旧一篇: 如何用c#来实现截屏

评论

#bukebushuo 发表于2007-07-28 21:55:00  IP: 218.59.80.*
如何去掉某种颜色透明混合?比如第二张图片上的白色
#老A 发表于2007-12-06 15:40:25  IP: 219.142.122.*
学习了。。。谢谢了。。。
#阳 发表于2008-04-15 11:11:20  IP: 58.62.144.*
谢谢,,,这篇文章帮我大忙了。。。
发表评论  


登录
Csdn Blog version 3.1a
Copyright © lijgame